// File: /workspace/github_c_makefile_star2_O3/theandrew168[P]squeaky/src/mce.c
// Function: mce_eval
struct value*
mce_eval(struct vm* vm, struct value* exp, struct value* env)
{
tailcall:

    if (is_self_evaluating(exp)) {
        return exp;
    } else if (is_variable(exp)) {
        return env_lookup(vm, exp, env);
    } else if (is_quoted(exp)) {
        return text_of_quotation(exp);
    } else if (is_assignment(exp)) {
        return eval_assignment(vm, exp, env);
    } else if (is_definition(exp)) {
        return eval_definition(vm, exp, env);
    } else if (is_if(exp)) {
        exp = eval_if(vm, exp, env);
        goto tailcall;
    } else if (is_environment(exp)) {
        return env;
    } else if (is_load(exp)) {
        return load(vm, load_args(exp), env);
    } else if (is_gc(exp)) {
        vm_gc(vm, env);
        return vm_make_empty_list(vm);
    } else if (is_lambda(exp)) {
        // TODO: check for the three different lambda forms:
        // (lambda (x) (* x x))
        // (lambda x x)
        // (lambda (x . rest) (append x rest))
        return vm_make_lambda(vm, lambda_params(exp), lambda_body(exp), env);
    } else if (is_application(exp)) {
        // 'apply' is evalutaed inline for TCO
        struct value* proc = mce_eval(vm, operator(exp), env);
        struct value* args = list_of_values(vm, operands(exp), env);

        // handle builtin 'eval' specifically for TCO
        if (is_primitive_proc(proc) && proc->as.builtin == mce_builtin_eval) {
            ASSERT_ARITY("eval", args, 2);
            ASSERT_TYPE("eval", args, 1, VALUE_PAIR);
            env = eval_env(args);
            exp = eval_exp(args);
            goto tailcall;
        }

        // handle builtin 'apply' specifically for TCO
        if (is_primitive_proc(proc) && proc->as.builtin == mce_builtin_apply) {
            proc = apply_operator(args);
            args = apply_operands(args);
        }

        if (is_primitive_proc(proc)) {
            return proc->as.builtin(vm, args);
        } else if (is_compound_proc(proc)) {
            // evaluate the lambda's body in the current stack frame (for TCO)
            env = env_extend(vm, proc->as.lambda.params, args, proc->as.lambda.env);
            exp = proc->as.lambda.body;
            while (!is_last_exp(exp)) {
                mce_eval(vm, first_exp(exp), env);
                exp = rest_exps(exp);
            }
            exp = first_exp(exp);
            goto tailcall;
        } else {
            fprintf(stderr, "runtime error (invalid proc) at: TODO\n");
            exit(EXIT_FAILURE);
        }
    }

    fprintf(stderr, "runtime error (invalid expr) at: TODO\n");
    exit(EXIT_FAILURE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x3065(%rip), %r15      # 0x8bbc
leaq 0x2d66(%rip), %r12      # 0x88c4
movl (%r14), %eax
leal -0x2(%rax), %ecx
cmpl $0x4, %ecx
jb 0x5e18
cmpl $0x6, %eax
je 0x5df2
movq %r14, %rdi
movq %r15, %rsi
callq 0x60d5
testb %al, %al
jne 0x5e0e
movq %r14, %rdi
movq %r12, %rsi
callq 0x60d5
movq %r14, %rdi
testb %al, %al
jne 0x5e2a
leaq 0x2d23(%rip), %rsi      # 0x88c9
callq 0x60d5
movq %r14, %rdi
testb %al, %al
jne 0x5e7f
leaq 0x2d13(%rip), %rsi      # 0x88d0
callq 0x60d5
movq %r14, %rdi
testb %al, %al
je 0x5c4f
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
movq %rax, %rdi
callq 0x69cc
movl %eax, %r13d
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
testb %r13b, %r13b
jne 0x5c3f
movq %rax, %rdi
callq 0x5abc
cmpl $0x1, (%rax)
jne 0x5c27
movq %rbx, %rdi
xorl %esi, %esi
callq 0x70c3
movq %rax, %r14
jmp 0x5b5e
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %r14
jmp 0x5b5e
leaq 0x2c7d(%rip), %rsi      # 0x88d3
callq 0x60d5
testb %al, %al
jne 0x5f86
movq %r14, %rdi
leaq 0x2c80(%rip), %rsi      # 0x88ed
callq 0x60d5
testb %al, %al
jne 0x5f86
movq %r14, %rdi
leaq 0x2c7a(%rip), %rsi      # 0x88fe
callq 0x60d5
testb %al, %al
jne 0x5f86
movq %r14, %rdi
leaq 0x2555(%rip), %rsi      # 0x81f0
callq 0x60d5
testb %al, %al
jne 0x5f8e
movq %r14, %rdi
leaq 0x2c64(%rip), %rsi      # 0x8916
callq 0x60d5
testb %al, %al
jne 0x601d
movq %r14, %rdi
leaq 0x2c50(%rip), %rsi      # 0x8919
callq 0x60d5
testb %al, %al
jne 0x603e
cmpl $0x7, (%r14)
jne 0x609f
movq %r14, %rdi
callq 0x5b2b
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
movq %rax, %r13
movq %r14, %rdi
callq 0x5abc
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x60f9
movq %rax, %r14
movl (%r13), %eax
cmpl $0x8, %eax
jne 0x5d59
movq 0x10(%r13), %rax
leaq 0x440(%rip), %rcx       # 0x6166
cmpq %rcx, %rax
je 0x5daf
leaq 0x45b(%rip), %rcx       # 0x6191
cmpq %rcx, %rax
jne 0x6085
movq %r14, %rdi
callq 0x5b2b
movq %rax, %r13
movq %r14, %rdi
callq 0x5abc
movq %rax, %r14
movl (%r13), %eax
cmpl $0x9, %eax
jne 0x6080
movq 0x10(%r13), %rsi
movq 0x20(%r13), %rcx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x5675
movq %rax, %rbp
movq 0x18(%r13), %r14
movq %r14, %rdi
callq 0x5abc
cmpl $0x1, (%rax)
je 0x5dea
movq %r14, %rdi
callq 0x5b2b
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
movq %r14, %rdi
callq 0x5abc
movq %rax, %r14
movq %rax, %rdi
jmp 0x5d7f
movq %r14, %rdi
callq 0x5a98
cmpq $0x2, %rax
jne 0x60a9
movl $0x1, %esi
movq %r14, %rdi
callq 0x5acc
cmpl $0x7, (%rax)
jne 0x60b1
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %rbp
movq %r14, %rdi
jmp 0x5c42
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x56e6
cmpl $0x1, (%r14)
jne 0x5ee2
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %r15
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbp, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5816
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movl (%rax), %r12d
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %r15
cmpl $0x6, %r12d
je 0x5eb3
movq %r15, %rdi
callq 0x5b2b
movq %rax, %r15
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
cmpl $0x6, (%rax)
jne 0x5f00
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
jmp 0x5f59
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5b2b
leaq 0x2a12(%rip), %rsi      # 0x8919
movq %rbx, %rdi
callq 0x71ce
movq %rax, %r12
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %rdi
callq 0x5abc
movq %rax, %r13
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x7232
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x7232
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbp, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5991
movq %rbp, %r14
jmp 0x5e18
movq %r14, %rdi
callq 0x5abc
movq %rax, %r14
movq %rax, %rdi
callq 0x5a98
cmpq $0x1, %rax
jne 0x60b9
movq %r14, %rdi
xorl %esi, %esi
callq 0x5acc
cmpl $0x5, (%rax)
jne 0x60c1
movq %r14, %rdi
xorl %esi, %esi
callq 0x5acc
movq %rax, %r15
movq 0x10(%rax), %rdi
leaq 0x2305(%rip), %rsi      # 0x82db
callq 0x1190
testq %rax, %rax
je 0x60c9
movq %rax, %r14
movq %r14, %rdi
callq 0x1230
testl %eax, %eax
jne 0x6013
movq %rbx, %rdi
movq %r14, %rsi
callq 0x675f
cmpl $0xe, (%rax)
je 0x6013
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x5b3c
jmp 0x5fe7
movq %r14, %rdi
callq 0x1160
jmp 0x6028
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6f11
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7071
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5b2b
movq %rax, %r15
movq %r14, %rdi
callq 0x5abc
movq %rax, %rdi
callq 0x5abc
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbp, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x72b4
cmpl $0x8, %eax
jne 0x60a4
movq 0x10(%r13), %rax
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
callq 0x2ca2
callq 0x2cc7
movq %r14, %rdi
callq 0x2cec
movq %r14, %rdi
callq 0x2d22
movq %r14, %rdi
callq 0x2d7b
movq %r14, %rdi
callq 0x2db4
addq $0x10, %r15
movq %r15, %rdi
callq 0x2e0a