#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 fm(%integer a,b,c)

  struct FM {
    union {
      struct {
        int A;
        int B;
        int C;
      };
    };
  };

  //     3
  //     4    ! A similar construct in pass2.imp fails to detect the recordfields
  //     5    ! of the routine parameters.  It's not clear what the difference is.
  //     6
  //     7    %routine userec(%record (fm) %name dest, %record (fm) src)

  void USEREC(struct FM * DEST, struct FM SRC) {
    _imp_enter(USEREC);

    //     8      dest_b = src_a

    DEST->B = SRC.A;

    //     9    %end

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

  //    10
  //    11  %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_end_backtrace();
  _imp_stop(0);
}