#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  %OWNINTEGER LABELNO=0

  static int LABELNO = 0;

  //     3  %CONSTINTEGER MAXLABELS=10
  //     4  %RECORDFORMAT F(%BYTE B,%STRING(255)LAB)

  struct F {
    union {
      struct {
        unsigned char B;
        _imp_string255 LAB;
      };
    };
  };

  //     5  %OWNSTRING(15)%ARRAY LBLNAME(0:MAXLABELS)

  static _imp_string15 LBLNAME[11];
  _imp_create_constdv(static, _imp_string, LBLNAME, 0, 10);

  //     6  %OWNRECORD(F)%ARRAY LBL(0:MAXLABELS)

  static struct F LBL[11];
  _imp_create_constdv(static, struct F, LBL, 0, 10);

  //     7
  //     8  %RECORD(F)%MAP LABEL(%STRING(15) L)

  struct F *LABEL(_imp_string15 L) {
    _imp_enter(LABEL);

    //     9  %INTEGER I

    int I = 0xFEFEFEFE;

    //    10   %IF LABELNO>0 %THENSTART

    if ((LABELNO) <= (0)) goto L_0001;

      //    11    %FOR I=1,1,LABELNO %CYCLE

    // %for I = 1, 1, LABELNO %cycle
    {
      int _final = LABELNO;
      if (invalid_for_loop(1, 1, _final)) _imp_signal(5, 1, 0, "Illegal cycle I = 1, 1, LABELNO");
      I = 0;
    L_0002:
      if (I == _final) goto L_0003;
      I += 1;

      //    12     %RESULT==LBL(I) %IF LBLNAME(I)=L


      /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(LBLNAME[I]), (_imp_string *)_imp_strp(L)) != 0) goto L_0005;

      /* clang-format on */
      _imp_result(&LBL[I]);
    L_0005:;

    //    13    %REPEAT

      goto L_0002;
    }

  L_0003:;

  //    14   %FINISH

  L_0001:;

  //    15  !  INSERT NEW LABEL-NAME.
  //    16   LABELNO=LABELNO+1

    LABELNO = LABELNO + 1;

    //    17   %IF LABELNO>MAXLABELS %THENSTART

    if ((LABELNO) <= (10)) goto L_0006;

      //    18    PRINT STRING(L." MAKES TOO MANY LABELS."); NEWLINE

    _imp_printf("%S%s", _imp_strp(L), " MAKES TOO MANY LABELS.");
    _imp_NEWLINE();

    //    19    %FOR I=1,1,MAXLABELS %CYCLE

    // %for I = 1, 1, 10 %cycle
    {
      I = 0;
    L_0007:
      if (I == 10) goto L_0008;
      I += 1;

      //    20     PRINT STRING(LBLNAME(I))

      _imp_printf("%S", _imp_strp(LBLNAME[I]));

      //    21     NEWLINE

      _imp_NEWLINE();

      //    22    %REPEAT

      goto L_0007;
    }

  L_0008:;

  //    23    %MONITOR

    _imp_monitor_loc(__LINE__, __FILE__, __PRETTY_FUNCTION__);

    //    24    %STOP

    _imp_stop(0);

    //    25   %FINISH    ;!  DEALING WITH TOO MANY LABELS.

  L_0006:;

  //    26   LBLNAME(LABELNO)=L

    _imp_strcpy(&LBLNAME[LABELNO], _imp_strp(L), /*max:*/ 15);

    //    27   LBL(LABELNO)_LAB=""

    _imp_strcpy(&LBL[LABELNO].LAB, _imp_strp(_imp_str_literal("")), /*max:*/ 255);

    //    28   %RESULT==LBL(LABELNO)

    _imp_result(&LBL[LABELNO]);

    //    29  %END

    _imp_begin_backtrace(LABEL);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("L = \"%.*s\"\n", L.cstr.len, L.cstr.s);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_bt_int(I);
    _imp_end_backtrace();
  } /* end of LABEL */

  //    30
  //    31
  //    32
  //    33    LABEL("T")_LAB="TESTING USE OF LABEL"

  _imp_strcpy(&LABEL(_imp_str_literal_size(15, "T"))->LAB, _imp_strp(_imp_str_literal("TESTING USE OF LABEL")),
              /*max:*/ 255);

  //    34    PRINT STRING(LABEL("T")_LAB); NEWLINE

  _imp_printf("%S", _imp_strp(LABEL(_imp_str_literal_size(15, "T"))->LAB));
  _imp_NEWLINE();

  //    35    LABEL("T")_LAB="THIS SHOULD MODIFY ".LABEL("T")_LAB." AS I TRIED TO DO YESTERDAY"

  _imp_strassign(&LABEL(_imp_str_literal_size(15, "T"))->LAB, 255, "%s%S%s", "THIS SHOULD MODIFY ",
                 _imp_strp(LABEL(_imp_str_literal_size(15, "T"))->LAB), " AS I TRIED TO DO YESTERDAY");

  //    36    PRINT STRING(LABEL("T")_LAB); NEWLINE

  _imp_printf("%S", _imp_strp(LABEL(_imp_str_literal_size(15, "T"))->LAB));
  _imp_NEWLINE();

  //    37  %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(LABELNO);
  _imp_end_backtrace();
  _imp_stop(0);
}