// File: /workspace/github_cpp_makefile_star2_O2/saumya-prakash[P]AssemblerEmulator/Assembler.cpp
// Function: Assembler::generate_object_file() const
void Assembler::generate_object_file() const    // format_code, newline, text size, newline, program code, newline, newline, data size, data
{
    string::size_type ind = filename.find_first_of('.');

    string object_file = string(filename.begin(), filename.begin()+ind);

    ofstream fo(object_file+".o", ofstream::binary | ofstream::out);

    const char nwln = '\n';

    const char* ptr = format_code;  // writing format_code
    for(unsigned i=0; i<8; ++i)
    {
        fo.write(format_code+i, 1);
    }

    fo.write(&nwln, 1);


    unsigned text_size = pc;
    unsigned data_size = data_to_reserve.size();    // more generic way to get data segment size

    print_bytes(fo, text_size);
    fo.write(&nwln, 1);

    list<struct line>::const_iterator it = lines.begin();

    for(unsigned i=0; i<pc; ++i)    // put text segment first
    {
        unsigned a = it->encoding;
        ++it;
        
        print_bytes(fo, a);
    }

    fo.write(&nwln, 1);
    fo.write(&nwln, 1);


    print_bytes(fo, data_size);
    fo.write(&nwln, 1);
    
    map<unsigned, unsigned>::const_iterator dt = data_to_reserve.begin();

    for(unsigned i=0; i<data_size; ++i)     // put data segment second
    {
        unsigned a = dt->second;
        ++dt;
        
        print_bytes(fo, a);
    }
    
    fo.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
addq $0xa8, %rdi
pushq $0x2e
popq %rsi
xorl %edx, %edx
callq 0x2260
movq 0xa8(%rbx), %rsi
leaq (%rax,%rsi), %rdx
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x719a
leaq 0x4a91(%rip), %rdx      # 0x8226
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x5c5c
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x2340
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x20e0
movb $0xa, (%r14)
xorl %r12d, %r12d
leaq 0x4a5a(%rip), %r13      # 0x8229
leaq 0x48(%rsp), %r14
pushq $0x1
popq %r15
cmpq $0x8, %r12
je 0x37f2
leaq (%r12,%r13), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x2270
incq %r12
jmp 0x37d8
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2270
movl 0x58(%rbx), %ebp
movl 0x90(%rbx), %edx
leaq 0x48(%rsp), %rsi
callq 0x55b4
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2270
movq 0x60(%rbx), %r15
xorl %r12d, %r12d
leaq 0x48(%rsp), %r14
cmpl 0x90(%rbx), %r12d
jae 0x3852
movl 0x38(%r15), %edx
movq (%r15), %r15
movq %r14, %rsi
callq 0x55b4
incl %r12d
jmp 0x3835
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2270
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2270
leaq 0x48(%rsp), %rsi
movl %ebp, %edx
callq 0x55b4
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2270
movq 0x48(%rbx), %r14
leaq 0x48(%rsp), %rbx
subl $0x1, %ebp
jb 0x38be
movl 0x24(%r14), %r15d
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rbx, %rsi
movl %r15d, %edx
callq 0x55b4
jmp 0x389d
leaq 0x48(%rsp), %rdi
callq 0x2290
leaq 0x48(%rsp), %rdi
callq 0x22b0
leaq 0x28(%rsp), %rdi
callq 0x20e0
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x390e
jmp 0x390e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20e0
jmp 0x391b
movq %rax, %rbx
jmp 0x391b
jmp 0x390e
jmp 0x390e
jmp 0x390e
jmp 0x390e
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x22b0
leaq 0x28(%rsp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2310
nop