#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  ! Copyright (C) 3L Ltd. 1987-1992
//     2
//     3  %routine Fail(%string(255) err)

static void FAIL(_imp_string255 ERR) {
  _imp_enter(FAIL);

  //     4    Printstring("Test ".err." failed.

  _imp_printf("%s%S%s", "Test ", _imp_strp(ERR), " failed.\n");

  //     5+ ")
  //     6    %monitor

  _imp_monitor_loc(__LINE__, __FILE__, __PRETTY_FUNCTION__);

  //     7    !%stop
  //     8  %end

  _imp_return;
  _imp_begin_backtrace(FAIL);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("ERR = \"%.*s\"\n", ERR.cstr.len, ERR.cstr.s);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_end_backtrace();
} /* end of FAIL */

//     9
//    10  %begin

int main(int argc, char **argv) { /*%begin*/
  _imp_enter(main);
  _imp_initialise(argc, argv);

  //    11    %constinteger Max = 6
  //    12    %string(Max) A, B, C

  _imp_string6 A;
  _imp_string6 B;
  _imp_string6 C;

  //    13    %integer j

  int J = 0xFEFEFEFE;

  //    14    %integer len, ch = 'A'

  int LEN = 0xFEFEFEFE;
  int CH = 0xFEFEFEFE;
  CH = 65;

  //    15    A = "";  B = "";  C = ""

  _imp_strcpy(&A, _imp_strp(_imp_str_literal("")), /*max:*/ 6);
  _imp_strcpy(&B, _imp_strp(_imp_str_literal("")), /*max:*/ 6);
  _imp_strcpy(&C, _imp_strp(_imp_str_literal("")), /*max:*/ 6);

  //    16    B = B.Tostring('0'+J) %for J = 1, 1, Max

  // %for J = 1, 1, 6 %cycle
  {
    J = 0;
  L_0001:
    if (J == 6) goto L_0002;
    J += 1;
    _imp_strassign(&B, 6, "%S%S", _imp_strp(B), _imp_strp(_imp_TOSTRING(48 + J)));
    goto L_0001;
  }

L_0002:;

//    17    printstring("A: """); printstring(A); printstring(""""); newline

  _imp_printf("%s", "A: \"");
  _imp_printf("%S", _imp_strp(A));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    18    printstring("B: """); printstring(B); printstring(""""); newline

  _imp_printf("%s", "B: \"");
  _imp_printf("%S", _imp_strp(B));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    19    printstring("C: """); printstring(C); printstring(""""); newline

  _imp_printf("%s", "C: \"");
  _imp_printf("%S", _imp_strp(C));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    20    Fail("string(6)/1") %unless A = "" %and C = ""


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(A), (_imp_string *)_imp_strp(_imp_str_literal(""))) != 0) goto L_0003;

  /* clang-format on */

  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(C), (_imp_string *)_imp_strp(_imp_str_literal(""))) == 0) goto L_0004;

/* clang-format on */
L_0003:;
  FAIL(_imp_str_literal("string(6)/1"));
L_0004:;

//    21    C = B

  _imp_strcpy(&C, _imp_strp(B), /*max:*/ 6);

  //    22    A = B

  _imp_strcpy(&A, _imp_strp(B), /*max:*/ 6);

  //    23    Charno(B, J) = 'a'+J %for J = 1, 1, Max

  // %for J = 1, 1, 6 %cycle
  {
    J = 0;
  L_0005:
    if (J == 6) goto L_0006;
    J += 1;
    *_imp_CHARNO((_imp_string255 *)&B, J) = 97 + J;
    goto L_0005;
  }

L_0006:;

//    24    Fail("string(6)/2") %unless A = C


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(A), (_imp_string *)_imp_strp(C)) == 0) goto L_0008;

  /* clang-format on */
  FAIL(_imp_str_literal("string(6)/2"));
L_0008:;

//    25    B = A

  _imp_strcpy(&B, _imp_strp(A), /*max:*/ 6);

  //    26    printstring("A: """); printstring(A); printstring(""""); newline

  _imp_printf("%s", "A: \"");
  _imp_printf("%S", _imp_strp(A));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    27    printstring("B: """); printstring(B); printstring(""""); newline

  _imp_printf("%s", "B: \"");
  _imp_printf("%S", _imp_strp(B));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    28    printstring("C: """); printstring(C); printstring(""""); newline

  _imp_printf("%s", "C: \"");
  _imp_printf("%S", _imp_strp(C));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    29    len = Length(A)

  LEN = *_imp_LENGTH((_imp_string255 *)&A);

  //    30    printstring("Setting CHARNO(""");

  _imp_printf("%s", "Setting CHARNO(\"");

  //    31    printstring(A)

  _imp_printf("%S", _imp_strp(A));

  //    32    printstring(""", ")

  _imp_printf("%s", "\", ");

  //    33    write(len,-1)

  _imp_WRITE(LEN, -1);

  //    34    printstring(") to '")

  _imp_printf("%s", ") to '");

  //    35    printsymbol(ch)

  _imp_PRINTSYMBOL(CH);

  //    36    printstring("'")

  _imp_printf("%s", "'");

  //    37    newline

  _imp_NEWLINE();

  //    38    Charno(A, Length(A)) = ch

  *_imp_CHARNO((_imp_string255 *)&A, *_imp_LENGTH((_imp_string255 *)&A)) = CH;

  //    39    Charno(B, Length(B)) = ch+1

  *_imp_CHARNO((_imp_string255 *)&B, *_imp_LENGTH((_imp_string255 *)&B)) = CH + 1;

  //    40    Charno(C, Length(C)) = ch+2

  *_imp_CHARNO((_imp_string255 *)&C, *_imp_LENGTH((_imp_string255 *)&C)) = CH + 2;

  //    41    printstring("A: """); printstring(A); printstring(""""); newline

  _imp_printf("%s", "A: \"");
  _imp_printf("%S", _imp_strp(A));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    42    printstring("B: """); printstring(B); printstring(""""); newline

  _imp_printf("%s", "B: \"");
  _imp_printf("%S", _imp_strp(B));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    43    printstring("C: """); printstring(C); printstring(""""); newline

  _imp_printf("%s", "C: \"");
  _imp_printf("%S", _imp_strp(C));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    44    %if A < B %start


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(A), (_imp_string *)_imp_strp(B)) >= 0) goto L_0009;

    /* clang-format on */

    //    45      printstring("A < B  OK"); newline

  _imp_printf("%s", "A < B  OK");
  _imp_NEWLINE();

  //    46    %else

  goto L_000A;
L_0009:;

//    47      printstring("A < B  FAILED!"); newline

  _imp_printf("%s", "A < B  FAILED!");
  _imp_NEWLINE();

  //    48    %finish

L_000A:;

//    49    %if A < B %and B < C %start


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(A), (_imp_string *)_imp_strp(B)) >= 0) goto L_000B;

  /* clang-format on */

  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(B), (_imp_string *)_imp_strp(C)) >= 0) goto L_000B;

    /* clang-format on */

    //    50      printstring("A < B %and B < C  OK"); newline

  _imp_printf("%s", "A < B %and B < C  OK");
  _imp_NEWLINE();

  //    51    %else

  goto L_000C;
L_000B:;

//    52      printstring("A < B %and B < C  FAILED!"); newline

  _imp_printf("%s", "A < B %and B < C  FAILED!");
  _imp_NEWLINE();

  //    53    %finish

L_000C:;

//    54    %if A < B < C %start

  {
    _imp_string6 *tmp;

    /* clang-format off */
if (_imp_strcmp/*a*/((_imp_string *)_imp_strp(A), (_imp_string *)(tmp=_imp_strp(B))) >= 0) goto L_000D;

    /* clang-format on */

    /* clang-format off */
if (_imp_strcmp/*b*/((_imp_string *)tmp, (_imp_string *)_imp_strp(C)) >= 0) goto L_000D;
}

  /* clang-format on */

  //    55      printstring("A < B < C  OK"); newline

  _imp_printf("%s", "A < B < C  OK");
  _imp_NEWLINE();

  //    56    %else

  goto L_000E;
L_000D:;

//    57      printstring("A < B < C  FAILED!"); newline

  _imp_printf("%s", "A < B < C  FAILED!");
  _imp_NEWLINE();

  //    58    %finish

L_000E:;

//    59    !Fail("string(6)/3") %unless A < B < C
//    60    A = "CAT"

  _imp_strcpy(&A, _imp_strp(_imp_str_literal("CAT")), /*max:*/ 6);

  //    61    B = "DOG"

  _imp_strcpy(&B, _imp_strp(_imp_str_literal("DOG")), /*max:*/ 6);

  //    62    C = A.B

  _imp_strassign(&C, 6, "%S%S", _imp_strp(A), _imp_strp(B));

  //    63    printstring("A: """); printstring(A); printstring(""""); newline

  _imp_printf("%s", "A: \"");
  _imp_printf("%S", _imp_strp(A));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    64    printstring("B: """); printstring(B); printstring(""""); newline

  _imp_printf("%s", "B: \"");
  _imp_printf("%S", _imp_strp(B));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    65    printstring("C: """); printstring(C); printstring(""""); newline

  _imp_printf("%s", "C: \"");
  _imp_printf("%S", _imp_strp(C));
  _imp_printf("%s", "\"");
  _imp_NEWLINE();

  //    66    Fail("string(6)/4") %unless "CATDOG" = C = A.B

  {
    _imp_string6 *tmp;

    /* clang-format off */
if (_imp_strcmp/*a*/((_imp_string *)_imp_strp(_imp_str_literal("CATDOG")), (_imp_string *)(tmp=_imp_strp(C))) != 0) goto L_0007;

    /* clang-format on */

    /* clang-format off */
if (_imp_strcmp/*b*/((_imp_string *)tmp, (_imp_string *)_imp_strp(*_imp_strp(_imp_strconcat("%S%S", _imp_strp(A), _imp_strp(B))))) == 0) goto L_000F;
}

/* clang-format on */
L_0007:;
  FAIL(_imp_str_literal("string(6)/4"));
L_000F:;

//    67  %end

  _imp_stop(0);
end_0:;
  _imp_begin_backtrace(main);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("ARGC = %d\n", argc);
  _imp_diagf("ARGV @[%p]\n", argv);
  for (int i = 0; i < argc; i++) {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", i, argv[i]);
  }
  if (argv[argc] == 0) {
    _imp_diagf("  ARGV(%d) = NULL\n", argc);
  } else {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", argc, argv[argc]);
  }
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_diagf("A = \"%.*s\"\n", A.cstr.len, A.cstr.s);
  _imp_diagf("B = \"%.*s\"\n", B.cstr.len, B.cstr.s);
  _imp_diagf("C = \"%.*s\"\n", C.cstr.len, C.cstr.s);
  _imp_bt_int(J);
  _imp_bt_int(LEN);
  _imp_bt_int(CH);
  _imp_end_backtrace();
  _imp_stop(0);
}

//    68  %endoffile