// File: /workspace/github_c_makefile_star2_O3/sharmaeklavya2[P]cerplag/compile_x86.c
// Function: compile_program_to_x86
void compile_program_to_x86(ProgramNode* program_node, pSD psd, FILE* ofp) {
    int data_size = psd->root->value->max_offset;

    fprintf(ofp, PROLOGUE_STR, data_size);

    strcpy(std_regs[0][TYPE_INTEGER], "ax");    // std_regs[0] must be "ax" for multiply to work
    strcpy(std_regs[0][TYPE_BOOLEAN], "al");
    strcpy(std_regs[0][TYPE_REAL], "xmm0");
    strcpy(std_regs[1][TYPE_INTEGER], "rax");
    strcpy(std_regs[1][TYPE_BOOLEAN], "rax");
    strcpy(std_regs[1][TYPE_REAL], "rax");
    strcpy(std_regs[2][TYPE_INTEGER], "cx");
    strcpy(std_regs[2][TYPE_BOOLEAN], "cl");
    strcpy(std_regs[2][TYPE_REAL], "xmm1");
    strcpy(std_regs[3][TYPE_INTEGER], "rcx");
    strcpy(std_regs[3][TYPE_BOOLEAN], "rcx");
    strcpy(std_regs[3][TYPE_REAL], "rcx");
    strcpy(std_size_name[TYPE_INTEGER], "word");
    strcpy(std_size_name[TYPE_BOOLEAN], "byte");
    strcpy(std_size_name[TYPE_REAL], "dword");

    X86Code x86_code;
    x86_code_init(&x86_code);
    ModuleNode* module_node = program_node->modules;
    for(; module_node != NULL; module_node = module_node->next) {
        if(module_node->name == NULL) {
            fprintf(ofp, "main:\n");
        }
        else {
            fprintf(ofp, "module_%s:\n", module_node->name);
        }

        x86_code_append(&x86_code, x86_instr_new2(X86_OP_mov, DATA_REG, "data"));
        x86_code_append(&x86_code, x86_instr_new2(X86_OP_xor, "rsi", "rsi"));
        x86_code_append(&x86_code, x86_instr_new2(X86_OP_xor, "rdi", "rdi"));
        compile_code_to_x86(&(module_node->base.ircode), &x86_code);
        if(module_node->name == NULL) {
            x86_code_append(&x86_code, x86_instr_new2(X86_OP_mov, "rax", "0"));
        }
        x86_code_append(&x86_code, x86_instr_new(X86_OP_ret));
        optimize_x86_code(&x86_code);
        x86_code_print(&x86_code, ofp);
        x86_code_clear(&x86_code);
    }

    // read libs and output them
    int num_libs = sizeof(include_lib_files) / sizeof(bool);
    int i;
    for(i=0; i<num_libs; ++i) {
        if(include_lib_files[i]) {
            fputc('\n', ofp);
            FILE* libfp = fopen(ASM_LIB_FILES[i], "r");
            setvbuf(libfp, NULL, _IOFBF, BUFSIZ);   // fully buffer file
            if (libfp) {
                int c;
                while ((c = fgetc(libfp)) != EOF)
                    fputc(c, ofp);
                fclose(libfp);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rdi, %r14
movq (%rsi), %rax
movq (%rax), %rax
movl 0x4c(%rax), %edx
leaq 0x28db(%rip), %rsi      # 0xfcd0
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3140
movb %r15b, 0xdb03(%rip)     # 0x1af0c
movw $0x7861, 0xdaf8(%rip)   # imm = 0x7861
movb %r15b, 0xdb07(%rip)     # 0x1af20
movw $0x6c61, 0xdafc(%rip)   # imm = 0x6C61
movb %r15b, 0xdaef(%rip)     # 0x1af18
movl $0x306d6d78, 0xdae1(%rip) # imm = 0x306D6D78
movl $0x786172, %eax         # imm = 0x786172
movl %eax, 0xdaf4(%rip)      # 0x1af32
movl %eax, 0xdb02(%rip)      # 0x1af46
movl %eax, 0xdaf2(%rip)      # 0x1af3c
movb %r15b, 0xdb0b(%rip)     # 0x1af5c
movw $0x7863, 0xdb00(%rip)   # imm = 0x7863
movb %r15b, 0xdb0f(%rip)     # 0x1af70
movw $0x6c63, 0xdb04(%rip)   # imm = 0x6C63
movb %r15b, 0xdaf7(%rip)     # 0x1af68
movl $0x316d6d78, 0xdae9(%rip) # imm = 0x316D6D78
movl $0x786372, %eax         # imm = 0x786372
movl %eax, 0xdafc(%rip)      # 0x1af82
movl %eax, 0xdb0a(%rip)      # 0x1af96
movl %eax, 0xdafa(%rip)      # 0x1af8c
movb %r15b, 0xda45(%rip)     # 0x1aede
movl $0x64726f77, 0xda37(%rip) # imm = 0x64726F77
movb %r15b, 0xda48(%rip)     # 0x1aef2
movl $0x65747962, 0xda3a(%rip) # imm = 0x65747962
movl $0x726f7764, 0xda26(%rip) # imm = 0x726F7764
movw $0x64, 0xda21(%rip)     # 0x1aee8
leaq -0x40(%rbp), %rdi
callq 0xc7fc
movq 0x40(%r14), %r14
testq %r14, %r14
je 0xd5ee
leaq -0x40(%rbp), %r13
leaq 0x2974(%rip), %r15      # 0xfe5c
movq 0x50(%r14), %rdx
testq %rdx, %rdx
je 0xd504
movq %rbx, %rdi
leaq 0x29da(%rip), %rsi      # 0xfed5
xorl %eax, %eax
callq 0x3140
jmp 0xd51d
movl $0x6, %esi
movl $0x1, %edx
leaq 0x29b9(%rip), %rdi      # 0xfece
movq %rbx, %rcx
callq 0x31d0
xorl %edi, %edi
leaq 0x27a6(%rip), %rsi      # 0xfccc
leaq 0x29b4(%rip), %rdx      # 0xfee1
callq 0xc5f6
movq %r13, %rdi
movq %rax, %rsi
callq 0xc8b4
movl $0x6, %edi
leaq 0x290f(%rip), %rdx      # 0xfe58
movq %rdx, %rsi
callq 0xc5f6
movq %r13, %rdi
movq %rax, %rsi
callq 0xc8b4
movl $0x6, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0xc5f6
movq %r13, %rdi
movq %rax, %rsi
callq 0xc8b4
movq 0x20(%r14), %r12
testq %r12, %r12
je 0xd592
movq %r12, %rdi
movq %r13, %rsi
callq 0xcf64
movq 0x28(%r12), %r12
jmp 0xd57b
cmpq $0x0, 0x50(%r14)
jne 0xd5b9
xorl %edi, %edi
leaq 0x28df(%rip), %rsi      # 0xfe81
leaq 0x290d(%rip), %rdx      # 0xfeb6
callq 0xc5f6
movq %r13, %rdi
movq %rax, %rsi
callq 0xc8b4
movl $0x14, %edi
callq 0xc5c9
movq %r13, %rdi
movq %rax, %rsi
callq 0xc8b4
movq %r13, %rdi
movq %rbx, %rsi
callq 0xc9b6
movq %r13, %rdi
callq 0xc991
movq 0x48(%r14), %r14
testq %r14, %r14
jne 0xd4e8
xorl %r12d, %r12d
leaq 0x8720(%rip), %r13      # 0x15d18
leaq 0x1250(%rip), %r14      # 0xe84f
cmpb $0x1, (%r12,%r13)
jne 0xd660
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3110
leaq 0x86c6(%rip), %rax      # 0x15ce0
movq (%rax,%r12,8), %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %r15
movl $0x2000, %ecx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3160
testq %r15, %r15
jne 0xd64b
jmp 0xd660
movl %eax, %edi
movq %rbx, %rsi
callq 0x3110
movq %r15, %rdi
callq 0x3100
cmpl $-0x1, %eax
jne 0xd641
movq %r15, %rdi
callq 0x30a0
incq %r12
cmpq $0x7, %r12
jne 0xd5ff
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq