// File: /workspace/github_c_makefile_star2_O1/yutopp[P]cc/cc.c
// Function: cc_compile
int cc_compile(CC* cc, Node* node) {
    int err = 0;
    IRModule* ir_mod = cc_build_ir(cc, node);

    ASM_X86_64* asm_x86_64 = asm_x86_64_new(ir_mod);

    fprintf(DEBUGOUT, "= ASM =\n");
    asm_x86_64_fprint(DEBUGOUT, asm_x86_64);
    fprintf(DEBUGOUT, "\n");
    fflush(DEBUGOUT);

    // TODO: fix
    char spath[16] = "/tmp/ccXXXXXX.s";
    int fd = mkstemps(spath, 2);
    if (fd == -1) {
        // TODO: fix
        fprintf(stderr, "Failed to open asm file: \n");
        err = 1;
        goto exit;
    }
    FILE* fp = fdopen(fd, "wb");
    asm_x86_64_fprint(fp, asm_x86_64);
    fclose(fp);

    char opath[16];
    memcpy(opath, spath, 16);
    opath[14] = 'o';

    char cmd[1024];
    int n = snprintf(cmd, 1024, "as %s -o %s", spath, opath);
    if (n < 0 || n >= 1024) {
        // TODO: fix
        fprintf(stderr, "Failed to write cmd: \n");
        err = 1;
        goto exit;
    }
    int cmd_res = system(cmd);
    if (cmd_res != 0) {
        // TODO: fix
        fprintf(stderr, "FAILED: as");
        err = 1;
        goto exit;
    }

    n = snprintf(cmd, 1024, "gcc %s -o a.out", opath);
    if (n < 0 || n >= 1024) {
        // TODO: fix
        fprintf(stderr, "Failed to write cmd: \n");
        err = 1;
        goto exit;
    }
    cmd_res = system(cmd);
    if (cmd_res != 0) {
        // TODO: fix
        fprintf(stderr, "FAILED: gcc");
        err = 1;
        goto exit;
    }

exit:
    asm_x86_64_drop(asm_x86_64);
    return err;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x420, %rsp            # imm = 0x420
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
callq 0x39bf
movq %rax, 0x38(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x39d7
movq %rax, 0x40(%rbx)
movq 0x4ad5(%rip), %r14      # 0xafd8
movq (%r14), %rcx
leaq 0x1a82(%rip), %rdi      # 0x7f8f
movl $0x7, %esi
movl $0x1, %edx
callq 0x11b0
movq (%r14), %rdi
movq 0x40(%rbx), %rsi
callq 0x3b66
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rdi
callq 0x1160
movq 0x40(%rbx), %rdi
callq 0x4c48
movq %rax, %rbx
movq (%r14), %rcx
leaq 0x19b4(%rip), %rdi      # 0x7f07
movl $0x8, %esi
movl $0x1, %edx
callq 0x11b0
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x5113
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rdi
callq 0x1160
movaps 0x1a17(%rip), %xmm0     # 0x7fa0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x2, %esi
callq 0x1030
cmpl $-0x1, %eax
je 0x6642
leaq 0x1980(%rip), %rsi      # 0x7f2b
movl %eax, %edi
callq 0x1180
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5113
movq %r14, %rdi
callq 0x1090
leaq 0x10(%rsp), %rcx
movaps (%rcx), %xmm0
movq %rsp, %r8
movaps %xmm0, (%r8)
movb $0x6f, 0xe(%r8)
leaq 0x194b(%rip), %rdx      # 0x7f2e
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x10c0
cmpl $0x400, %eax            # imm = 0x400
jae 0x6649
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
jne 0x6650
leaq 0x194c(%rip), %rdx      # 0x7f5c
xorl %ebp, %ebp
leaq 0x20(%rsp), %rdi
movq %rsp, %rcx
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x10c0
cmpl $0x400, %eax            # imm = 0x400
jae 0x6657
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
je 0x6661
callq 0x1483
jmp 0x665c
callq 0x14e9
jmp 0x665c
callq 0x14c7
jmp 0x665c
callq 0x1461
jmp 0x665c
callq 0x14a5
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x5092
movl %ebp, %eax
addq $0x420, %rsp            # imm = 0x420
popq %rbx
popq %r14
popq %rbp
retq