// File: /workspace/github_c_makefile_star2_O2/yutopp[P]cc/main.c
// Function: main
int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "argc: %d shoule be 2", argc);
        return 1;
    }

    int exit_code = 1;
    char *fpath = argv[1];

    printf("C => %s\n", fpath);
    FILE *fp = fopen(fpath, "rb");
    if (fp == NULL) {
        fprintf(stderr, "Failed to open file: ");
        goto exit_0;
    }

    char *fcontent = read_all(fp);
    fclose(fp);

    printf("%s\n", fcontent);

    CC* cc = cc_new(fcontent, fpath);

    ParserResult res = cc_parse(cc);
    if (res.result == PARSER_ERROR) {
        fprintf(stderr, "ERROR: ");
        parser_fprint_error(stderr, &res.error);
        fprintf(stderr, "\n");
        goto exit;
    }
    assert(res.value.node);

    printf("= AST =\n");
    node_fprint(stdout, res.value.node);
    printf("\n");

    /*AnalyzerResult res = */cc_analyze(cc, res.value.node);
    /* TODO: error check */

    int err =
    /*CompileResult res = */cc_compile(cc, res.value.node);
    if (err) {
        goto exit;
    }

    exit_code = 0; // Success

exit:
    cc_drop(cc);

exit_0:
    free(fcontent);

    return exit_code;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
cmpl $0x2, %edi
jne 0x1403
movq 0x8(%rsi), %r14
leaq 0x4d2d(%rip), %rdi      # 0x6019
movq %r14, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x4d25(%rip), %rsi      # 0x6022
movq %r14, %rdi
callq 0x11a0
testq %rax, %rax
je 0x1478
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1180
movq %r15, %rdi
callq 0x1130
movq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1180
leaq 0x1(%r12), %rdi
callq 0x1160
movq %rax, %rbx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x1080
movb $0x0, (%rbx,%r12)
movq %r15, %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1070
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5920
movq %rax, %r14
movq %rsp, %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x59f9
cmpl $0x1, (%r15)
je 0x1422
cmpq $0x0, 0x18(%rsp)
je 0x145b
leaq 0x4ccf(%rip), %rdi      # 0x6070
callq 0x1070
movq 0x9c2b(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq 0x18(%rsp), %rsi
callq 0x2a15
pushq $0xa
popq %rdi
callq 0x1050
movq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x5ada
movq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x5b0d
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movq %r14, %rdi
callq 0x5954
movq %rbx, %rdi
callq 0x1040
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl %edi, %edx
movq 0x9bec(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x4bee(%rip), %rsi      # 0x6004
xorl %eax, %eax
callq 0x1120
pushq $0x1
popq %rbp
jmp 0x13f4
movq 0x9bcf(%rip), %r15      # 0xaff8
movq (%r15), %rcx
leaq 0x4c08(%rip), %rdi      # 0x603b
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x19e9
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
pushq $0x1
popq %rbp
jmp 0x13e4
leaq 0x4be1(%rip), %rdi      # 0x6043
leaq 0x4be9(%rip), %rsi      # 0x6052
leaq 0x4be9(%rip), %rcx      # 0x6059
pushq $0x36
popq %rdx
callq 0x10d0
movq 0x9b79(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x4b9c(%rip), %rdi      # 0x6025
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0