#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    %integer sheila, sheila2

  int SHEILA = 0xFEFEFEFE;
  int SHEILA2 = 0xFEFEFEFE;

  //     3    %integername bert, bert2

  int *BERT = (void *)0xFEFEFEFEFEFEFEFELL;
  int *BERT2 = (void *)0xFEFEFEFEFEFEFEFELL;

  //     4
  //     5    %integermap fred

  int *FRED(void) {
    _imp_enter(FRED);

    //     6      %result == sheila2

    _imp_result(&SHEILA2);

    //     7    %end

    _imp_begin_backtrace(FRED);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of FRED */

  //     8
  //     9    %integermap fred2(%integer i)

  int *FRED2(int I) {
    _imp_enter(FRED2);

    //    10      %result == bert

    _imp_result(BERT);

    //    11    %end

    _imp_begin_backtrace(FRED2);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("I = %d\n", I);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of FRED2 */

  // ?I unused
  //    12
  //    13    bert == sheila

  BERT = (int *)&SHEILA;
  //    14    bert == fred

  BERT = (int *)FRED();
  //    15    bert == fred2(55)

  BERT = (int *)FRED2(55);
  //    16    bert == bert

  BERT = (int *)BERT;
  //    17    bert = 42

  *BERT = 42;

  //    18
  //    19    sheila = 21

  SHEILA = 21;

  //    20    sheila = bert

  SHEILA = *BERT;

  //    21    sheila = fred

  SHEILA = *FRED();

  //    22    sheila = fred2(44)

  SHEILA = *FRED2(44);

  //    23
  //    24    fred = 63

  *FRED() = 63;

  //    25    fred2(88) = 63

  *FRED2(88) = 63;

  //    26
  //    27    %if sheila2 = sheila %then %stop

  if ((SHEILA2) != (SHEILA)) goto L_0001;
  _imp_stop(0);
L_0001:;

//    28    %if fred = sheila %then %stop

  if ((*FRED()) != (SHEILA)) goto L_0002;
  _imp_stop(0);
L_0002:;

//    29    %if fred2(99) = sheila %then %stop

  if ((*FRED2(99)) != (SHEILA)) goto L_0003;
  _imp_stop(0);
L_0003:;

//    30    %if bert = sheila %then %stop

  if ((*BERT) != (SHEILA)) goto L_0004;
  _imp_stop(0);
L_0004:;

//    31    %if fred == bert %then %stop


  /* clang-format off */
if (_imp_ADDR((void *)(FRED())) != _imp_ADDR((void *)(BERT))) goto L_0005;

  /* clang-format on */
  _imp_stop(0);
L_0005:;

//    32    %if fred2(11) == bert %then %stop


  /* clang-format off */
if (_imp_ADDR((void *)(FRED2(11))) != _imp_ADDR((void *)(BERT))) goto L_0006;

  /* clang-format on */
  _imp_stop(0);
L_0006:;

//    33    %if bert == bert2 %then %stop


  /* clang-format off */
if (_imp_ADDR((void *)(BERT)) != _imp_ADDR((void *)(BERT2))) goto L_0007;

  /* clang-format on */
  _imp_stop(0);
L_0007:;

//    34
//    35  %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_bt_int(SHEILA);
  _imp_bt_int(SHEILA2);
  if (BERT == 0) {
    _imp_diagf("BERT @[<NIL>]\n");
  } else if (_imp_unassigned(BERT)) {
    _imp_diagf("BERT @[<UNASSIGNED POINTER>]\n");
  } else {
    if (_imp_unassigned(*BERT)) {
      _imp_diagf("BERT @[%p] = <UNASSIGNED INTEGER>\n", BERT);
    } else {
      _imp_diagf("BERT @[%p] = %d\n", BERT, *(int *)BERT);
    }
  }
  if (BERT2 == 0) {
    _imp_diagf("BERT2 @[<NIL>]\n");
  } else if (_imp_unassigned(BERT2)) {
    _imp_diagf("BERT2 @[<UNASSIGNED POINTER>]\n");
  } else {
    if (_imp_unassigned(*BERT2)) {
      _imp_diagf("BERT2 @[%p] = <UNASSIGNED INTEGER>\n", BERT2);
    } else {
      _imp_diagf("BERT2 @[%p] = %d\n", BERT2, *(int *)BERT2);
    }
  }
  _imp_end_backtrace();
  _imp_stop(0);
}