// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x2e, %esi
xorl %edx, %edx
callq 0x2240
movq 0xa8(%rbx), %rsi
leaq (%rax,%rsi), %rdx
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x9352
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x32b6
leaq 0x638b(%rip), %rsi      # 0xa200
leaq 0x8(%rsp), %rdi
callq 0x2310
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x14, %edx
callq 0x2320
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x3eaa
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movb $0xa, 0x8(%rsp)
xorl %r15d, %r15d
leaq 0x634a(%rip), %r13      # 0xa203
leaq 0x48(%rsp), %r14
leaq (%r15,%r13), %rsi
movl $0x1, %edx
movq %r14, %rdi
callq 0x2250
incq %r15
cmpq $0x8, %r15
jne 0x3ebe
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
movl 0x58(%rbx), %ebp
movl 0x90(%rbx), %edx
leaq 0x48(%rsp), %rsi
callq 0x7a32
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
cmpl $0x0, 0x90(%rbx)
je 0x3f43
movq 0x60(%rbx), %r15
xorl %r13d, %r13d
leaq 0x48(%rsp), %r14
movl 0x38(%r15), %edx
movq (%r15), %r15
movq %r14, %rsi
callq 0x7a32
incl %r13d
cmpl 0x90(%rbx), %r13d
jb 0x3f28
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
leaq 0x48(%rsp), %rsi
movl %ebp, %edx
callq 0x7a32
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x2250
testl %ebp, %ebp
je 0x3fb6
movq 0x48(%rbx), %r14
leaq 0x48(%rsp), %rbx
movl 0x24(%r14), %r15d
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rbx, %rsi
movl %r15d, %edx
callq 0x7a32
decl %ebp
jne 0x3f98
leaq 0x48(%rsp), %rdi
callq 0x2260
movq 0xa001(%rip), %rax      # 0xdfc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x50(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x48(%rsp,%rcx)
callq 0x2170
leaq 0x140(%rsp), %rdi
callq 0x20b0
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x4009
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x404a
jmp 0x404a
jmp 0x4021
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x4057
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x4057
movq %rax, %rbx
jmp 0x4057
jmp 0x404a
jmp 0x404a
jmp 0x404a
jmp 0x404a
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2290
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x406e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22e0