// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
callq 0x306e
movq %rax, 0x38(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x307e
movq %rax, 0x40(%rbx)
movq 0x5497(%rip), %r15      # 0xafd8
movq (%r15), %rcx
leaq 0x1454(%rip), %rdi      # 0x6f9f
pushq $0x7
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x11c0
movq (%r15), %rdi
movq 0x40(%rbx), %rsi
callq 0x326d
movq (%r15), %rsi
pushq $0xa
popq %rbp
movl %ebp, %edi
callq 0x10f0
movq (%r15), %rdi
callq 0x1170
movq 0x40(%rbx), %rdi
callq 0x42dc
movq %rax, %rbx
movq (%r15), %rcx
leaq 0x1386(%rip), %rdi      # 0x6f17
pushq $0x8
popq %rsi
movq %r14, %rdx
callq 0x11c0
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x47b9
movq (%r15), %rsi
movl %ebp, %edi
callq 0x10f0
movq (%r15), %rdi
callq 0x1170
movaps 0x13f0(%rip), %xmm0     # 0x6fb0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
pushq $0x2
popq %rsi
callq 0x1030
cmpl $-0x1, %eax
je 0x5c9a
leaq 0x135b(%rip), %rsi      # 0x6f3b
movl %eax, %edi
callq 0x1190
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x47b9
movq %r14, %rdi
callq 0x1090
leaq 0x10(%rsp), %rcx
movaps (%rcx), %xmm0
movq %rsp, %r8
movaps %xmm0, (%r8)
movb $0x6f, 0xe(%r8)
leaq 0x1326(%rip), %rdx      # 0x6f3e
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x10c0
cmpl $0x400, %eax            # imm = 0x400
jae 0x5c85
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
jne 0x5caf
leaq 0x1327(%rip), %rdx      # 0x6f6c
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 0x5c85
leaq 0x20(%rsp), %rdi
callq 0x10a0
testl %eax, %eax
je 0x5cce
movq 0x5381(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x12fb(%rip), %rdi      # 0x6f7c
pushq $0xb
jmp 0x5cc2
movq 0x536c(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x12b4(%rip), %rdi      # 0x6f4a
pushq $0x16
jmp 0x5cc2
movq 0x5357(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x1275(%rip), %rdi      # 0x6f20
pushq $0x1a
jmp 0x5cc2
movq 0x5342(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x12a1(%rip), %rdi      # 0x6f61
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0
pushq $0x1
popq %rbp
movq %rbx, %rdi
callq 0x4746
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r14
popq %r15
popq %rbp
retq