// File: /workspace/github_c_makefile_star2_O0/johnvtan[P]ccccc/asm.c
// Function: primary_to_instrs
static list_t *primary_to_instrs(primary_t *primary, env_t *env) {
    if (!primary) {
        UNREACHABLE("primary_to_instrs: wtf are you doing, primary is null\n");
    }

    debug("primary to instrs: %u\n", primary->type);

    if (primary->type == PRIMARY_INT) {
        list_t *ret = list_new();
        list_push(ret, instr_i2r(OP_MOV, primary->integer, REG_RAX));
        debug("int %u\n", primary->integer);
        return ret;
    }

    if (primary->type == PRIMARY_CHAR) {
        UNREACHABLE("primary_to_instrs: Got a char???\n");
    }

    if (primary->type == PRIMARY_EXPR) {
        return expr_to_instrs(primary->expr, env);
    }

    if (primary->type == PRIMARY_VAR) {
        // Do i just move the variable from its home to RAX?
        list_t *ret = list_new();
        debug("Got primary var: %s\n", string_get(primary->var));

        var_info_t *var_info = env_get_declared(env, primary->var);
        if (!var_info) {
            UNREACHABLE("Compilation error: variable referenced before declaration\n");
        }
        list_push(ret, instr_m2r(OP_MOV, var_info->home, REG_RAX));
        return ret;
    }

    if (primary->type == PRIMARY_FN_CALL) {
        // Parsing checks if this was declared before it was used, so assume this call is good
        debug("Got fn call for function %s\n", string_get(primary->fn_call->fn_name));
        list_t *ret = fn_caller_prepare(primary->fn_call->param_exprs, env);
        list_push(ret, instr_label(OP_CALL, primary->fn_call->fn_name));
        list_concat(ret, fn_caller_restore());

        // Return value should still be in RAX
        return ret;
    }

    UNREACHABLE("Unexpected primary expr\n");
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2e64
jmp 0x2e39
leaq 0x61c4(%rip), %rdi      # 0x9004
leaq 0x61f7(%rip), %rsi      # 0x903e
movl $0x13e, %edx            # imm = 0x13E
leaq 0x6453(%rip), %rcx      # 0x92a6
movb $0x0, %al
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
jmp 0x2e66
jmp 0x2e68
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x2eb1
callq 0x45f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq 0x8(%rax), %rsi
movl $0x1, %edx
movl %edx, %edi
callq 0x2810
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x46a0
jmp 0x2ea4
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3042
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x2ee7
jmp 0x2ebc
leaq 0x6141(%rip), %rdi      # 0x9004
leaq 0x6174(%rip), %rsi      # 0x903e
movl $0x14b, %edx            # imm = 0x14B
leaq 0x6407(%rip), %rcx      # 0x92dd
movb $0x0, %al
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x2f0a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x2a50
movq %rax, -0x8(%rbp)
jmp 0x3042
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x2fa3
callq 0x45f0
movq %rax, -0x28(%rbp)
jmp 0x2f22
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x4520
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x2f6b
jmp 0x2f40
leaq 0x60bd(%rip), %rdi      # 0x9004
leaq 0x60f0(%rip), %rsi      # 0x903e
movl $0x159, %edx            # imm = 0x159
leaq 0x63a5(%rip), %rcx      # 0x92ff
movb $0x0, %al
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movq 0x10(%rax), %rdx
movl $0x1, %ecx
movl %ecx, %edi
callq 0x4000
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x46a0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3042
movq -0x10(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x3015
jmp 0x2fae
jmp 0x2fb0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x40f0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movl $0x17, %edi
callq 0x2b90
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x46a0
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x4290
movq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x4740
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3042
jmp 0x3017
leaq 0x5fe6(%rip), %rdi      # 0x9004
leaq 0x6019(%rip), %rsi      # 0x903e
movl $0x16a, %edx            # imm = 0x16A
leaq 0x6309(%rip), %rcx      # 0x933a
movb $0x0, %al
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)