// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
leaq 0x50(%rsp), %rdi
movq %rdx, 0x10(%rdi)
movq %rcx, 0x18(%rdi)
movq %r8, 0x20(%rdi)
movq %r9, 0x28(%rdi)
testb %al, %al
je 0x3a53
movaps %xmm0, 0x80(%rsp)
movaps %xmm1, 0x90(%rsp)
movaps %xmm2, 0xa0(%rsp)
movaps %xmm3, 0xb0(%rsp)
movaps %xmm4, 0xc0(%rsp)
movaps %xmm5, 0xd0(%rsp)
movaps %xmm6, 0xe0(%rsp)
movaps %xmm7, 0xf0(%rsp)
leaq 0x140(%rsp), %rdx
movabsq $0x3000000010, %rcx     # imm = 0x3000000010
pushq $0x10
popq %rax
testl %esi, %esi
je 0x3ad2
movq %rdi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
cmpq $0x28, %rax
ja 0x3af8
movq %rax, %rcx
addq 0x20(%rsp), %rcx
addl $0x8, %eax
movl %eax, 0x10(%rsp)
movq (%rcx), %rax
movq 0x42060(%rbx), %rbp
movq 0x8018(%rbp), %rcx
movslq 0x18(%rax), %r13
shlq $0x4, %r13
movq %rcx, 0x8(%rsp)
addq %rcx, %r13
movq %r13, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %rax
movq %rax, (%rsp)
jmpq *(%r13)
movq %rdi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
cmpq $0x28, %rax
ja 0x3b48
movq %rax, %rcx
addq 0x40(%rsp), %rcx
addl $0x8, %eax
movl %eax, 0x30(%rsp)
jmp 0x3b56
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq (%rax), %rax
movq 0x42060(%rbx), %rbp
movq 0x8018(%rbp), %rcx
movslq 0x18(%rax), %r13
shlq $0x4, %r13
movq %rcx, 0x8(%rsp)
addq %rcx, %r13
movq %r13, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %rax
movq %rax, (%rsp)
jmpq *(%r13)
movq 0x38(%rsp), %rcx
leaq 0x8(%rcx), %rax
movq %rax, 0x38(%rsp)
movl (%rcx), %eax
imulq $0x18, %rax, %rcx
leaq 0x657d(%rip), %rax      # 0xa0e0
addq %rcx, %rax
jmp 0x4098
movswq 0x8(%r13), %rax
movq 0x10(%rbp,%rax,8), %rax
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
addq $-0x8, 0x42070(%rbx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %rax
movq 0x42070(%rbx), %rcx
leaq -0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
testl $0xfffffff8, -0x8(%rcx) # imm = 0xFFFFFFF8
pushq $0x1
popq %rcx
cmoveq %rax, %rcx
shlq $0x4, %rcx
addq %rcx, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %rax
shlq $0x4, %rax
addq %rax, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswl 0x8(%r13), %edx
movq 0x42080(%rbx), %rsi
movq %rbx, %rdi
callq 0x2bc9
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %r13
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %r15
movq 0x42070(%rbx), %r14
movq 0x42080(%rbx), %r12
addq $-0x8, %r14
movq %r14, 0x42070(%rbx)
shlq $0x4, %r15
movq 0x18(%r12,%r15), %rdi
callq 0x281c
testl %eax, %eax
je 0x3ca7
leaq 0x28a0(%rip), %rsi      # 0x6514
movq %rbx, %rdi
callq 0x1f4e
movq 0x42068(%rbx), %r13
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
leaq (%r12,%r15), %rax
addq $0x18, %rax
movq (%r14), %rcx
movq %rcx, (%rax)
pushq $0x5b
popq %rax
leaq 0x8(%r14), %rcx
movq %rcx, 0x42070(%rbx)
movq %rax, (%r14)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswl 0x8(%r13), %ecx
movswq 0xa(%r13), %rax
movq 0x42088(%rbx), %rdx
testl %ecx, %ecx
jle 0x3cf2
decl %ecx
movq 0x10(%rdx), %rdx
jmp 0x3ce6
movq 0x20(%rdx,%rax,8), %rax
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswl 0x8(%r13), %edx
movswq 0xa(%r13), %rax
movq 0x42088(%rbx), %rcx
testl %edx, %edx
jle 0x3d38
decl %edx
movq 0x10(%rcx), %rcx
jmp 0x3d2c
movq 0x42070(%rbx), %rdx
movq -0x8(%rdx), %rsi
movq %rsi, 0x20(%rcx,%rax,8)
movq %rdx, 0x42070(%rbx)
movq $0x5b, -0x8(%rdx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %rax
movq 0x42070(%rbx), %rcx
movq 0x42088(%rbx), %rdx
movq 0x20(%rdx,%rax,8), %rax
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %rax
movq 0x42070(%rbx), %rcx
movq 0x42088(%rbx), %rdx
leaq -0x8(%rcx), %rsi
movq %rsi, 0x42070(%rbx)
movq -0x8(%rcx), %rcx
movq %rcx, 0x20(%rdx,%rax,8)
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswq 0x8(%r13), %rax
movq 0x10(%rbp,%rax,8), %rdx
movq 0x42088(%rbx), %rsi
movq %rbx, %rdi
callq 0x2c58
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %r13
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movslq 0x42078(%rbx), %rax
decq %rax
movl %eax, 0x42078(%rbx)
shlq $0x4, %rax
movq (%rsp), %rdx
movq (%rdx,%rax), %rcx
movq %rcx, 0x42088(%rbx)
movq 0x8(%rdx,%rax), %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movswl 0x8(%r13), %eax
movslq %eax, %r14
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %r15
movq %r15, 0x42070(%rbx)
movq -0x8(%rax), %r12
movq %r12, %rdi
callq 0x2605
testl %eax, %eax
je 0x3eb9
leaq (,%r14,8), %r13
subq %r13, %r15
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq *0x18(%r12)
movq 0x42070(%rbx), %rcx
subq %r13, %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %r13
jmpq *(%r13)
movq %r12, %rdi
callq 0x2cb6
testl %eax, %eax
je 0x3f4a
movq %rbp, 0x48(%rsp)
movq 0x18(%r12), %rbp
movq 0x42088(%rbx), %rax
movslq 0x42078(%rbx), %rcx
leal 0x1(%rcx), %edx
shlq $0x4, %rcx
movq (%rsp), %rsi
movq %rax, (%rsi,%rcx)
movq %r13, 0x8(%rsi,%rcx)
movl %edx, 0x42078(%rbx)
leaq (,%r14,8), %r13
subq %r13, %r15
movq 0x10(%r12), %rsi
movl 0x10(%rbp), %edx
movl 0x14(%rbp), %ecx
movq %rbx, %rdi
movl %r14d, %r8d
movq %r15, %r9
callq 0x4f5f
movq %rax, 0x42088(%rbx)
subq %r13, 0x42070(%rbx)
movslq 0x18(%rbp), %r13
movq 0x48(%rsp), %rbp
shlq $0x4, %r13
addq 0x8(%rsp), %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movq %r12, %rdi
callq 0x2662
testl %eax, %eax
je 0x407e
leaq 0x2360(%rip), %rdi      # 0x62c1
leaq 0x22b6(%rip), %rsi      # 0x621e
leaq 0x27d4(%rip), %rcx      # 0x6743
movl $0x9c0, %edx            # imm = 0x9C0
callq 0x10a0
movswl 0x8(%r13), %eax
movslq %eax, %r14
addq $0x10, %r13
movq %r13, 0x42068(%rbx)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %r15
movq %r15, 0x42070(%rbx)
movq -0x8(%rax), %r12
movq %r12, %rdi
callq 0x2605
testl %eax, %eax
je 0x4012
leaq (,%r14,8), %r13
subq %r13, %r15
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq *0x18(%r12)
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 (%rsp), %rdx
movq (%rdx,%rax), %rcx
movq %rcx, 0x42088(%rbx)
movq 0x8(%rdx,%rax), %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movq %r12, %rdi
callq 0x2cb6
testl %eax, %eax
je 0x4072
movq 0x10(%r12), %rsi
movq 0x18(%r12), %r12
leaq (,%r14,8), %r13
subq %r13, %r15
movl 0x10(%r12), %edx
movl 0x14(%r12), %ecx
movq %rbx, %rdi
movl %r14d, %r8d
movq %r15, %r9
callq 0x4f5f
movq %rax, 0x42088(%rbx)
subq %r13, 0x42070(%rbx)
movslq 0x18(%r12), %r13
shlq $0x4, %r13
addq 0x8(%rsp), %r13
movq %r13, 0x42068(%rbx)
jmpq *(%r13)
movq %r12, %rdi
callq 0x2662
testl %eax, %eax
jne 0x40aa
jmpq *(%r13)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x42070(%rbx)
movq -0x8(%rax), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2210(%rip), %rdi      # 0x62c1
leaq 0x2166(%rip), %rsi      # 0x621e
leaq 0x2684(%rip), %rcx      # 0x6743
movl $0x9d7, %edx            # imm = 0x9D7
callq 0x10a0