// File: /workspace/github_c_makefile_star2_O2/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 0x288f(%rip), %rsi      # 0xecd0
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3130
movb %r15b, 0xdab7(%rip)     # 0x19f0c
movw $0x7861, 0xdaac(%rip)   # imm = 0x7861
movb %r15b, 0xdabb(%rip)     # 0x19f20
movw $0x6c61, 0xdab0(%rip)   # imm = 0x6C61
movb %r15b, 0xdaa3(%rip)     # 0x19f18
movl $0x306d6d78, 0xda95(%rip) # imm = 0x306D6D78
movl $0x786172, %eax         # imm = 0x786172
movl %eax, 0xdaa8(%rip)      # 0x19f32
movl %eax, 0xdab6(%rip)      # 0x19f46
movl %eax, 0xdaa6(%rip)      # 0x19f3c
movb %r15b, 0xdabf(%rip)     # 0x19f5c
movw $0x7863, 0xdab4(%rip)   # imm = 0x7863
movb %r15b, 0xdac3(%rip)     # 0x19f70
movw $0x6c63, 0xdab8(%rip)   # imm = 0x6C63
movb %r15b, 0xdaab(%rip)     # 0x19f68
movl $0x316d6d78, 0xda9d(%rip) # imm = 0x316D6D78
movl $0x786372, %eax         # imm = 0x786372
movl %eax, 0xdab0(%rip)      # 0x19f82
movl %eax, 0xdabe(%rip)      # 0x19f96
movl %eax, 0xdaae(%rip)      # 0x19f8c
movb %r15b, 0xd9f9(%rip)     # 0x19ede
movl $0x64726f77, 0xd9eb(%rip) # imm = 0x64726F77
movb %r15b, 0xd9fc(%rip)     # 0x19ef2
movl $0x65747962, 0xd9ee(%rip) # imm = 0x65747962
movw $0x64, 0xd9df(%rip)     # 0x19ee8
movl $0x726f7764, 0xd9d1(%rip) # imm = 0x726F7764
leaq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0xb84a
addq $0x40, %r14
pushq $0x6
popq %r12
leaq 0x292e(%rip), %r13      # 0xee5c
movq (%r14), %r14
testq %r14, %r14
je 0xc623
movq 0x50(%r14), %rdx
testq %rdx, %rdx
je 0xc556
movq %rbx, %rdi
leaq 0x2988(%rip), %rsi      # 0xeed5
xorl %eax, %eax
callq 0x3130
jmp 0xc56b
leaq 0x2971(%rip), %rdi      # 0xeece
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x31c0
xorl %edi, %edi
leaq 0x2758(%rip), %rsi      # 0xeccc
leaq 0x2966(%rip), %rdx      # 0xeee1
callq 0xb687
movq %r15, %rdi
movq %rax, %rsi
callq 0xb8ff
movl %r12d, %edi
leaq 0x28c3(%rip), %rdx      # 0xee58
movq %rdx, %rsi
callq 0xb687
movq %r15, %rdi
movq %rax, %rsi
callq 0xb8ff
movl %r12d, %edi
movq %r13, %rsi
movq %r13, %rdx
callq 0xb687
movq %r15, %rdi
movq %rax, %rsi
callq 0xb8ff
leaq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0xc3ef
cmpq $0x0, 0x50(%r14)
jne 0xc5f4
xorl %edi, %edi
leaq 0x28a4(%rip), %rsi      # 0xee81
leaq 0x28d2(%rip), %rdx      # 0xeeb6
callq 0xb687
movq %r15, %rdi
movq %rax, %rsi
callq 0xb8ff
pushq $0x14
popq %rdi
callq 0xb65f
movq %r15, %rdi
movq %rax, %rsi
callq 0xb8ff
movq %r15, %rdi
movq %rbx, %rsi
callq 0xb9f8
movq %r15, %rdi
callq 0xb9d6
addq $0x48, %r14
jmp 0xc52e
xorl %r13d, %r13d
leaq 0x86eb(%rip), %r14      # 0x14d18
leaq 0x121b(%rip), %r15      # 0xd84f
cmpq $0x7, %r13
je 0xc69f
cmpb $0x1, (%r13,%r14)
jne 0xc69a
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x3100
leaq 0x868c(%rip), %rax      # 0x14ce0
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x3160
movq %rax, %r12
movl $0x2000, %ecx           # imm = 0x2000
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3150
testq %r12, %r12
jne 0xc685
jmp 0xc69a
movl %eax, %edi
movq %rbx, %rsi
callq 0x3100
movq %r12, %rdi
callq 0x30f0
cmpl $-0x1, %eax
jne 0xc67b
movq %r12, %rdi
callq 0x30a0
incq %r13
jmp 0xc634
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop