// File: /workspace/github_c_makefile_star2_O1/pedro-onate[P]eva/eva.c
// Function: es_vm_run
static void* es_vm_run(es_ctx_t* ctx, es_vm_mode_t mode, ...)
{

#ifdef LABELS_AS_VALUES
  static es_inst_info_t inst_info[] = {
    { &&HALT,       "halt",       0 },
    { &&CONST,      "const",      1 },
    { &&POP,        "pop",        0 },
    { &&GLOBAL_REF, "global-ref", 1 },
    { &&GLOBAL_SET, "global-set", 1 },
    { &&CLOSED_REF, "closed-ref", 2 },
    { &&CLOSED_SET, "closed-set", 2 },
    { &&ARG_REF,    "arg-ref",    1 },
    { &&ARG_SET,    "arg-set",    1 },
    { &&JMP,        "jmp",        1 },
    { &&BF,         "bf",         1 },
    { &&CALL,       "call",       1 },
    { &&TAIL_CALL,  "tail-call",  1 },
    { &&RETURN,     "return",     0 },
    { &&CLOSURE,    "closure",    1 }
  };

  if (mode == ES_VM_FETCH_OPCODE) {
    va_list ap;
    va_start(ap, mode);
    es_opcode_t opcode = va_arg(ap, es_opcode_t);
    va_end(ap);
    return &inst_info[opcode];
  }
#endif

  va_list ap;
  va_start(ap, mode);
  es_val_t proc = va_arg(ap, es_val_t);
  va_end(ap);

  es_inst_t* inst  = es_bytecode_val(ctx->bytecode)->inst;
  ctx->ip          = inst + es_proc_addr(proc);
  ctx->args        = es_nil;
  es_val_t* consts = es_bytecode_val(ctx->bytecode)->consts;

  #ifdef LABELS_AS_VALUES
    #define SWITCH(value) goto *(value);
    #define CASE(label)   label
    #define BREAK         goto *(ctx->ip->opcode)
  #else
    #define SWITCH(value) switch(value)
    #define CASE(label)   case label
    #define BREAK         break
  #endif

  while(1) {
    SWITCH(ctx->ip->opcode) {
      CASE(HALT):
        //assert(ctx->sp - ctx->stack == 1);
        return (void*)pop(ctx);
      CASE(CONST): {
        int const_idx = ctx->ip->operand1;
        push(ctx, consts[const_idx]);
        ctx->ip++;
        BREAK;
      }
      CASE(POP):
        ctx->sp--;
        ctx->ip++;
        BREAK;
      CASE(BF): {
        int else_br = ctx->ip->operand1;
        es_val_t val = pop(ctx);
        ctx->ip += es_is_true(val) ? 1 : else_br;
        BREAK;
      }
      CASE(JMP):
        ctx->ip += ctx->ip->operand1;
        BREAK;
      CASE(GLOBAL_REF): {
        int global_idx      = ctx->ip->operand1;
        es_val_t global_val = env_ref(ctx, ctx->env, global_idx);
        push(ctx, global_val);
        ctx->ip++;
        BREAK;
      }
      CASE(GLOBAL_SET): {
        int global_idx = ctx->ip->operand1;
        es_val_t val   = pop(ctx);
        push(ctx, env_set(ctx, ctx->env, global_idx, val));
        ctx->ip++;
        BREAK;
      }
      CASE(CLOSED_REF): {
        int depth = ctx->ip->operand1;
        int idx   = ctx->ip->operand2;
        es_args_t* cenv = es_args_val(ctx->args);
        while(depth-- > 0) {
          cenv = cenv->parent;
        }
        push(ctx, cenv->args[idx]);
        ctx->ip++;
        BREAK;
      }
      CASE(CLOSED_SET): {
        int depth = ctx->ip->operand1;
        int idx   = ctx->ip->operand2;
        es_args_t* cenv = es_args_val(ctx->args);
        while(depth-- > 0) {
          cenv = cenv->parent;
        }
        cenv->args[idx] = pop(ctx);
        push(ctx, es_void);
        ctx->ip++;
        BREAK;
      }
      CASE(ARG_REF): {
        int arg_idx  = ctx->ip->operand1;
        es_val_t arg = es_args_val(ctx->args)->args[arg_idx];
        push(ctx, arg);
        ctx->ip++;
        BREAK;
      }
      CASE(ARG_SET): {
        int arg_idx  = ctx->ip->operand1;
        es_val_t val = pop(ctx);
        es_args_val(ctx->args)->args[arg_idx] = val;
        ctx->ip++;
        BREAK;
      }
      CASE(CLOSURE): {
        int const_idx = ctx->ip->operand1;
        es_val_t proc = consts[const_idx];
        es_val_t closure = es_make_closure(ctx, ctx->args, proc);
        push(ctx, closure);
        ctx->ip++;
        BREAK;
      }
      CASE(RETURN):
        restore(ctx);
        BREAK;
      CASE(CALL): {
        int argc = ctx->ip->operand1;
        ctx->ip++;
        es_val_t proc = pop(ctx);
        if (es_is_fn(proc)) {
          es_val_t* argv = ctx->sp - argc;
          es_val_t res   = es_fn_apply_argv(ctx, proc, argc, argv);
          pop_n(ctx, argc);
          push(ctx, res);
        } else if (es_is_closure(proc)) {
          es_closure_t* closure = es_closure_val(proc);
          es_proc_t* proc = es_obj_to(es_proc_t*, closure->proc);
          save(ctx);
          es_val_t* argv = ctx->sp - argc;
          ctx->args  = es_make_args(ctx, closure->env, proc->arity, proc->rest, argc, argv);
          pop_n(ctx, argc);
          ctx->ip = inst + proc->addr;
        } else if (es_is_cont(proc)) {
          // TODO
          assert(0);
        }
        BREAK;
      }
      CASE(TAIL_CALL): {
        int argc = ctx->ip->operand1;
        ctx->ip++;
        es_val_t proc = pop(ctx);
        if (es_is_fn(proc)) {
          es_val_t* argv = ctx->sp - argc;
          es_val_t res   = es_fn_apply_argv(ctx, proc, argc, argv);
          pop_n(ctx, argc);
          push(ctx, res);
          restore(ctx);
        } else if (es_is_closure(proc)) {
          es_closure_t* closure = es_closure_val(proc);
          es_proc_t* proc = es_obj_to(es_proc_t*, closure->proc);
          es_val_t* argv  = ctx->sp - argc;
          ctx->args   = es_make_args(ctx, closure->env, proc->arity, proc->rest, argc, argv);
          pop_n(ctx, argc);
          ctx->ip = inst + proc->addr;
        } else if (es_is_cont(proc)) {
          // TODO
          assert(0);
        }
        BREAK;
      }
    }
  }
  return (void*)es_void;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x40(%rsp), %rdi
movq %rdx, 0x10(%rdi)
movq %rcx, 0x18(%rdi)
movq %r8, 0x20(%rdi)
movq %r9, 0x28(%rdi)
testb %al, %al
je 0x4752
movaps %xmm0, 0x70(%rsp)
movaps %xmm1, 0x80(%rsp)
movaps %xmm2, 0x90(%rsp)
movaps %xmm3, 0xa0(%rsp)
movaps %xmm4, 0xb0(%rsp)
movaps %xmm5, 0xc0(%rsp)
movaps %xmm6, 0xd0(%rsp)
movaps %xmm7, 0xe0(%rsp)
leaq 0x130(%rsp), %rcx
movabsq $0x3000000010, %rax     # imm = 0x3000000010
testl %esi, %esi
je 0x47c8
movq %rdi, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
movl $0x10, %eax
cmpq $0x28, %rax
ja 0x47f5
movl $0x10, %ecx
addq 0x10(%rsp), %rcx
addl $0x8, %eax
movl %eax, (%rsp)
movq (%rcx), %rax
movq 0x42060(%rbx), %r14
movq 0x8018(%r14), %r15
movslq 0x18(%rax), %rax
shlq $0x4, %rax
addq %r15, %rax
movq %rax, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %r12
jmpq *(%rax)
movq %rdi, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movl $0x10, %ecx
cmpq $0x28, %rcx
ja 0x483a
movl $0x10, %eax
addq 0x30(%rsp), %rax
addl $0x8, %ecx
movl %ecx, 0x20(%rsp)
jmp 0x4848
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax), %rax
movq 0x42060(%rbx), %r14
movq 0x8018(%r14), %r15
movslq 0x18(%rax), %rax
shlq $0x4, %rax
addq %r15, %rax
movq %rax, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %r12
jmpq *(%rax)
movq 0x28(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movl (%rax), %eax
leaq (%rax,%rax,2), %rax
leaq 0x688b(%rip), %rcx      # 0xb0e0
leaq (%rcx,%rax,8), %rax
jmp 0x4f54
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswq 0x8(%rax), %rdx
movq 0x10(%r14,%rdx,8), %rdx
leaq 0x8(%rcx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rdx, (%rcx)
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
addq $-0x8, 0x42070(%rbx)
movq 0x42068(%rbx), %rax
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswq 0x8(%rax), %rdx
leaq -0x8(%rcx), %rsi
movq %rsi, 0x42070(%rbx)
testl $0xfffffff8, -0x8(%rcx) # imm = 0xFFFFFFF8
movl $0x1, %ecx
cmoveq %rdx, %rcx
shlq $0x4, %rcx
addq %rax, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movq 0x42068(%rbx), %rax
movswq 0x8(%rax), %rcx
shlq $0x4, %rcx
addq %rax, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movq 0x42068(%rbx), %rax
movq 0x42080(%rbx), %rsi
movswl 0x8(%rax), %edx
movq %rbx, %rdi
callq 0x3316
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rax
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswq 0x8(%rax), %rdx
leaq -0x8(%rcx), %rax
movq %rax, 0x42070(%rbx)
movq -0x8(%rcx), %rax
movq 0x42080(%rbx), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
addq $0x18, %rcx
movq (%rcx), %rsi
movl %esi, %edi
andl $0x7, %edi
cmpq $0x5, %rdi
ja 0x49c1
movl $0x1, %edx
leaq 0x2bc4(%rip), %r8       # 0x755c
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
movl (%rsi), %edx
jmp 0x49c6
sarl $0x3, %esi
movl %esi, %edx
jmp 0x49c6
movl $0x2, %edx
jmp 0x49c6
movl $0x3, %edx
jmp 0x49c6
movl $0x4, %edx
jmp 0x49c6
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x9, %edx
jne 0x4a11
movl $0x18, %edx
movq %rbx, %rdi
movl $0x16, %esi
callq 0x2177
leaq 0x309e(%rip), %rcx      # 0x7a82
movq %rcx, 0x10(%rax)
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rax
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq %rax, (%rcx)
movl $0x5b, %eax
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rax
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42088(%rbx), %rcx
movswl 0x8(%rax), %esi
movswq 0xa(%rax), %rdx
testl %esi, %esi
jle 0x4a6a
incl %esi
movq 0x10(%rcx), %rcx
decl %esi
cmpl $0x1, %esi
jg 0x4a5f
movq 0x20(%rcx,%rdx,8), %rcx
movq 0x42070(%rbx), %rdx
leaq 0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rcx, (%rdx)
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42088(%rbx), %rcx
movswl 0x8(%rax), %esi
movswq 0xa(%rax), %rdx
testl %esi, %esi
jle 0x4ab9
incl %esi
movq 0x10(%rcx), %rcx
decl %esi
cmpl $0x1, %esi
jg 0x4aae
movq 0x42070(%rbx), %rsi
leaq -0x8(%rsi), %rdi
movq %rdi, 0x42070(%rbx)
movq -0x8(%rsi), %rdi
movq %rdi, 0x20(%rcx,%rdx,8)
movq %rsi, 0x42070(%rbx)
movq $0x5b, -0x8(%rsi)
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswq 0x8(%rax), %rdx
movq 0x42088(%rbx), %rsi
movq 0x20(%rsi,%rdx,8), %rdx
leaq 0x8(%rcx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rdx, (%rcx)
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswq 0x8(%rax), %rdx
leaq -0x8(%rcx), %rsi
movq %rsi, 0x42070(%rbx)
movq -0x8(%rcx), %rcx
movq 0x42088(%rbx), %rsi
movq %rcx, 0x20(%rsi,%rdx,8)
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42088(%rbx), %rsi
movswq 0x8(%rax), %rax
movq 0x10(%r14,%rax,8), %rdx
movq %rbx, %rdi
callq 0x341b
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rax
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movslq 0x42078(%rbx), %rax
decq %rax
movl %eax, 0x42078(%rbx)
shlq $0x4, %rax
movq (%r12,%rax), %rcx
movq %rcx, 0x42088(%rbx)
movq 0x8(%r12,%rax), %rax
movq %rax, 0x42068(%rbx)
jmpq *(%rax)
movq 0x42068(%rbx), %rax
movq 0x42070(%rbx), %rcx
movswl 0x8(%rax), %edx
movslq %edx, %r8
addq $0x10, %rax
movq %rax, 0x42068(%rbx)
leaq -0x8(%rcx), %r9
movq %r9, 0x42070(%rbx)
movq -0x8(%rcx), %rax
movl %eax, %ecx
andl $0x7, %ecx
cmpq $0x5, %rcx
ja 0x4c4a
movl $0x1, %edx
leaq 0x28ab(%rip), %rsi      # 0x74cc
movslq (%rsi,%rcx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl (%rax), %edx
jmp 0x4c4f
movl %eax, %edx
sarl $0x3, %edx
jmp 0x4c4f
movl $0x2, %edx
jmp 0x4c4f
movl $0x3, %edx
jmp 0x4c4f
movl $0x4, %edx
jmp 0x4c4f
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0xe, %edx
jne 0x4cac
leaq (,%r8,8), %r13
subq %r13, %r9
movq %rbx, %rdi
movl %r8d, %esi
movq %r9, %rdx
callq *0x18(%rax)
movq 0x42070(%rbx), %rcx
subq %r13, %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movslq 0x42078(%rbx), %rax
decq %rax
movl %eax, 0x42078(%rbx)
shlq $0x4, %rax
movq (%r12,%rax), %rcx
movq %rcx, 0x42088(%rbx)
movq 0x8(%r12,%rax), %rax
jmp 0x4d2d
cmpq $0x5, %rcx
ja 0x4ce7
movl $0x1, %edx
leaq 0x2826(%rip), %rsi      # 0x74e4
movslq (%rsi,%rcx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl (%rax), %edx
jmp 0x4cec
movl %eax, %edx
sarl $0x3, %edx
jmp 0x4cec
movl $0x2, %edx
jmp 0x4cec
movl $0x3, %edx
jmp 0x4cec
movl $0x4, %edx
jmp 0x4cec
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x8, %edx
jne 0x4d36
movq 0x10(%rax), %rsi
movq 0x18(%rax), %r13
leaq (,%r8,8), %rbp
subq %rbp, %r9
movl 0x10(%r13), %edx
movl 0x14(%r13), %ecx
movq %rbx, %rdi
callq 0x650c
movq %rax, 0x42088(%rbx)
subq %rbp, 0x42070(%rbx)
movslq 0x18(%r13), %rax
shlq $0x4, %rax
addq %r15, %rax
movq %rax, 0x42068(%rbx)
jmp 0x4d7f
cmpq $0x5, %rcx
ja 0x4d71
movl $0x1, %edx
leaq 0x27b4(%rip), %rsi      # 0x74fc
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movl (%rax), %edx
jmp 0x4d76
sarl $0x3, %eax
movl %eax, %edx
jmp 0x4d76
movl $0x2, %edx
jmp 0x4d76
movl $0x3, %edx
jmp 0x4d76
movl $0x4, %edx
jmp 0x4d76
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x13, %edx
je 0x4f66
movq 0x42068(%rbx), %rax
jmpq *(%rax)
movq 0x42068(%rbx), %rcx
movq 0x42070(%rbx), %rax
movswl 0x8(%rcx), %edx
movslq %edx, %r8
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
leaq -0x8(%rax), %r9
movq %r9, 0x42070(%rbx)
movq -0x8(%rax), %rax
movl %eax, %edx
andl $0x7, %edx
cmpq $0x5, %rdx
ja 0x4df8
movl $0x1, %esi
leaq 0x2746(%rip), %rdi      # 0x7514
movslq (%rdi,%rdx,4), %r10
addq %rdi, %r10
jmpq *%r10
movl (%rax), %esi
jmp 0x4dfd
movl %eax, %esi
sarl $0x3, %esi
jmp 0x4dfd
movl $0x2, %esi
jmp 0x4dfd
movl $0x3, %esi
jmp 0x4dfd
movl $0x4, %esi
jmp 0x4dfd
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpl $0xe, %esi
jne 0x4e3a
leaq (,%r8,8), %r13
subq %r13, %r9
movq %rbx, %rdi
movl %r8d, %esi
movq %r9, %rdx
callq *0x18(%rax)
movq 0x42070(%rbx), %rcx
subq %r13, %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rax
jmpq *(%rax)
cmpq $0x5, %rdx
ja 0x4e76
movl $0x1, %esi
leaq 0x26e0(%rip), %rdi      # 0x752c
movslq (%rdi,%rdx,4), %r10
addq %rdi, %r10
jmpq *%r10
movl (%rax), %esi
jmp 0x4e7b
movl %eax, %esi
sarl $0x3, %esi
jmp 0x4e7b
movl $0x2, %esi
jmp 0x4e7b
movl $0x3, %esi
jmp 0x4e7b
movl $0x4, %esi
jmp 0x4e7b
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpl $0x8, %esi
jne 0x4ef0
movq 0x18(%rax), %r13
movq 0x42088(%rbx), %rdx
movslq 0x42078(%rbx), %rsi
leal 0x1(%rsi), %edi
shlq $0x4, %rsi
movq %rdx, (%r12,%rsi)
movq %rcx, 0x8(%r12,%rsi)
movl %edi, 0x42078(%rbx)
leaq (,%r8,8), %rbp
subq %rbp, %r9
movq 0x10(%rax), %rsi
movl 0x10(%r13), %edx
movl 0x14(%r13), %ecx
movq %rbx, %rdi
callq 0x650c
movq %rax, 0x42088(%rbx)
subq %rbp, 0x42070(%rbx)
movslq 0x18(%r13), %rax
shlq $0x4, %rax
addq %r15, %rax
movq %rax, 0x42068(%rbx)
movq 0x42068(%rbx), %rax
jmpq *(%rax)
cmpq $0x5, %rdx
ja 0x4f2b
movl $0x1, %ecx
leaq 0x2642(%rip), %rsi      # 0x7544
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movl (%rax), %ecx
jmp 0x4f30
sarl $0x3, %eax
movl %eax, %ecx
jmp 0x4f30
movl $0x2, %ecx
jmp 0x4f30
movl $0x3, %ecx
jmp 0x4f30
movl $0x4, %ecx
jmp 0x4f30
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x13, %ecx
je 0x4f85
movq 0x42068(%rbx), %rax
jmpq *(%rax)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x42070(%rbx)
movq -0x8(%rax), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28c4(%rip), %rdi      # 0x7831
leaq 0x281a(%rip), %rsi      # 0x778e
leaq 0x2d3c(%rip), %rcx      # 0x7cb7
movl $0x9d7, %edx            # imm = 0x9D7
callq 0x10a0
leaq 0x28a5(%rip), %rdi      # 0x7831
leaq 0x27fb(%rip), %rsi      # 0x778e
leaq 0x2d1d(%rip), %rcx      # 0x7cb7
movl $0x9c0, %edx            # imm = 0x9C0
callq 0x10a0