// File: /workspace/github_c_makefile_star2_O2/alexj136[P]minijit/src/icodegen.c
// Function: icodegen_Expr
ICodeOperationVector *icodegen_Expr(Prog *prog, Expr *expr, int *next_label,
        int *func_labels) {

    BEGIN_CODE_GENERATION;

    if(Expr_isInt(expr)) {

        // Just load the literal value into the accumulator
        INSERT_OPERATION(LOADIMM    , Int_value(expr)   , ACCUMULATOR   );
    }
    else if(Expr_isAdd(expr) || Expr_isSub(expr)) {

        // Evaluate the RHS
        if(Expr_isAdd(expr)) {
            INSERT_EXPR_CODE(Add_rhs(expr));
        }
        else /* if(Expr_isSub(expr)) */ {
            INSERT_EXPR_CODE(Sub_rhs(expr));
        }

        // Push the RHS result on the stack
        INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Evaluate the LHS
        INSERT_EXPR_CODE(Add_lhs(expr));

        // Pop the RHS into temporary register
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , TEMPORARY     );

        // Do the operation
        if(Expr_isAdd(expr)) {
            INSERT_OPERATION(ADD        , ACCUMULATOR       , TEMPORARY     );
        }
        else /* if(Expr_isSub(expr)) */ {
            INSERT_OPERATION(SUB        , ACCUMULATOR       , TEMPORARY     );
        }
    }
    else if(Expr_isCall(expr)) {

        // Determine the label number to jump to in the call
        int callee_label = func_labels[Call_name(expr)];
        int num_args     = Call_num_args(expr);
        int num_vars     = Func_num_vars(Prog_func(prog, Call_name(expr)));
        int idx;

        // Push the current frame pointer on the stack
        INSERT_OPERATION(STORE      , FRAME_POINTER     , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Push the current return address on the stack
        INSERT_OPERATION(STORE      , RETURN_ADDRESS    , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Add zeros to the stack for the non-argument variables in the callee
        for(idx = num_vars - 1; idx >= num_args; idx--) {

            INSERT_OPERATION(LOADIMM    , 0                 , ACCUMULATOR   );
            INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
            INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
            INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );
        }

        // For each argument expression, evaluate and push the result on the
        // stack
        for(idx = num_args - 1; idx >= 0; idx--) {

            // Evaluate the idxth argument expression
            INSERT_EXPR_CODE(Call_arg(expr, idx));

            // Push the result on the stack
            INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
            INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
            INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );
        }

        // Set the frame pointer for the callee (one less than the stack
        // pointer)
        INSERT_OPERATION(MOVE       , STACK_POINTER     , FRAME_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , FRAME_POINTER     , TEMPORARY     );

        // Jump to the function
        INSERT_OPERATION(JUMPLINK   , callee_label      , 0             );

        // Pop the arguments and variables off the stack
        INSERT_OPERATION(LOADIMM    , num_vars          , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );

        // Restore the return address
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , RETURN_ADDRESS);

        // Restore the frame pointer
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , FRAME_POINTER );
    }
    else if(Expr_isVar(expr)) {

        // Calculate the address from which to load
        INSERT_OPERATION(LOADIMM    , Var_name(expr)    , ACCUMULATOR   );
        INSERT_OPERATION(MOVE       , FRAME_POINTER     , TEMPORARY     );
        INSERT_OPERATION(SUB        , TEMPORARY         , ACCUMULATOR   );

        // Perform the load operation with the calculated address
        INSERT_OPERATION(LOAD       , TEMPORARY         , ACCUMULATOR   );
    }
    else {
        ERROR("Expr type not recognised.");
    }

    RETURN_GENERATED_CODE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f68, %rsp           # imm = 0x1F68
movq %rcx, -0x40(%rbp)
movq %rdx, -0x50(%rbp)
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x134fa
movq %rax, %rbx
movq %r14, -0x30(%rbp)
movl (%r14), %eax
cmpq $0x4, %rax
ja 0x14c7a
leaq 0x6f4d(%rip), %rcx      # 0x1b754
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq -0x50(%rbp), %r12
movq %r12, %rdx
movq -0x40(%rbp), %r13
movq %r13, %rcx
callq 0x147bf
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x136c9
movq %r14, %rdi
callq 0x13c48
pushq $0x2
popq %rdi
pushq $0x4
popq %r14
movl %r14d, %esi
xorl %edx, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x5
popq %rdi
pushq $0x1
popq %rsi
movl %r14d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x3
popq %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movq -0x30(%rbp), %r14
movq 0x8(%r14), %rsi
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x147bf
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0x136c9
movq %r13, %rdi
callq 0x13c48
pushq $0x5
popq %rdi
pushq $0x1
popq %r12
movl %r12d, %esi
movl %edi, %edx
movl %edi, %r15d
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x4
popq %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r12d, %edi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
cmpl $0x1, (%r14)
jne 0x14c4d
pushq $0x3
popq %rdi
pushq $0x4
popq %rsi
pushq $0x5
jmp 0x14998
movq -0x30(%rbp), %rax
movl 0x4(%rax), %esi
pushq $0x5
popq %r14
pushq $0x4
popq %r15
movl %r14d, %edi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x1
popq %r12
xorl %edi, %edi
movl %r12d, %esi
movl %r14d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r15d, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r12d, %edi
movl %r14d, %esi
movl %r15d, %edx
jmp 0x14c55
movq -0x30(%rbp), %rax
movl 0x4(%rax), %esi
pushq $0x5
popq %rdi
pushq $0x4
popq %rdx
jmp 0x14c55
movq -0x30(%rbp), %r14
movslq 0x4(%r14), %rax
movq -0x40(%rbp), %rcx
movl (%rcx,%rax,4), %eax
movl %eax, -0x48(%rbp)
movq 0x18(%r14), %rdi
callq 0x9b87
movl %eax, %r12d
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movl 0x4(%r14), %esi
callq 0x86d9
movq 0x8(%rax), %rdi
callq 0x7537
movl %eax, %r14d
pushq $0x2
popq %rdi
pushq $0x1
popq %r15
movl %r15d, %esi
xorl %edx, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x5
popq %rdi
pushq $0x4
popq %r13
movl %r15d, %esi
movl %r13d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r14d, -0x44(%rbp)
incl %r14d
pushq $0x3
popq %rdi
pushq $0x2
popq %r15
xorl %esi, %esi
movl %r13d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x2
popq %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x5
popq %r15
movl %r15d, %edi
pushq $0x1
popq %rsi
movl %r13d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x3
popq %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
decl %r14d
movl %r15d, %edi
movl %r13d, %r15d
cmpl %r12d, %r14d
jg 0x14a1f
pushq $0x4
popq %r15
pushq $0x3
popq %r13
testl %r12d, %r12d
jle 0x14b2b
decl %r12d
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movl %r12d, %esi
callq 0x9e11
movq -0x38(%rbp), %rdi
movq %rax, %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x147bf
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x136c9
movq %r14, %rdi
callq 0x13c48
pushq $0x2
popq %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x5
popq %rdi
pushq $0x1
popq %rsi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r13d, %edi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
jmp 0x14a97
pushq $0x1
popq %r14
xorl %edi, %edi
xorl %esi, %esi
movl %r14d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x5
popq %r15
movl %r15d, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x4
popq %r12
movl %r12d, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x8
popq %rdi
movl -0x48(%rbp), %esi
xorl %edx, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r15d, %edi
movl -0x44(%rbp), %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r12d, %edi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r15d, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r12d, %edi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
pushq $0x2
popq %rdx
movl %r14d, %edi
xorl %esi, %esi
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r15d, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r12d, %edi
xorl %esi, %esi
movl %r15d, %edx
callq 0x13028
movq %rbx, %rdi
movq %rax, %rsi
callq 0x135dc
movl %r14d, %edi
xorl %esi, %esi
movl %r14d, %edx
jmp 0x14c55
pushq $0x4
popq %rdi
pushq $0x5
popq %rdx
movl %edi, %esi
callq 0x13028
movq %rax, %rsi
movq %rbx, %rdi
callq 0x135dc
movq %rbx, %rax
addq $0x1f68, %rsp           # imm = 0x1F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5383(%rip), %rdi      # 0x1a004
leaq 0x6ae0(%rip), %rsi      # 0x1b768
leaq 0x6af6(%rip), %rcx      # 0x1b785
movl $0x105, %edx            # imm = 0x105
xorl %eax, %eax
callq 0x6100
leaq 0x538a(%rip), %rbx      # 0x1a02c
leaq 0x573d(%rip), %rsi      # 0x1a3e6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
leaq -0x1f90(%rbp), %r15
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60e0
movl %eax, %r14d
movq %r15, %rdi
movl %eax, %esi
callq 0x60b0
movq %rax, %r15
leaq 0x5538(%rip), %rdi      # 0x1a216
callq 0x6070
testl %r14d, %r14d
cmovlel %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x14d05
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
jmp 0x14ced
pushq $0x1
popq %rdi
callq 0x61a0
nopl (%rax)