// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x9f80
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x7090
movq %rax, -0x28(%rbp)
movq 0x5289(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq 0x22cf(%rip), %rsi      # 0xc028
movb $0x0, %al
callq 0x10e0
movq 0x5271(%rip), %rax      # 0xefd8
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x7310
movq 0x525e(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq 0x22ec(%rip), %rsi      # 0xc070
movb $0x0, %al
callq 0x10e0
movq 0x5246(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x1130
movq 0x231f(%rip), %rax      # 0xc0c0
movq %rax, -0x40(%rbp)
movq 0x231c(%rip), %rax      # 0xc0c8
movq %rax, -0x38(%rbp)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x1030
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x9deb
movq 0x522a(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x2259(%rip), %rsi      # 0xc031
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
jmp 0x9f60
movl -0x44(%rbp), %edi
leaq 0x2257(%rip), %rsi      # 0xc04c
callq 0x1150
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7310
movq -0x50(%rbp), %rdi
callq 0x1070
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x6f, -0x52(%rbp)
leaq -0x460(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x60(%rbp), %r8
movl $0x400, %esi            # imm = 0x400
leaq 0x220c(%rip), %rdx      # 0xc04f
movb $0x0, %al
callq 0x10a0
movl %eax, -0x464(%rbp)
cmpl $0x0, -0x464(%rbp)
jl 0x9e65
cmpl $0x400, -0x464(%rbp)    # imm = 0x400
jl 0x9e89
movq 0x518c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x21e5(%rip), %rsi      # 0xc05b
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
jmp 0x9f60
leaq -0x460(%rbp), %rdi
callq 0x1080
movl %eax, -0x468(%rbp)
cmpl $0x0, -0x468(%rbp)
je 0x9ec8
movq 0x514d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x21bd(%rip), %rsi      # 0xc072
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
jmp 0x9f60
leaq -0x460(%rbp), %rdi
leaq -0x60(%rbp), %rcx
movl $0x400, %esi            # imm = 0x400
leaq 0x219e(%rip), %rdx      # 0xc07d
movb $0x0, %al
callq 0x10a0
movl %eax, -0x464(%rbp)
cmpl $0x0, -0x464(%rbp)
jl 0x9f01
cmpl $0x400, -0x464(%rbp)    # imm = 0x400
jl 0x9f22
movq 0x50f0(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x2149(%rip), %rsi      # 0xc05b
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
jmp 0x9f60
leaq -0x460(%rbp), %rdi
callq 0x1080
movl %eax, -0x468(%rbp)
cmpl $0x0, -0x468(%rbp)
je 0x9f5e
movq 0x50b4(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x213f(%rip), %rsi      # 0xc08d
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
jmp 0x9f60
jmp 0x9f60
movq -0x28(%rbp), %rdi
callq 0x7220
movl -0x14(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopw %cs:(%rax,%rax)