#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      %integer n

  int N = 0xFEFEFEFE;

  //     3      %record %format flightfm(%integer from,to,cost)

  struct FLIGHTFM {
    union {
      struct {
        int FROM;
        int TO;
        int COST;
      };
    };
  };

  //     4
  //     5      %integer %function min(%integer a, b)

  int MIN(int A, int B) {
    _imp_enter(MIN);

    //     6          %result = a %if (a < b)

    if ((A) >= (B)) goto L_0001;
    _imp_result(A);
  L_0001:;

  //     7          %result = b

    _imp_result(B);

    //     8      %end

    _imp_begin_backtrace(MIN);
    _imp_diagf("\n\nArguments:\n\n");
    _imp_diagf("A = %d\n", A);
    _imp_diagf("B = %d\n", B);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of MIN */

  //     9
  //    10      select input(1)

  _imp_SELECTINPUT(1);

  //    11      read(n)

  _imp_READ(((_imp_name){.address = &N, .typeinfo = 1, .sizeinfo = sizeof(N)}));

  //    12
  //    13      %begin

  { /*%begin*/
    _imp_enter(_imp_block_1000);

    //    14          %record(flightfm) %array flights(1:n)

    struct FLIGHTFM FLIGHTS[N - 1 + 1];
    _imp_create_constdv(, struct FLIGHTFM, FLIGHTS, 1, N);

    //    15          %integer i,start,destination,stops, min cost, max cost

    int I = 0xFEFEFEFE;
    int START = 0xFEFEFEFE;
    int DESTINATION = 0xFEFEFEFE;
    int STOPS = 0xFEFEFEFE;
    int MINCOST = 0xFEFEFEFE;
    int MAXCOST = 0xFEFEFEFE;

    //    16
    //    17          %routine cheapest price( %integer src, dst, stops, cost so far )

    void CHEAPESTPRICE(int SRC, int DST, int STOPS, int COSTSOFAR) {
      _imp_enter(CHEAPESTPRICE);

      //    18              %integer i

      int I = 0xFEFEFEFE;

      //    19
      //    20              %if (src = dst) %start

      if ((SRC) != (DST)) goto L_0002;

        //    21                  min cost = min(min cost, cost so far)

      MINCOST = MIN(MINCOST, COSTSOFAR);

      //    22              %finish %else %if (stops >= 0) %start

      goto L_0003;
    L_0002:;
      if ((STOPS) < (0)) goto L_0004;

        //    23
        //    24                  %for i = 1,1,n %cycle

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

        //    25                      %if (flights(i)_from = src) %start

        if ((FLIGHTS[I - 1].FROM) != (SRC)) goto L_0008;

          //    26                          cheapest price( flights(i)_to, dst, stops - 1, cost so far + flights(i)_cost )

        CHEAPESTPRICE(FLIGHTS[I - 1].TO, DST, STOPS - 1, COSTSOFAR + FLIGHTS[I - 1].COST);

        //    27                      %finish

      L_0008:;

      //    28                  %repeat

        goto L_0005;
      }

    L_0006:;

    //    29
    //    30              %finish

    L_0004:;
    L_0003:;

    //    31
    //    32          %end

      _imp_return;
      _imp_begin_backtrace(CHEAPESTPRICE);
      _imp_diagf("\n\nArguments:\n\n");
      _imp_diagf("SRC = %d\n", SRC);
      _imp_diagf("DST = %d\n", DST);
      _imp_diagf("STOPS = %d\n", STOPS);
      _imp_diagf("COSTSOFAR = %d\n", COSTSOFAR);
      _imp_diagf("\nLocal Variables:\n\n");
      _imp_bt_int(I);
      _imp_end_backtrace();
    } /* end of CHEAPESTPRICE */

    //    33
    //    34          %for i=1,1,n %cycle

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

      //    35              read(flights(i)_from)

      _imp_READ(((_imp_name){.address = &FLIGHTS[I - 1].FROM, .typeinfo = 1, .sizeinfo = sizeof(FLIGHTS[I - 1].FROM)}));

      //    36              read(flights(i)_to)

      _imp_READ(((_imp_name){.address = &FLIGHTS[I - 1].TO, .typeinfo = 1, .sizeinfo = sizeof(FLIGHTS[I - 1].TO)}));

      //    37              read(flights(i)_cost)

      _imp_READ(((_imp_name){.address = &FLIGHTS[I - 1].COST, .typeinfo = 1, .sizeinfo = sizeof(FLIGHTS[I - 1].COST)}));

      //    38          %repeat

      goto L_0009;
    }

  L_000A:;

  //    39          read(start)

    _imp_READ(((_imp_name){.address = &START, .typeinfo = 1, .sizeinfo = sizeof(START)}));

    //    40          read(destination)

    _imp_READ(((_imp_name){.address = &DESTINATION, .typeinfo = 1, .sizeinfo = sizeof(DESTINATION)}));

    //    41          read(stops)

    _imp_READ(((_imp_name){.address = &STOPS, .typeinfo = 1, .sizeinfo = sizeof(STOPS)}));

    //    42
    //    43          select output(0)

    _imp_SELECTOUTPUT(0);

    //    44          %for i=1,1,n %cycle

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

      //    45              print string("Flight(".itos(i,0)."): ");

      _imp_printf("%s%S%s", "Flight(", _imp_strp(_imp_ITOS(I, 0)), "): ");

      //    46              print string(" From: ".itos(flights(i)_from,0))

      _imp_printf("%s%S", " From: ", _imp_strp(_imp_ITOS(FLIGHTS[I - 1].FROM, 0)));

      //    47              print string("   To: ".itos(flights(i)_to,0))

      _imp_printf("%s%S", "   To: ", _imp_strp(_imp_ITOS(FLIGHTS[I - 1].TO, 0)));

      //    48              print string(" Cost: ".itos(flights(i)_cost,0))

      _imp_printf("%s%S", " Cost: ", _imp_strp(_imp_ITOS(FLIGHTS[I - 1].COST, 0)));

      //    49              newline

      _imp_NEWLINE();

      //    50          %repeat

      goto L_000C;
    }

  L_000D:;

  //    51          printstring("      Start: ".itos(start,0)); newline

    _imp_printf("%s%S", "      Start: ", _imp_strp(_imp_ITOS(START, 0)));
    _imp_NEWLINE();

    //    52          printstring("Destination: ".itos(destination,0)); newline

    _imp_printf("%s%S", "Destination: ", _imp_strp(_imp_ITOS(DESTINATION, 0)));
    _imp_NEWLINE();

    //    53          printstring("      Stops: ".itos(stops,0)); newline

    _imp_printf("%s%S", "      Stops: ", _imp_strp(_imp_ITOS(STOPS, 0)));
    _imp_NEWLINE();

    //    54          max cost = 0

    MAXCOST = 0;

    //    55          %for i = 1,1,n %cycle

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

      //    56              max cost = max cost + flights(i)_cost

      MAXCOST = MAXCOST + FLIGHTS[I - 1].COST;

      //    57          %repeat

      goto L_000E;
    }

  L_000F:;

  //    58          print string( "Greatest cost: ".itos(max cost,0));newline

    _imp_printf("%s%S", "Greatest cost: ", _imp_strp(_imp_ITOS(MAXCOST, 0)));
    _imp_NEWLINE();

    //    59
    //    60          min cost = max cost

    MINCOST = MAXCOST;

    //    61          cheapest price( start, destination, stops, 0 )

    CHEAPESTPRICE(START, DESTINATION, STOPS, 0);

    //    62          %if (min cost = max cost) %start

    if ((MINCOST) != (MAXCOST)) goto L_0010;

      //    63              print string( "No route found for #hops, start, end points and the given flights data")

    _imp_printf("%s", "No route found for #hops, start, end points and the given flights data");

    //    64          %finish %else %start

    goto L_0011;
  L_0010:;

  //    65              print string( "Minimum cost: ".itos(min cost,0))

    _imp_printf("%s%S", "Minimum cost: ", _imp_strp(_imp_ITOS(MINCOST, 0)));

    //    66          %finish

  L_0011:;

  //    67          newline

    _imp_NEWLINE();

    //    68
    //    69      %end

    goto end_1000;
  end_1000:;
    _imp_begin_backtrace(_imp_block_1000);
    _imp_diagf("\n\nLocal Variables:\n\n");
    _imp_bt_int(I);
    _imp_bt_int(START);
    _imp_bt_int(DESTINATION);
    _imp_bt_int(STOPS);
    _imp_bt_int(MINCOST);
    _imp_bt_int(MAXCOST);
    _imp_end_backtrace();
  }

  //    70
  //    71  %end

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

//    72  %endoffile