#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  %begin

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

  //     2
  //     3    %routine printarray(%integerarray(2)%name A, %integer a0low, a0high, a1low, a1high)

  void PRINTARRAY(int *A, int A0LOW, int A0HIGH, int A1LOW, int A1HIGH) {
    //     4      %integer A0, A1

    int A0;
    int A1;

    //     5      ! Imp needs some perm code to determine upper and lower bounds of arrays
    //     6      %for A0 = a0low, 1, a0high %cycle

    // %for A0 = A0LOW, 1, A0HIGH %cycle
    {
      int _initial = A0LOW, _final = A0HIGH;
      if (invalid_for_loop(_initial, 1, _final)) _imp_signal(5, 1, 0, "Illegal cycle A0 = A0LOW, 1, A0HIGH");
      A0 = _initial - (1);
    L_0001:
      if (A0 == _final) goto L_0002;
      A0 += 1;

      //     7        %for A1 = a1low, 1, a1high %cycle

      // %for A1 = A1LOW, 1, A1HIGH %cycle
      {
        int _initial = A1LOW, _final = A1HIGH;
        if (invalid_for_loop(_initial, 1, _final)) _imp_signal(5, 1, 0, "Illegal cycle A1 = A1LOW, 1, A1HIGH");
        A1 = _initial - (1);
      L_0004:
        if (A1 == _final) goto L_0005;
        A1 += 1;

        //     8          write(A(A0, A1), 3); print string(", ")

        _imp_WRITE([/*BUG*/ 0], 3);
        _imp_PRINTSTRING(_imp_str_literal(", "));

        //     9        %repeat

        goto L_0004;
      }

    L_0005:;

    //    10        newline

      _imp_NEWLINE();

      //    11      %repeat

      goto L_0001;
    }

  L_0002:;

  //    12      newline

    _imp_NEWLINE();

    //    13    %end

    return;
  } /* end of PRINTARRAY */

  //    14
  //    15    %integername element

  int *ELEMENT;

  //    16    %integer i

  int I;

  //    17
  //    18    ! imp77 doesn't allow initialised multi-dimensional arrays.  (Imp80 would!)
  //    19    ! So I'll initialise these explicitly:
  //    20    %integerarray fred(0:1, 0:2)       { simple case }

  int FRED[2][3];

  //    21    %integerarray jim(11:13, 100:103)  { non-zero lower bounds }

  int JIM[3][4];

  //    22    element == fred(0,0); element = i %and element == element++1 %for i = 0, 1, 5

  ELEMENT = (int *)/*?*/ &FRED[0][0];
  // %for I = 0, 1, 5 %cycle
  {
    I = -1;
  L_0007:
    if (I == 5) goto L_0008;
    I += 1;
    *ELEMENT = I;
    ELEMENT = (int *)ELEMENT + 1;
    goto L_0007;
  }

L_0008:;

//    23    element == jim(11,100); element = i %and element == element++1 %for i = 0, 1, 11

  ELEMENT = (int *)/*?*/ &JIM[0][0];
  // %for I = 0, 1, 11 %cycle
  {
    I = -1;
  L_0009:
    if (I == 11) goto L_000A;
    I += 1;
    *ELEMENT = I;
    ELEMENT = (int *)ELEMENT + 1;
    goto L_0009;
  }

L_000A:;

//    24
//    25    printarray(fred, 0,1, 0,2)

  PRINTARRAY(FRED, 0, 1, 0, 2);

  //    26    printarray(jim, 11,13, 100,103)

  PRINTARRAY(JIM, 11, 13, 100, 103);

  //    27
  //    28  %endofprogram

  _imp_stop(0);
  return 0;
end_0:;
}