// File: /workspace/github_c_makefile_star2_O3/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 0x4399
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 0x440f
movq %rdi, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
movl $0x10, %eax
cmpq $0x28, %rax
ja 0x443c
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), %rcx
shlq $0x4, %rcx
addq %r15, %rcx
movq %rcx, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %r12
jmpq *(%rcx)
movq %rdi, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movl $0x10, %ecx
cmpq $0x28, %rcx
ja 0x4481
movl $0x10, %eax
addq 0x30(%rsp), %rax
addl $0x8, %ecx
movl %ecx, 0x20(%rsp)
jmp 0x448f
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), %rcx
shlq $0x4, %rcx
addq %r15, %rcx
movq %rcx, 0x42068(%rbx)
movq $0x3, 0x42088(%rbx)
leaq 0x4a090(%rbx), %r12
jmpq *(%rcx)
movq 0x28(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movl (%rax), %eax
leaq (%rax,%rax,2), %rax
leaq 0x5c44(%rip), %rcx      # 0xa0e0
leaq (%rcx,%rax,8), %rax
jmp 0x4a05
movswq 0x8(%rcx), %rax
movq 0x10(%r14,%rax,8), %rax
movq 0x42070(%rbx), %rdx
leaq 0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rax, (%rdx)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
addq $-0x8, 0x42070(%rbx)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rax
movq 0x42070(%rbx), %rdx
leaq -0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
testl $0xfffffff8, -0x8(%rdx) # imm = 0xFFFFFFF8
movl $0x1, %edx
cmoveq %rax, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswl 0x8(%rcx), %edx
movq 0x42080(%rbx), %rsi
movq %rbx, %rdi
callq 0x3268
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rcx
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rsi
movq 0x42070(%rbx), %rdx
movq 0x42080(%rbx), %rdi
leaq -0x8(%rdx), %rax
movq %rax, 0x42070(%rbx)
movq -0x8(%rdx), %rdx
shlq $0x4, %rsi
addq %rdi, %rsi
addq $0x18, %rsi
movq (%rsi), %rdi
movl %edi, %r8d
andl $0x7, %r8d
cmpq $0x3, %r8
je 0x45b4
testq %r8, %r8
jne 0x4602
movl (%rdi), %edi
jmp 0x45b7
sarl $0x3, %edi
cmpl $0x9, %edi
jne 0x4602
movl $0x18, %edx
movq %rbx, %rdi
movl $0x16, %esi
callq 0x2122
leaq 0x2167(%rip), %rcx      # 0x673c
movq %rcx, 0x10(%rax)
movq 0x42068(%rbx), %rcx
movq 0x42070(%rbx), %rdx
leaq 0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rax, (%rdx)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movq %rdx, (%rsi)
movl $0x5b, %edx
leaq 0x8(%rax), %rsi
movq %rsi, 0x42070(%rbx)
movq %rdx, (%rax)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswl 0x8(%rcx), %esi
movswq 0xa(%rcx), %rax
movq 0x42088(%rbx), %rdx
testl %esi, %esi
jle 0x4646
incl %esi
movq 0x10(%rdx), %rdx
decl %esi
cmpl $0x1, %esi
ja 0x463b
movq 0x20(%rdx,%rax,8), %rax
movq 0x42070(%rbx), %rdx
leaq 0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rax, (%rdx)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswl 0x8(%rcx), %esi
movswq 0xa(%rcx), %rax
movq 0x42088(%rbx), %rdx
testl %esi, %esi
jle 0x468e
incl %esi
movq 0x10(%rdx), %rdx
decl %esi
cmpl $0x1, %esi
ja 0x4683
movq 0x42070(%rbx), %rsi
movq -0x8(%rsi), %rdi
movq %rdi, 0x20(%rdx,%rax,8)
movq %rsi, 0x42070(%rbx)
movq $0x5b, -0x8(%rsi)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rax
movq 0x42070(%rbx), %rdx
movq 0x42088(%rbx), %rsi
movq 0x20(%rsi,%rax,8), %rax
leaq 0x8(%rdx), %rsi
movq %rsi, 0x42070(%rbx)
movq %rax, (%rdx)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rax
movq 0x42070(%rbx), %rdx
movq 0x42088(%rbx), %rsi
leaq -0x8(%rdx), %rdi
movq %rdi, 0x42070(%rbx)
movq -0x8(%rdx), %rdx
movq %rdx, 0x20(%rsi,%rax,8)
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswq 0x8(%rcx), %rax
movq 0x10(%r14,%rax,8), %rdx
movq 0x42088(%rbx), %rsi
movq %rbx, %rdi
callq 0x333f
movq 0x42070(%rbx), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x42070(%rbx)
movq %rax, (%rcx)
movq 0x42068(%rbx), %rcx
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%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), %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
movswl 0x8(%rcx), %eax
movslq %eax, %r8
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %r9
movq %r9, 0x42070(%rbx)
movq -0x8(%rax), %rax
movl %eax, %edx
andl $0x7, %edx
cmpq $0x3, %rdx
je 0x47d0
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %esi
jmp 0x47d5
movl %eax, %esi
sarl $0x3, %esi
cmpl $0xe, %esi
jne 0x4812
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), %rcx
jmpq *(%rcx)
cmpl $0x3, %edx
je 0x4824
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %esi
jmp 0x4829
movl %eax, %esi
sarl $0x3, %esi
cmpl $0x8, %esi
jne 0x4897
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 0x5e0f
movq %rax, 0x42088(%rbx)
subq %rbp, 0x42070(%rbx)
movslq 0x18(%r13), %rcx
shlq $0x4, %rcx
addq %r15, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
cmpl $0x3, %edx
je 0x48a9
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %eax
jmp 0x48ac
sarl $0x3, %eax
cmpl $0x13, %eax
jne 0x49ed
leaq 0x1c2d(%rip), %rdi      # 0x64e9
leaq 0x1b83(%rip), %rsi      # 0x6446
leaq 0x20a1(%rip), %rcx      # 0x696b
movl $0x9c0, %edx            # imm = 0x9C0
callq 0x10a0
movswl 0x8(%rcx), %eax
movslq %eax, %r8
addq $0x10, %rcx
movq %rcx, 0x42068(%rbx)
movq 0x42070(%rbx), %rax
leaq -0x8(%rax), %r9
movq %r9, 0x42070(%rbx)
movq -0x8(%rax), %rax
movl %eax, %edx
andl $0x7, %edx
cmpq $0x3, %rdx
je 0x4914
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %esi
jmp 0x4919
movl %eax, %esi
sarl $0x3, %esi
cmpl $0xe, %esi
jne 0x497a
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), %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
cmpl $0x3, %edx
je 0x4988
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %esi
jmp 0x498d
movl %eax, %esi
sarl $0x3, %esi
cmpl $0x8, %esi
jne 0x49d7
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 0x5e0f
movq %rax, 0x42088(%rbx)
subq %rbp, 0x42070(%rbx)
movslq 0x18(%r13), %rcx
shlq $0x4, %rcx
addq %r15, %rcx
movq %rcx, 0x42068(%rbx)
jmpq *(%rcx)
cmpl $0x3, %edx
je 0x49e5
testq %rdx, %rdx
jne 0x49ed
movl (%rax), %eax
jmp 0x49e8
sarl $0x3, %eax
cmpl $0x13, %eax
je 0x4a17
jmpq *(%rcx)
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 0x1acb(%rip), %rdi      # 0x64e9
leaq 0x1a21(%rip), %rsi      # 0x6446
leaq 0x1f3f(%rip), %rcx      # 0x696b
movl $0x9d7, %edx            # imm = 0x9D7
callq 0x10a0