// File: /workspace/github_c_makefile_star2_O3/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 0x3693
movq %rax, 0x38(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x36a7
movq %rax, 0x40(%rbx)
movq 0x4de1(%rip), %r14      # 0xafd8
movq (%r14), %rcx
leaq 0x1d9e(%rip), %rdi      # 0x7f9f
movl $0x7, %esi
movl $0x1, %edx
callq 0x11c0
movq (%r14), %rdi
movq 0x40(%rbx), %rsi
callq 0x3836
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rdi
callq 0x1170
movq 0x40(%rbx), %rdi
callq 0x4914
movq %rax, %rbx
movq (%r14), %rcx
leaq 0x1cd0(%rip), %rdi      # 0x7f17
movl $0x8, %esi
movl $0x1, %edx
callq 0x11c0
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x4e12
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rdi
callq 0x1170
movaps 0x1d33(%rip), %xmm0     # 0x7fb0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x2, %esi
callq 0x1030
cmpl $-0x1, %eax
je 0x6336
leaq 0x1c9c(%rip), %rsi      # 0x7f3b
movl %eax, %edi
callq 0x1190
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4e12
movq %r14, %rdi
callq 0x1090
leaq 0x10(%rsp), %rcx
movaps (%rcx), %xmm0
movq %rsp, %r8
movaps %xmm0, (%r8)
movb $0x6f, 0xe(%r8)
leaq 0x1c67(%rip), %rdx      # 0x7f3e
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x10c0
cmpl $0x400, %eax            # imm = 0x400
jae 0x633d
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
jne 0x6344
leaq 0x1c68(%rip), %rdx      # 0x7f6c
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 0x634b
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
je 0x6355
callq 0x1493
jmp 0x6350
callq 0x14f9
jmp 0x6350
callq 0x14d7
jmp 0x6350
callq 0x1471
jmp 0x6350
callq 0x14b5
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x4d91
movl %ebp, %eax
addq $0x420, %rsp            # imm = 0x420
popq %rbx
popq %r14
popq %rbp
retq