// File: /workspace/github_c_makefile_star2_O1/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 0x2853(%rip), %rsi      # 0xfcd0
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3140
movb %r15b, 0xda7b(%rip)     # 0x1af0c
movw $0x7861, 0xda70(%rip)   # imm = 0x7861
movb %r15b, 0xda7f(%rip)     # 0x1af20
movw $0x6c61, 0xda74(%rip)   # imm = 0x6C61
movb %r15b, 0xda67(%rip)     # 0x1af18
movl $0x306d6d78, 0xda59(%rip) # imm = 0x306D6D78
movl $0x786172, %eax         # imm = 0x786172
movl %eax, 0xda6c(%rip)      # 0x1af32
movl %eax, 0xda7a(%rip)      # 0x1af46
movl %eax, 0xda6a(%rip)      # 0x1af3c
movb %r15b, 0xda83(%rip)     # 0x1af5c
movw $0x7863, 0xda78(%rip)   # imm = 0x7863
movb %r15b, 0xda87(%rip)     # 0x1af70
movw $0x6c63, 0xda7c(%rip)   # imm = 0x6C63
movb %r15b, 0xda6f(%rip)     # 0x1af68
movl $0x316d6d78, 0xda61(%rip) # imm = 0x316D6D78
movl $0x786372, %eax         # imm = 0x786372
movl %eax, 0xda74(%rip)      # 0x1af82
movl %eax, 0xda82(%rip)      # 0x1af96
movl %eax, 0xda72(%rip)      # 0x1af8c
movb %r15b, 0xd9bd(%rip)     # 0x1aede
movl $0x64726f77, 0xd9af(%rip) # imm = 0x64726F77
movb %r15b, 0xd9c0(%rip)     # 0x1aef2
movl $0x65747962, 0xd9b2(%rip) # imm = 0x65747962
movl $0x726f7764, 0xd99e(%rip) # imm = 0x726F7764
movw $0x64, 0xd999(%rip)     # 0x1aee8
leaq -0x40(%rbp), %rdi
callq 0xc894
movq 0x40(%r14), %r14
testq %r14, %r14
je 0xd676
leaq -0x40(%rbp), %r13
leaq 0x28ec(%rip), %r15      # 0xfe5c
movq 0x50(%r14), %rdx
testq %rdx, %rdx
je 0xd58c
movq %rbx, %rdi
leaq 0x2952(%rip), %rsi      # 0xfed5
xorl %eax, %eax
callq 0x3140
jmp 0xd5a5
movl $0x6, %esi
movl $0x1, %edx
leaq 0x2931(%rip), %rdi      # 0xfece
movq %rbx, %rcx
callq 0x31d0
xorl %edi, %edi
leaq 0x271e(%rip), %rsi      # 0xfccc
leaq 0x292c(%rip), %rdx      # 0xfee1
callq 0xc69e
movq %r13, %rdi
movq %rax, %rsi
callq 0xc94f
movl $0x6, %edi
leaq 0x2887(%rip), %rdx      # 0xfe58
movq %rdx, %rsi
callq 0xc69e
movq %r13, %rdi
movq %rax, %rsi
callq 0xc94f
movl $0x6, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0xc69e
movq %r13, %rdi
movq %rax, %rsi
callq 0xc94f
movq 0x20(%r14), %r12
testq %r12, %r12
je 0xd61a
movq %r12, %rdi
movq %r13, %rsi
callq 0xcfec
movq 0x28(%r12), %r12
jmp 0xd603
cmpq $0x0, 0x50(%r14)
jne 0xd641
xorl %edi, %edi
leaq 0x2857(%rip), %rsi      # 0xfe81
leaq 0x2885(%rip), %rdx      # 0xfeb6
callq 0xc69e
movq %r13, %rdi
movq %rax, %rsi
callq 0xc94f
movl $0x14, %edi
callq 0xc66d
movq %r13, %rdi
movq %rax, %rsi
callq 0xc94f
movq %r13, %rdi
movq %rbx, %rsi
callq 0xca3c
movq %r13, %rdi
callq 0xca07
movq 0x48(%r14), %r14
testq %r14, %r14
jne 0xd570
xorl %r12d, %r12d
leaq 0x8698(%rip), %r13      # 0x15d18
leaq 0x11c8(%rip), %r14      # 0xe84f
cmpb $0x1, (%r12,%r13)
jne 0xd6e8
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3110
leaq 0x863e(%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 0xd6d3
jmp 0xd6e8
movl %eax, %edi
movq %rbx, %rsi
callq 0x3110
movq %r15, %rdi
callq 0x3100
cmpl $-0x1, %eax
jne 0xd6c9
movq %r15, %rdi
callq 0x30a0
incq %r12
cmpq $0x7, %r12
jne 0xd687
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq