#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 x

  int X = 0xFEFEFEFE;

  //     3
  //     4    ! The intention was this:
  //     5    x = (65 << 24) ! (76 << 16) ! (71 << 8) ! 3

  X = ((int)65) << (((int)24) & 31) | ((int)76) << (((int)16) & 31) | ((int)71) << (((int)8) & 31) | 3;

  //     6
  //     7    ! but the vax compiler contained this line:
  //     8
  //     9    x = ((3 + (71 << 8)) + 76 << 16) + 65 << 24

  X = 3 + (((int)71) << (((int)8) & 31)) + (((int)76) << (((int)16) & 31)) + (((int)65) << (((int)24) & 31));

  //    10
  //    11    ! and C is complaining that the result does not fit in 32 bits.
  //    12    ! So clearly, the precedence of shift vs add is wrong!
  //    13
  //    14  !    5 Statements compiled
  //    15  ! test/shiftprecbug.c: In function 'main':
  //    16  ! test/shiftprecbug.c:17:39: warning: result of '1196359745 << 24' requires 56 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
  //    17  !    17 | X = ((3 + (71 << 8)) + 76 << 16) + 65 << 24;
  //    18  !       |                                       ^~
  //    19
  //    20  %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(X);
  _imp_end_backtrace();
  _imp_stop(0);
}