// File: /workspace/github_c_makefile_star2_O1/alexj136[P]minijit/src/interpreter.c
// Function: interpret_Expr
InterpretResult *interpret_Expr(Prog *prog, Expr *expr, int *store) {
    if(Expr_isInt(expr)) {
        return InterpretSuccess_init(Int_value(expr));
    }
    else if(Expr_isAdd(expr) || Expr_isSub(expr)) {
        InterpretResult *lhs;
        InterpretResult *rhs;

        if(Expr_isAdd(expr)) {
            lhs = interpret_Expr(prog, Add_lhs(expr), store);
            rhs = interpret_Expr(prog, Add_rhs(expr), store);
        }
        else /* if(Expr_isSub(expr)) */ {
            lhs = interpret_Expr(prog, Sub_lhs(expr), store);
            rhs = interpret_Expr(prog, Sub_rhs(expr), store);
        }

        if((lhs->type == iSuccess) && (rhs->type == iSuccess)) {
            int lhs_val = lhs->result;
            int rhs_val = rhs->result;

            free(lhs);
            free(rhs);

            return InterpretSuccess_init(
                    lhs_val + (Expr_isSub(expr) ? -rhs_val : rhs_val));
        }

        else if(lhs->type != iSuccess) {
            free(rhs);
            return lhs;
        }

        else /* if(rhs->type != iSuccess) */ {
            free(lhs);
            return rhs;
        }
    }
    else if(Expr_isCall(expr)) {

        Func *callee = Prog_func(prog, Call_name(expr));

        if(callee == NULL) {
            return InterpretFailFunctionNotFound_init(Expr_src_line_no(expr),
                    Expr_src_char_no(expr));
        }

        if(Func_num_args(callee) != Call_num_args(expr)) {
            return InterpretFailIncorrectNumArgs_init(Expr_src_line_no(expr),
                    Expr_src_char_no(expr));
        }

        int *callee_store = challoc(sizeof(int) * Func_num_vars(callee));
        bool failed_arg_eval = false;
        InterpretResult *arg_res;

        // Evaluate each argument expression. Stop early if we encounter an
        // error.
        int idx;
        for(idx = 0; idx < Call_num_args(expr); idx++) {

            arg_res = interpret_Expr(prog, Call_arg(expr, idx), store);
            callee_store[idx] = arg_res->result;

            if(arg_res->type != iSuccess) {
                failed_arg_eval = true;
                break;
            }

            free(arg_res);
        }

        // If an error was encountered in argument evaluation, clean up and
        // return an error as the result of the entire call
        if(failed_arg_eval) {
            free(callee_store);
            return arg_res;
        }

        // Argument evaluation succeeded, so write zero in place for the initial
        // values of the other variables in the callee's store
        for(idx = Func_num_args(callee); idx < Func_num_vars(callee); idx++) {
            callee_store[idx] = 0;
        }

        // Make the call, free the argument array, and return the call result
        InterpretResult *call_res = interpret_Func(prog, callee, callee_store);
        free(callee_store);
        return call_res;
    }
    else if(Expr_isVar(expr)) {
        return InterpretSuccess_init(store[Var_name(expr)]);
    }
    else {
        ERROR("Expr type not recognised.");
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f58, %rsp           # imm = 0x1F58
movl (%rsi), %eax
cmpq $0x4, %rax
ja 0x13a63
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8c3d(%rip), %rcx      # 0x1c430
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x137c3
movq %rax, %r13
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x137c3
movq %rax, %rbx
movl (%r13), %eax
testl %eax, %eax
je 0x1393c
movq %rbx, %rdi
callq 0x6030
jmp 0x139ec
movslq 0x4(%r14), %rax
movl (%r12,%rax,4), %ebx
jmp 0x139c0
movl 0x4(%r14), %ebx
jmp 0x139c0
movq (%rbx), %rdi
movl 0x4(%r14), %esi
callq 0x883f
testq %rax, %rax
je 0x13959
movq %rax, %r13
movl 0x4(%rax), %r15d
movq 0x18(%r14), %rdi
callq 0x9eba
cmpl %eax, %r15d
jne 0x13973
movq %r13, -0x40(%rbp)
movq 0x8(%r13), %rdi
callq 0x75ec
leal (,%rax,4), %edi
callq 0x62ec
movq %rax, %r15
movq 0x18(%r14), %rdi
callq 0x9eba
testl %eax, %eax
setg -0x29(%rbp)
movq %r15, -0x38(%rbp)
jle 0x13a01
movq 0x18(%r14), %rdi
xorl %esi, %esi
callq 0xa14b
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x137c3
movq %rax, %r13
movl 0x4(%rax), %eax
movl %eax, (%r15)
cmpl $0x0, (%r13)
jne 0x13a01
movl $0x1, %r15d
movq %r13, %rdi
callq 0x6030
movq 0x18(%r14), %rdi
callq 0x9eba
cltq
cmpq %rax, %r15
setl -0x29(%rbp)
jge 0x13a01
movq 0x18(%r14), %rdi
movl %r15d, %esi
callq 0xa14b
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x137c3
movq %rax, %r13
movl 0x4(%rax), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
cmpl $0x0, (%r13)
je 0x138e5
jmp 0x13a01
cmpl $0x0, (%rbx)
je 0x13998
testl %eax, %eax
jne 0x1382c
movq %r13, %rdi
callq 0x6030
movq %rbx, %r13
jmp 0x139ec
movq 0x20(%r14), %rbx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r13
movq $0x2, (%rax)
jmp 0x1398b
movq 0x20(%r14), %rbx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r13
movq $0x1, (%rax)
movl $0x0, 0x8(%rax)
movq %rbx, 0xc(%rax)
jmp 0x139ec
movl 0x4(%r13), %r15d
movl 0x4(%rbx), %r12d
movq %r13, %rdi
callq 0x6030
movq %rbx, %rdi
callq 0x6030
movl %r12d, %ebx
negl %ebx
cmpl $0x2, (%r14)
cmovnel %r12d, %ebx
addl %r15d, %ebx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r13
movl $0x0, (%rax)
movl %ebx, 0x4(%rax)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x8(%r13)
movl $0xffffffff, 0x10(%r13) # imm = 0xFFFFFFFF
movq %r13, %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, -0x29(%rbp)
je 0x13a10
movq -0x38(%rbp), %rdi
jmp 0x1382f
movq -0x40(%rbp), %r15
movslq 0x4(%r15), %r14
movq 0x8(%r15), %rdi
callq 0x75ec
cmpl %eax, %r14d
movq -0x38(%rbp), %r12
jge 0x13a45
movl $0x0, (%r12,%r14,4)
incq %r14
movq 0x8(%r15), %rdi
callq 0x75ec
cltq
cmpq %rax, %r14
jl 0x13a2a
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x13afb
movq %rax, %rbx
movq %r12, %rdi
callq 0x6030
movq %rbx, %rax
jmp 0x139ef
leaq 0x759a(%rip), %rdi      # 0x1b004
leaq 0x89d3(%rip), %rsi      # 0x1c444
leaq 0x89ed(%rip), %rcx      # 0x1c465
movl $0xf3, %edx
xorl %eax, %eax
callq 0x6100
leaq 0x75a1(%rip), %rdi      # 0x1b02c
leaq 0x7944(%rip), %rsi      # 0x1b3d6
xorl %eax, %eax
callq 0x6100
leaq -0x1f80(%rbp), %rbx
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60e0
movl %eax, %r14d
movq %rbx, %rdi
movl %eax, %esi
callq 0x60b0
movq %rax, %rbx
leaq 0x7752(%rip), %rdi      # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x13af1
movl %r14d, %r15d
leaq 0x7554(%rip), %r14      # 0x1b02c
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
cmpq %r12, %r15
jne 0x13adb
movl $0x1, %edi
callq 0x61a0