#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    ! see if we can remove redundant *& in assignment of 'that == buff(0)'
  //     3    %recordformat bfm(%integer i, j)

  struct BFM {
    union {
      struct {
        int I;
        int J;
      };
    };
  };

  //     4
  //     5    ! BUG: currently the '*' is missing from the map declaration
  //     6    %record(bfm)%map   buff(%integer addr)

  struct BFM *BUFF(int ADDR) {
    _imp_enter(BUFF);

    //     7    %record(bfm)  this

    struct BFM THIS;

    //     8      %result == this

    _imp_result(&THIS);

    //     9    %end;

    _imp_begin_backtrace(BUFF);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("ADDR = %d\n", ADDR);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_diagf("THIS = \"<RECORD>\"\n");
    _imp_end_backtrace();
  } /* end of BUFF */

  // ?ADDR unused
  //    10         ;          !  of 'buff'
  //    10
  //    11    %record (bfm) %name  that

  struct BFM *THAT = (void *)0xFEFEFEFEFEFEFEFELL;

  //    12
  //    13    that == buff(0)

  THAT = (struct BFM *)BUFF(0);
  //    14
  //    15  %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("THAT @[%p] = <RECORD>\n", THAT);
  _imp_end_backtrace();
  _imp_stop(0);
}