void exec_listp() { // 3
  MUnaryPredicate(M1P_LISTP);
  return;
}

void exec_not() { // 8
  MGetStackArg1();
  if (Meq(stack_arg1, Mfalse))
    MSetStackArg1(Mtrue);
  else
    MSetStackArg1(Mfalse);
  MNextInstruction();
  return;
}

void exec_list_ref() { // 27
  MBinaryOp(M2_LIST_REF_INTEGER);
  return;
}

void exec_stringp() { // 33
  MUnaryPredicate(M1P_STRINGP);
  return;
}

void exec_vectorp() { // 34
  MUnaryPredicate(M1P_VECTORP);
  return;
}

void exec_vector_length() { // 35
  MUnaryOp(M1_VECTOR_LENGTH_INTEGER);
  return;
}

void exec_eof_object() { // 38
  MUnaryPredicate(M1P_EOF_OBJECT);
  return;
}

void exec_display() { // 39
  MUnaryOp(M1_DISPLAY);
  return;
}

void exec_newline() { // 40
  stack_arg1 = newline_();
  push(stack_arg1);
  MNextInstruction();
  return;
}

void exec_rplaca() { // 54
  MBinaryOp(M2_RPLACA);
  return;
}

void exec_rplacd() { // 55
  MBinaryOp(M2_RPLACD);
  return;
}

void exec_read_char() { // 56
  stack_arg1 = read_char_();
  MpushOld(stack_arg1);
  MNextInstruction();
  return;
}

void exec_string_ref() { // 57
  MBinaryOp(M2_STRING_REF_INTEGER);
  return;
}

void exec_string_set() { // 58
  MGetStackArg1();
  MGetStackArg2();
  MGetStackArg3();
  stack_arg1 = string_set_integer(stack_arg3, stack_arg2, stack_arg1);
  MPop();
  MPop();
  MSetStackArg1(stack_arg1);
  MNextInstruction();
  return;
}

void exec_write_char() { // 59
  MUnaryOp(M1_WRITE_CHR);
  return;
}

void exec_list_tail() { // 60
  MBinaryOp(M2_LIST_TAIL_INTEGER);
  return;
}

void exec_chareq() { // 61
  MBinaryPredicate(M2P_CHAREQ);
  return;
}

void exec_charnum() { // 62
  MUnaryOp(M1_CHARNUM);
  return;
}

void exec_charalpha() { // 63
  MUnaryOp(M1_CHARALPHA);
  return;
}

void exec_charup() { // 64
  MUnaryOp(M1_CHARUP);
  return;
}

void exec_chardown() { // 65
  MUnaryOp(M1_CHARDOWN);
  return;
}

void exec_make_string() { // 66
  MUnaryOp(M1_MAKE_STRING_INTEGER);
  return;
}

void exec_stringeq() { // 67
  MBinaryPredicate(M2P_STRINGEQ);
  return;
}

void exec_string_length() { // 68
  MUnaryOp(M1_STRING_LENGTH_INTEGER);
  return;
}

void exec_string_to_symbol() { // 69
  MUnaryOp(M1_STRING_TO_SYMBOL);
  return;
}

void exec_string_to_number() { // 70
  MUnaryOp(M1_STRING_TO_NUMBER);
  return;
}

void exec_number_to_string() { // 71
  MUnaryOp(M1_NUMBER_TO_STRING);
  return;
}

void exec_symbol_to_string() { // 72
  MUnaryOp(M1_SYMBOL_TO_STRING);
  return;
}

void exec_make_vector() { // 73
  MUnaryOp(M1_MAKE_VECTOR_INTEGER);
  return;
}

void exec_writeout() { // 74
  MUnaryOp(M1_WRITEOUT);
  return;
}

void exec_trace() { // 75
  trace = 1;
  MNextInstruction();
  return;
}

void exec_eqv() { // 76
  MBinaryOp(M2_EQV);
  return;
}

void exec_charp() { // 77
  MUnaryPredicate(M1P_CHARP);
  return;
}

void exec_char_to_int() { // 78
  MUnaryOp(M1_CHAR_TO_INT);
  return;
}

void exec_inttochar() { // 79
  MUnaryOp(M1_INTTOCHAR);
  return;
}

void exec_halt() { // 80
  MGetStackArg1();
  MPop();
  main_cleanup();
  my_exit(0);
  return;
}

void exec_pair() { // 81
  MUnaryOp(M1_PAIRP);
  return;
}

// 82 ???? name

void exec_argsd(int nargs) { // 83
  int temp;
  int idx;
  int list;
  int intArg1;
  operands = Moperands();
  intArg1 = Mintvecref(Marg1());
  if (nargs < intArg1) {
    printf("wrong number of arguments calling ");
    temp = writeout(kar(binding));
    writeln();
    cleanup_();
  }

  list = 0;
  arglist = make_vector_no_init(intArg1 + 1);
  Mvecset(arglist, intArg1 + 1, 0); // argsd initially empty
  for (idx = 1; idx <= nargs - intArg1; idx++) {
    MGetStackArg1();
    MPop();
    list = kons(stack_arg1, list);
  }
  Mvecset(arglist, intArg1 + 1, list);
  for (idx = intArg1 - 1; idx >= 0; idx--) {
    MGetStackArg1();
    MPop();
    Mvecset(arglist, idx + 1, stack_arg1);
  }
  env = kons(arglist, env);  
  MNextInstruction();
  return;
}

void exec_procedurep() { // 86
  MUnaryPredicate(M1P_PROCEDUREP);
  return;
}

void exec_exit() { // 87
  int return_code;
  MGetStackArg1();
  return_code = get_integer_val(stack_arg1);
  my_exit(return_code);
  return;
}

void exec_boolp() { // 88
  MUnaryPredicate(M1P_BOOLP);
  return;
}

void exec_cc() { // 89
  arglist = make_vector_no_init(1); // vector of size 1 to store the stack
  Mvecset(arglist, 1, stack);
  MpushOld(
    MmakeProc(
      MakeClosure(
        call_cc_code, // procedures code
        kons(arglist, 0)))); // environment is the stack
  MNextInstruction();
  return;
}

void exec_setcc() { // 90
  MGetStackArg1();
  stack = stack_arg1;
  MNextInstruction();
  return;
}

void exec_memv() { // 91
  MBinaryOp(M2_MEMV);
  return;
}

void exec_assv() { // 92
  MBinaryOp(M2_ASSV);
  return;
}

void exec_integerp() { // 93
  MUnaryPredicate(M1P_INTEGERP);
  return;
}

void exec_makevectorwithfill() { // 94
  MBinaryOp(M2_MAKE_VECTOR_INTEGER_WITH_FILL);
  return;
}

void exec_makestringwithfill() { // 95
  MBinaryOp(M2_MAKE_STRING_INTEGER_WITH_FILL);
  return;
}

void exec_stringlt() { // 101
  MBinaryPredicate(M2P_STRING_LT);
  return;
}

void exec_stringgt() { // 102
  MBinaryPredicate(M2P_STRING_GT); // TODO remove extra exec_... proc calls
  return;
}

void exec_stringle() { // 103
  MBinaryPredicate(M2P_STRING_LE);
  return;
}

void exec_stringge() { // 104
  MBinaryPredicate(M2P_STRING_GE);
  return;
}

void exec_stringcilt() { // 105
  MBinaryPredicate(M2P_STRING_CI_LT);
  return;
}

void exec_stringcigt() { // 106
  MBinaryPredicate(M2P_STRING_CI_GT);
  return;
}

void exec_stringcile() { // 107
  MBinaryPredicate(M2P_STRING_CI_LE);
  return;
}

void exec_stringcige() { // 108
  MBinaryPredicate(M2P_STRING_CI_GE);
  return;
}

void exec_stringcieq() { // 109
  MBinaryPredicate(M2P_STRING_CI_EQ);
  return;
}



void exec_() {
  int temp;
  int temp2;
  int temp3;
  int temp4;
  int primitive_fn;
  int nargs;
  int arg1;
  int idx;
  int hash_idx;
  int processing;

  if (!Mlistp(instructions)) {
    printf("bad code");
    writeln();
    my_exit(0);
  }

  nargs = 0;
  operands = 0;
  processing = 1;

  while (processing) {
    instruction = Mcar(instructions);
    opcode = Mopcode();

//  if (trace) {
//    writeln();
//    printf("$$ ");
//    temp = writeout(opcode);
//    writeln();
//  }

    if (Msymbolp(opcode)) {
      primitive_fn = MsymProcRef(opcode);
//    if (debug)
//      histogram[primitive_fn] = histogram[primitive_fn] + 1;
      switch (primitive_fn) {
        case 0: // car
          MGetStackArg1();
          if (Mpairp(stack_arg1))
            stack_arg1 = Mcar(stack_arg1);
          else
            notpair();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 1: // cdr
          MGetStackArg1();
          if (Mpairp(stack_arg1))
            stack_arg1 = Mcdr(stack_arg1);
          else
            notpair();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 2: // cons
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = kons(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 3: // list?
          exec_listp();
          break;
        case 4: // symbol?
          MGetStackArg1();
          if (Msymbolp(stack_arg1))
            MSetStackArg1(Mtrue);
          else
            MSetStackArg1(Mfalse);
          MNextInstruction();
          break;
        case 5: // eq
          if (Meq(MStackArg2Ref(), MStackArg1Ref())) {
            MPop();
            MSetStackArg1(Mtrue);
          }
          else {
            MPop();
            MSetStackArg1(Mfalse);
          }
          MNextInstruction();
          break;
        case 6: // quote
          break;
        case 7: // null
          MGetStackArg1();
          if (Mnotnull(stack_arg1))
            MSetStackArg1(Mfalse);
          else
            MSetStackArg1(Mtrue);
          MNextInstruction();
          break;
        case 8:
          exec_not();
          break;
        case 9: // caar
          MUnaryOp(M1_KAAR);
          break;
        case 10: // cdar
          MUnaryOp(M1_KDAR);
          break;
        case 11: // cadr
          MUnaryOp(M1_KADR);
          break;
        case 12: // cddr
          MUnaryOp(M1_KDDR);
          break;
        case 13: // const
          operands = Moperands();
          Mpush(Marg1());
          MNextInstruction();
          break;
        case 14: // lvar
          operands = Moperands();
          // explicit inline of brute_list_ref
          temp2 = env;
          for (temp = 1; temp <= Mintvecref(Marg1()); temp++)
            temp2 = Mcdr(temp2);
          temp = Mcar(temp2);
          temp2 = Marg2();
          temp3 = Mintvecref(temp2);
          temp4 = Mvecvecref( // vector_ref_no_check
            temp,
            temp3);
          Mpush(temp4);
//        if (trace)
//          binding = Mcdr(Mcdr(operands));
          MNextInstruction();
          break;
        case 15: // lset
          operands = Moperands();
          MGetStackArg1();
          // 
          // set_local_var(
          //   Mintvecref(Marg1()), Mintvecref(Marg2()), stack_arg1);
          //
          temp = Mintvecref(Marg1());
          temp2 =
            list_ref(
              env,
              temp);
          temp4 = Marg2();
          temp3 = Mintvecref(temp4);
          Mvecvecset(
            temp2,
            temp3,
            stack_arg1);
          MNextInstruction();
          break;
        case 16: // gvar
          operands = Moperands();
          arg1 = Marg1();
//        if (trace) {
//          writeln();
//          printf("$$$ ");
//          temp = writeout(arg1);
//          writeln();
//        }
          temp = Mveclength(globals);
          hash_idx = hash_atom(arg1, temp);
          binding = hash_binding(globals, arg1, hash_idx);
          if (binding != 0) {
            MpushOld(Mcdr(binding));
          }
          else {
            printf("global not found: ");
            temp = writeout(arg1);
            writeln();
            cleanup_();
          }
          MNextInstruction();
          break;
        case 17: // gset
          operands = Moperands();
          MGetStackArg1();
          temp = hash_set(globals, Marg1(), stack_arg1);
          MNextInstruction();
          break;
        case 18: // args
          operands = Moperands();
          if (nargs != Mintvecref(Marg1())) {
            printf("wrong number of arguments calling ");
            temp = writeout(kar(binding));
            writeln();
            cleanup_();
          }

          arglist = make_vector_no_init(nargs);
          for (idx = nargs - 1; idx >= 0; idx--) {
            MGetStackArg1();
            MPop();
            Mvecset(arglist, idx + 1, stack_arg1);
          }
          env = kons(arglist, env);  
          MNextInstruction();
          break;
        case 19: // br
          operands = Moperands();
          instructions = Marg1();
          break;
        case 20: // btr
          MGetStackArg1();
          if (stack_arg1 != Mfalse) {
            MPop();
            operands = Moperands();
            instructions = Marg1();
          }
          else {
            MPop();
            MNextInstruction();
          }
          break;
        case 21: // bfl
          MGetStackArg1();
          if (stack_arg1 == Mfalse) {
            MPop();
            operands = Moperands();
            instructions = Marg1();
          }
          else {
            MPop();
            MNextInstruction();
          }
          break;
        case 22: // save
          operands = Moperands();
          MpushOld(
            MmakeProc(
            MakeClosure(
              Marg1(), // continuation
              env))); // current environment
          MNextInstruction();
          break;
        case 23: // proc
          operands = Moperands();
          MpushOld(
            MmakeProc(
              MakeClosure(
                operands, // procedures code
                env))); // current environment
          MNextInstruction();
          break;
        case 24: // callj
          operands = Moperands();
          nargs = Mintvecref(Marg1());
          closure = MStackArg1Ref();
          MPop();
          instructions = ClosureCode(closure); // return address
          MrestoreEnv();
          break;
        case 25: // return
          returnval = MStackArg1Ref(); // pop return value
          MPop();
          closure = MStackArg1Ref(); // dont pop off stack clobber later
          instructions = ClosureCode(closure); // return address
          MrestoreEnv();
          MSetStackArg1(returnval); // clobbers closure
          break;
        case 26: // pop
          MPop();
          MNextInstruction();
          break;
        case 27:
          exec_list_ref();
          break;
        case 28: // memq
          MBinaryOp(M2_MEMQ);
          break;
        case 29: // member
          MBinaryOp(M2_MEMBER);
          break;
        case 30: // assq
          MBinaryOp(M2_ASSQ);
          break;
        case 31: // length
          MUnaryOp(M1_LENGTH_INTEGER);
          break;
        case 32: // number?
          MGetStackArg1();
          if (Mnumberp(stack_arg1))
            MSetStackArg1(Mtrue);
          else
            MSetStackArg1(Mfalse);
          MNextInstruction();
          break;
        case 33:
          exec_stringp();
          break;
        case 34:
          exec_vectorp();
          break;
        case 35:
          exec_vector_length();
          break;
        case 36: // vector-ref
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 =
            vector_ref(stack_arg2, get_integer_val(stack_arg1));
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 37: // vector-set!
          MGetStackArg1();
          MGetStackArg2();
          MGetStackArg3();
          stack_arg1 =
            vector_set(
              stack_arg3, get_integer_val(stack_arg2), stack_arg1);
          MPop();
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 38:
          exec_eof_object();
          break;
        case 39:
          exec_display();
          break;
        case 40:
          exec_newline();
          break;
        case 41: // div
          exec_div();
          break;
        case 42: // add
           exec_add();
          break;
        case 43: // sub
           exec_sub();
          break;
        case 44: // mult
           exec_mult();
          break;
        case 45: // quotient
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = quotient_(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 46: // remainder
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = remainder_(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 47: // =
          exec_eq();
          break;
        case 48: // modulo
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = modulo_(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 49: // <
          exec_lt();
          break;
        case 50: // <=
          exec_le();
          break;
        case 51:// >
          exec_gt();
          break;
        case 52: // >=
          exec_ge();
          break;
        case 53: // equal?
          MBinaryPredicate(M2P_EQUALP);
          break;
        case 54:
          exec_rplaca();
          break;
        case 55:
          exec_rplacd();
          break;
        case 56:
          exec_read_char();
          break;
        case 57:
          exec_string_ref();
          break;
        case 58:
          exec_string_set();
          break;
        case 59:
          exec_write_char();
          break;
        case 60:
          exec_list_tail();
          break;
        case 61:
          exec_chareq();
          break;
        case 62:
          exec_charnum();
          break;
        case 63:
          exec_charalpha();
          break;
        case 64:
          exec_charup();
          break;
        case 65:
          exec_chardown();
          break;
        case 66:
          exec_make_string();
          break;
        case 67:
          exec_stringeq();
          break;
        case 68:
          exec_string_length();
          break;
        case 69:
          exec_string_to_symbol();
          break;
        case 70:
          exec_string_to_number();
          break;
        case 71:
          exec_number_to_string();
          break;
        case 72:
          exec_symbol_to_string();
          break;
        case 73:
          exec_make_vector();
          break;
        case 74:
          exec_writeout();
          break;
        case 75:
          exec_trace();
          break;
        case 76:
          exec_eqv();
          break;
        case 77:
          exec_charp();
          break;
        case 78:
          exec_char_to_int();
          break;
        case 79:
          exec_inttochar();
          break;
        case 80:
          exec_halt();
          break;
        case 81:
          exec_pair();
          break;
        case 82:
          exec_halt(); // name
          break;
        case 83:
          exec_argsd(nargs);
          break;
        case 84: // callnj
          operands = Moperands();
          closure = MStackArg1Ref();
          MPop();
          nargs = Mintvecref(MStackArg1Ref());
          MPop();
          instructions = ClosureCode(closure); // return address
          MrestoreEnv();
          break;
        case 85:
          closure = MStackArg1Ref(); // not really closure - overloaded var
          MPop();
          nargs = list_length(closure);
          for (idx = 0; idx < nargs; idx++) {
            stack_arg1 = Mcar(closure);
            MpushOld(stack_arg1);
            closure = Mcdr(closure);
          }
          MpushOld(make_integer(nargs));
          MNextInstruction();
          break;
        case 86:
          exec_procedurep();
          break;
        case 87:
          exec_exit();
          break;
        case 88:
          exec_boolp();
          break;
        case 89:
          exec_cc();
          break;
        case 90:
          exec_setcc();
          break;
        case 91:
          exec_memv();
          break;
        case 92:
          exec_assv();
          break;
        case 93:
          exec_integerp();
          break;
        case 94:
          exec_makevectorwithfill();
          break;
        case 95:
          exec_makestringwithfill();
          break;
        case 96: // bitwise-and
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = bitwiseand(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 97: // bitwise-or
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = bitwiseor(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 98: // bitwise-not
          MUnaryOp(M1_BITWISENOT);
          break;
        case 99: // bitwise-xor
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = bitwisexor(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 100: // arithmetic-shift
          MGetStackArg1();
          MGetStackArg2();
          stack_arg1 = arithmeticshift(stack_arg2, stack_arg1);
          MPop();
          MSetStackArg1(stack_arg1);
          MNextInstruction();
          break;
        case 101:
          exec_stringlt();
          break;
        case 102:
          exec_stringgt();
          break;
        case 103:
          exec_stringle();
          break;
        case 104:
          exec_stringge();
          break;
        case 105:
          exec_stringcilt();
          break;
        case 106:
          exec_stringcigt();
          break;
        case 107:
          exec_stringcile();
          break;
        case 108:
          exec_stringcige();
          break;
        case 109:
          exec_stringcieq();
          break;
