#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  %begin

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

  //     2    %string (1) snl; snl = "

  _imp_string1 SNL;
  _imp_strcpy(&SNL, _imp_strp(_imp_str_literal("\n")), /*max:*/ 1);

  //     3+ "
  //     4    %begin

  { /*%begin*/
    _imp_enter(_imp_block_1000);

    //     5      %string (63) test1, test2

    _imp_string63 TEST1;
    _imp_string63 TEST2;

    //     6      test1 = "A string"

    _imp_strcpy(&TEST1, _imp_strp(_imp_str_literal("A string")), /*max:*/ 63);

    //     7      test2 = "another"

    _imp_strcpy(&TEST2, _imp_strp(_imp_str_literal("another")), /*max:*/ 63);

    //     8      print string(test1." + ".test2.snl)

    _imp_printf("%S%s%S%S", _imp_strp(TEST1), " + ", _imp_strp(TEST2), _imp_strp(SNL));

    //     9    %end

    goto end_1000;
  end_1000:;
    _imp_begin_backtrace(_imp_block_1000);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_diagf("TEST1 = \"%.*s\"\n", TEST1.cstr.len, TEST1.cstr.s);
    _imp_diagf("TEST2 = \"%.*s\"\n", TEST2.cstr.len, TEST2.cstr.s);
    _imp_end_backtrace();
  }

  //    10    %begin

  { /*%begin*/
    _imp_enter(_imp_block_1001);

    //    11      %string(32) test2; test2 = "another"

    _imp_string32 TEST2;
    _imp_strcpy(&TEST2, _imp_strp(_imp_str_literal("another")), /*max:*/ 32);

    //    12      %recordformat strec(%string(63) test1)

    struct STREC {
      union {
        struct {
          _imp_string63 TEST1;
        };
      };
    };

    //    13      %record (strec) x

    struct STREC X;

    //    14      x_test1 = "A string"

    X.= _imp_str_literal("A string");

    //    15      print string(x_test1." + ".test2.snl)

    _imp_printf("%S%s%S%S", _imp_strp(X.), " + ", _imp_strp(TEST2), _imp_strp(SNL));

    //    16    %end

    goto end_1001;
  end_1001:;
    _imp_begin_backtrace(_imp_block_1001);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_diagf("TEST2 = \"%.*s\"\n", TEST2.cstr.len, TEST2.cstr.s);
    _imp_diagf("X = \"<RECORD>\"\n");
    _imp_end_backtrace();
  }

  //    17    %begin

  { /*%begin*/
    _imp_enter(_imp_block_1002);

    //    18      %string(32) %map test2

    _imp_string32 *TEST2(void) {
      _imp_enter(TEST2);

      //    19        %ownstring(32) s = "another"

      static _imp_string32 S = _imp_str_init("another");

      //    20        %result == s

      _imp_result(_imp_strp(S));

      //    21      %end

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

    //    22      %string(63)%array test1(0:9)

    _imp_string63 TEST1[10];
    _imp_create_constdv(, _imp_string, TEST1, 0, 9);

    //    23      test1(2) = "A string"

    _imp_strcpy(&TEST1[2], _imp_strp(_imp_str_literal("A string")), /*max:*/ 63);

    //    24      print string(test1(2)." + ".test2.snl)

    _imp_printf("%S%s%S%S", _imp_strp(TEST1[2]), " + ", /*map*/ TEST2(), _imp_strp(SNL));

    //    25    %end

    goto end_1002;
  end_1002:;
    _imp_begin_backtrace(_imp_block_1002);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_end_backtrace();
  }

  //    26    %begin

  { /*%begin*/
    _imp_enter(_imp_block_1003);

    //    27      %string(32) %map test2

    _imp_string32 *TEST2(void) {
      _imp_enter(TEST2);

      //    28        %ownstring(32) s = "another"

      static _imp_string32 S = _imp_str_init("another");

      //    29        %result == s

      _imp_result(_imp_strp(S));

      //    30      %end

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

    //    31      %recordformat strec(%string(63) test1)

    struct STREC {
      union {
        struct {
          _imp_string63 TEST1;
        };
      };
    };

    //    32      %record (strec) x

    struct STREC X;

    //    33      x_test1 = "A string"

    X.= _imp_str_literal("A string");

    //    34      print string(x_test1." + ".test2.snl)

    _imp_printf("%S%s%S%S", _imp_strp(X.), " + ", /*map*/ TEST2(), _imp_strp(SNL));

    //    35    %end

    goto end_1003;
  end_1003:;
    _imp_begin_backtrace(_imp_block_1003);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_diagf("X = \"<RECORD>\"\n");
    _imp_end_backtrace();
  }

  //    36    %begin

  { /*%begin*/
    _imp_enter(_imp_block_1004);

    //    37      %string (63) test1, test2a

    _imp_string63 TEST1;
    _imp_string63 TEST2A;

    //    38      %string (*) %name test2

    _imp_string *TEST2 = (void *)0xFEFEFEFEFEFEFEFELL;

    //    39      test1 = "A string"

    _imp_strcpy(&TEST1, _imp_strp(_imp_str_literal("A string")), /*max:*/ 63);

    //    40      test2a = "another"

    _imp_strcpy(&TEST2A, _imp_strp(_imp_str_literal("another")), /*max:*/ 63);

    //    41      test2 == test2a

    TEST2 = (_imp_string *)_imp_strp(TEST2A);
    //    42      print string(test1." + ".test2.snl)

    _imp_printf("%S%s%S%S", _imp_strp(TEST1), " + ", TEST2, _imp_strp(SNL));

    //    43    %end

    goto end_1004;
  end_1004:;
    _imp_begin_backtrace(_imp_block_1004);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_diagf("TEST1 = \"%.*s\"\n", TEST1.cstr.len, TEST1.cstr.s);
    _imp_diagf("TEST2A = \"%.*s\"\n", TEST2A.cstr.len, TEST2A.cstr.s);
    _imp_diagf("TEST2 @[%p] = \"%.*s\"\n", TEST2, TEST2->cstr.len, TEST2->cstr.s);
    _imp_end_backtrace();
  }

  //    44  %endofprogram

  _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("SNL = \"%.*s\"\n", SNL.cstr.len, SNL.cstr.s);
  _imp_end_backtrace();
  _imp_stop(0);
}