#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  %STRING(5) %ARRAY a(1:10)

  _imp_string5 A[10];
  _imp_create_constdv(, _imp_string, A, 1, 10);

  //     3  %STRING(5) %NAME ap

  _imp_string5 *AP = (void *)0xFEFEFEFEFEFEFEFELL;

  //     4  %INTEGER i

  int I = 0xFEFEFEFE;

  //     5  %ROUTINE conc(%STRING(5) %NAME x)

  void CONC(_imp_string5 * X) {
    _imp_enter(CONC);

    //     6        x=x."a"

    *X = **_imp_strp(_imp_strconcat("%S%s", X, "a"));  // UNHANDLED STR ASSVAL DEST

    //     7  %END

    _imp_return;
    _imp_begin_backtrace(CONC);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("X = \"%.*s\"\n", X->cstr.len, X->cstr.s);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of CONC */

  //     8        a(i)=tostring(i+'0') %FOR i=1,1,1

  // %for I = 1, 1, 1 %cycle
  {
    if (invalid_for_loop(1, 1, 1)) _imp_signal(5, 1, 0, "Illegal cycle I = 1, 1, 1");
    I = 0;
  L_0001:
    if (I == 1) goto L_0002;
    I += 1;
    _imp_strcpy(&A[I - 1], _imp_strp(_imp_TOSTRING(I + 48)), /*max:*/ 5);
    goto L_0001;
  }

L_0002:;

//     9        printstring("before conc "); printstring(a(1)); newline

  _imp_printf("%s", "before conc ");
  _imp_printf("%S", _imp_strp(A[0]));
  _imp_NEWLINE();

  //    10        conc(a(i)) %FOR i=1,1,1

  // %for I = 1, 1, 1 %cycle
  {
    if (invalid_for_loop(1, 1, 1)) _imp_signal(5, 1, 0, "Illegal cycle I = 1, 1, 1");
    I = 0;
  L_0004:
    if (I == 1) goto L_0005;
    I += 1;
    CONC((_imp_string5 *)&A[I - 1]);
    goto L_0004;
  }

L_0005:;

//    11        printstring("afeter conc "); printstring(a(1)); newline

  _imp_printf("%s", "afeter conc ");
  _imp_printf("%S", _imp_strp(A[0]));
  _imp_NEWLINE();

  //    12        %FOR i=1,1,1 %CYCLE

  // %for I = 1, 1, 1 %cycle
  {
    if (invalid_for_loop(1, 1, 1)) _imp_signal(5, 1, 0, "Illegal cycle I = 1, 1, 1");
    I = 0;
  L_0006:
    if (I == 1) goto L_0007;
    I += 1;

    //    13           ap==a(i)

    AP = (_imp_string5 *)_imp_strp(A[I - 1]);
    //    14           ap=ap."b"

    *AP = **_imp_strp(_imp_strconcat("%S%s", AP, "b"));  // UNHANDLED STR ASSVAL DEST

    //    15         printstring("after 2nd conc "); printstring(a(1)); newline

    _imp_printf("%s", "after 2nd conc ");
    _imp_printf("%S", _imp_strp(A[0]));
    _imp_NEWLINE();

    //    16           printstring(a(i)) %UNLESS a(i)=tostring(i+'0')."ab"


    /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(A[I - 1]), (_imp_string *)_imp_strp(*_imp_strp(_imp_strconcat("%S%s", _imp_strp(_imp_TOSTRING(I + 48)), "ab")))) == 0) goto L_0008;

    /* clang-format on */
    _imp_printf("%S", _imp_strp(A[I - 1]));
  L_0008:;

  //    17        %REPEAT

    goto L_0006;
  }

L_0007:;

//    18  %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("AP @[%p] = \"%.*s\"\n", AP, AP->cstr.len, AP->cstr.s);
  _imp_bt_int(I);
  _imp_end_backtrace();
  _imp_stop(0);
}