#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      %recordformat fubar(%integer a,b, (%bytearray c(1:10) %or %string(9) s), %short d)

  struct FUBAR {
    union {
      struct {
        int A;
        int B;
        union {
          struct {
            unsigned char C[10];
          };
          struct {
            _imp_string9 S;
          };
        };

        short int D;
      };
    };
  };

  //     3      %record(fubar) snafu = 0

  struct FUBAR SNAFU;
  SNAFU = (struct FUBAR){0};

  //     4      %integer i,l,u

  int I = 0xFEFEFEFE;
  int L = 0xFEFEFEFE;
  int U = 0xFEFEFEFE;

  //     5
  //     6      snafu_s = "fubar"

  _imp_strcpy(&SNAFU.S, _imp_strp(_imp_str_literal("fubar")), /*max:*/ 255);

  //     7
  //     8      { Using the SIZEOF and TYPEOF+typename functions for each record field }
  //     9      printstring("SIZEOF(snafu)=");write(sizeof(snafu),0);newline

  _imp_printf("%s", "SIZEOF(snafu)=");
  _imp_WRITE(_imp_SIZEOF(SNAFU), 0);
  _imp_NEWLINE();

  //    10      printstring("TYPEOF(snafu)=");printstring(typename(typeof(snafu)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(4)));
  _imp_NEWLINES(2);

  //    11
  //    12      printstring("SIZEOF(snafu_a)=");write(sizeof(snafu_a),0);newline

  _imp_printf("%s", "SIZEOF(snafu_a)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    13      printstring("TYPEOF(snafu_a)=");printstring(typename(typeof(snafu_a)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_a)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    14
  //    15      printstring("SIZEOF(snafu_b)=");write(sizeof(snafu_b),0);newline

  _imp_printf("%s", "SIZEOF(snafu_b)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    16      printstring("TYPEOF(snafu_b)=");printstring(typename(typeof(snafu_b)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_b)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    17
  //    18      printstring("SIZEOF(snafu_c)=");write(sizeof(snafu_c),0);newline

  _imp_printf("%s", "SIZEOF(snafu_c)=");
  _imp_WRITE(_imp_SIZEOF(SNAFU.C), 0);
  _imp_NEWLINE();

  //    19      printstring("TYPEOF(snafu_c)=");printstring(typename(typeof(snafu_c)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_c)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(33)));
  _imp_NEWLINES(2);

  //    20
  //    21      printstring("SIZEOF(snafu_c(1))=");write(sizeof(snafu_c(1)),0);newline

  _imp_printf("%s", "SIZEOF(snafu_c(1))=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    22      printstring("TYPEOF(snafu_c(1))=");printstring(typename(typeof(snafu_c(1))));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_c(1))=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    23
  //    24      printstring("SIZEOF(snafu_s)=");write(sizeof(snafu_s),0);newline

  _imp_printf("%s", "SIZEOF(snafu_s)=");
  _imp_WRITE(256, 0);
  _imp_NEWLINE();

  //    25      printstring("TYPEOF(snafu_s)=");printstring(typename(typeof(snafu_s)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_s)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(3)));
  _imp_NEWLINES(2);

  //    26
  //    27      printstring("SIZEOF(snafu_d)=");write(sizeof(snafu_d),0);newline

  _imp_printf("%s", "SIZEOF(snafu_d)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    28      printstring("TYPEOF(snafu_d)=");printstring(typename(typeof(snafu_d)));newlines(2)

  _imp_printf("%s", "TYPEOF(snafu_d)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    29
  //    30      { Accessing via %name }
  //    31      %record(fubar)%name bilbo == snafu

  struct FUBAR *BILBO = (void *)0xFEFEFEFEFEFEFEFELL;
  BILBO = (struct FUBAR *)&SNAFU;
  //    32      { Using the SIZEOF and TYPEOF+typename functions for each record field }
  //    33      printstring("SIZEOF(bilbo)=");write(sizeof(bilbo),0);newline

  _imp_printf("%s", "SIZEOF(bilbo)=");
  _imp_WRITE(_imp_SIZEOF(BILBO), 0);
  _imp_NEWLINE();

  //    34      printstring("TYPEOF(bilbo)=");printstring(typename(typeof(bilbo)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(20)));
  _imp_NEWLINES(2);

  //    35
  //    36      printstring("SIZEOF(bilbo_a)=");write(sizeof(bilbo_a),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_a)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    37      printstring("TYPEOF(bilbo_a)=");printstring(typename(typeof(bilbo_a)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_a)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    38
  //    39      printstring("SIZEOF(bilbo_b)=");write(sizeof(bilbo_b),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_b)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    40      printstring("TYPEOF(bilbo_b)=");printstring(typename(typeof(bilbo_b)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_b)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    41
  //    42      printstring("SIZEOF(bilbo_c)=");write(sizeof(bilbo_c),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_c)=");
  _imp_WRITE(_imp_SIZEOF(BILBO->C), 0);
  _imp_NEWLINE();

  //    43      printstring("TYPEOF(bilbo_c)=");printstring(typename(typeof(bilbo_c)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_c)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(33)));
  _imp_NEWLINES(2);

  //    44
  //    45      printstring("SIZEOF(bilbo_c(1))=");write(sizeof(bilbo_c(1)),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_c(1))=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    46      printstring("TYPEOF(bilbo_c(1))=");printstring(typename(typeof(bilbo_c(1))));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_c(1))=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    47
  //    48      printstring("SIZEOF(bilbo_s)=");write(sizeof(bilbo_s),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_s)=");
  _imp_WRITE(256, 0);
  _imp_NEWLINE();

  //    49      printstring("TYPEOF(bilbo_s)=");printstring(typename(typeof(bilbo_s)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_s)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(3)));
  _imp_NEWLINES(2);

  //    50
  //    51      printstring("SIZEOF(bilbo_d)=");write(sizeof(bilbo_d),0);newline

  _imp_printf("%s", "SIZEOF(bilbo_d)=");
  _imp_WRITE(4, 0);
  _imp_NEWLINE();

  //    52      printstring("TYPEOF(bilbo_d)=");printstring(typename(typeof(bilbo_d)));newlines(2)

  _imp_printf("%s", "TYPEOF(bilbo_d)=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(1)));
  _imp_NEWLINES(2);

  //    53
  //    54      { Accessing using the Dope Vector }
  //    55      printstring("Dimensions of bilbo_c=");write(array dimensions(bilbo_c),0);newlines(2)

  _imp_printf("%s", "Dimensions of bilbo_c=");
  _imp_WRITE(_imp_ARRAYDIMENSIONS(((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)})), 0);
  _imp_NEWLINES(2);

  //    56      printstring("Array Entry Size of bilbo_c=");write(array entry size(bilbo_c),0);newline

  _imp_printf("%s", "Array Entry Size of bilbo_c=");
  _imp_WRITE(_imp_ARRAYENTRYSIZE(((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)})), 0);
  _imp_NEWLINE();

  //    57      printstring("Array Entry Type of bilbo_c=");printstring(type name(array entry type(bilbo_c)));newline

  _imp_printf("%s", "Array Entry Type of bilbo_c=");
  _imp_printf("%S", _imp_strp(_imp_TYPENAME(_imp_ARRAYENTRYTYPE(
                        ((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)})))));
  _imp_NEWLINE();

  //    58      %for i = 1,1,array dimensions(bilbo_c) %cycle

  // %for I = 1, 1, _imp_ARRAYDIMENSIONS(((_imp_name){.address=&BILBO->C, .typeinfo=1, .sizeinfo=sizeof(BILBO->C)})) %cycle
  {
    int _final = _imp_ARRAYDIMENSIONS(((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)}));
    if (invalid_for_loop(1, 1, _final))
      _imp_signal(5, 1, 0,
                  "Illegal cycle I = 1, 1, _imp_ARRAYDIMENSIONS(((_imp_name){.address=&BILBO->C, .typeinfo=1, "
                  ".sizeinfo=sizeof(BILBO->C)}))");
    I = 0;
  L_0001:
    if (I == _final) goto L_0002;
    I += 1;

    //    59          l = array lower bound( bilbo_c, i )

    L = _imp_ARRAYLOWERBOUND(((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)}), I);

    //    60          u = array upper bound( bilbo_c, i )

    U = _imp_ARRAYUPPERBOUND(((_imp_name){.address = &BILBO->C, .typeinfo = 1, .sizeinfo = sizeof(BILBO->C)}), I);

    //    61          printstring("Dimension(");write(i,0);printstring(")=(");

    _imp_printf("%s", "Dimension(");
    _imp_WRITE(I, 0);
    _imp_printf("%s", ")=(");

    //    62          write(l,0);printstring(":");write(u,0);printstring(")");newline

    _imp_WRITE(L, 0);
    _imp_printf("%s", ":");
    _imp_WRITE(U, 0);
    _imp_printf("%s", ")");
    _imp_NEWLINE();

    //    63      %repeat

    goto L_0001;
  }

L_0002:;

//    64
//    65  %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("SNAFU = \"<RECORD>\"\n");
  _imp_bt_int(I);
  _imp_bt_int(L);
  _imp_bt_int(U);
  _imp_diagf("BILBO @[%p] = <RECORD>\n", BILBO);
  _imp_end_backtrace();
  _imp_stop(0);
}