#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1
//     2
//     3  %externalintegerfn get YNX

int GETYNX(void) {
  _imp_enter(GETYNX);

  //     4    %on %event 9 %start

  if (!_imp_on_event(9)) goto L_0001;

    //     5      %result = 'X'

  _imp_result(88);

  //     6    %finish

L_0001:;

//     7    %integer c

  int C = 0xFEFEFEFE;

  //     8    %cycle

L_0002:;

//     9      !read symbol(c)  ;! read symbol currently not working due to %name parameters not being handled yet.
//    10      c = next symbol; skip symbol

  C = _imp_NEXTSYMBOL();
  _imp_SKIPSYMBOL();

  //    11      %signal 7,1,0 %if c = '?'

  if ((C) != (63)) goto L_0003;
  _imp_signal(7, 1, 0, "");
L_0003:;

//    12      c = c - 'a' + 'A' %if 'a' <= c <= 'z'

  {
    int tmp;
    if ((97) > (tmp = C)) goto L_0004;
    // Double-sided test on C (Part II)
    if (tmp > (122)) goto L_0004;
  }
  C = C - 97 + 65;
L_0004:;

//    13      %result = c %if c = 'Y' %or c = 'N'

  if ((C) == (89)) goto L_0005;
  if ((C) != (78)) goto L_0006;
L_0005:;
  _imp_result(C);
L_0006:;

//    14    %repeat

  goto L_0002;

  //    15  %end

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

//    16
//    17  ! A complete program.  Should run.
//    18  %begin

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

  //    19    %integer Answer

  int ANSWER = 0xFEFEFEFE;

  //    20    %on %event 7 %start

  if (!_imp_on_event(7)) goto L_0007;

    //    21      print string("What indeed?"); newline

  _imp_printf("%s", "What indeed?");
  _imp_NEWLINE();

  //    22      ! drop through and restart...
  //    23    %finish

L_0007:;

//    24
//    25    prompt("Y or N: ");

  _imp_PROMPT(_imp_str_literal_size(31, "Y or N: "));

  //    26    print string("Starting"); newline

  _imp_printf("%s", "Starting");
  _imp_NEWLINE();

  //    27    %cycle

L_0008:;

//    28      Answer = get YNX

  ANSWER = GETYNX();

  //    29      %if Answer = 'Y' %start

  if ((ANSWER) != (89)) goto L_0009;

    //    30        print string("Yes!"); newline

  _imp_printf("%s", "Yes!");
  _imp_NEWLINE();

  //    31      %finish %else %if Answer = 'N' %start

  goto L_000A;
L_0009:;
  if ((ANSWER) != (78)) goto L_000B;

    //    32        print string("No?"); newline

  _imp_printf("%s", "No?");
  _imp_NEWLINE();

  //    33      %finish %else %if Answer = 'X' %start

  goto L_000A;
L_000B:;
  if ((ANSWER) != (88)) goto L_000C;

    //    34        print string("

  _imp_printf("%s", "\nExiting on trapped END OF FILE event!.");

  //    35+ Exiting on trapped END OF FILE event!."); newlines(2)

  _imp_NEWLINES(2);

  //    36        %stop

  _imp_stop(0);

  //    37      %finish %else %start

L_000C:;

//    38        %monitor %and %stop

  _imp_monitor_loc(__LINE__, __FILE__, __PRETTY_FUNCTION__);
  _imp_stop(0);

  //    39      %finish

L_000A:;

//    40    %repeat

  goto L_0008;

  //    41  %endofprogram

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(ANSWER);
  _imp_end_backtrace();
  _imp_stop(0);
}