// File: 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:
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, 0x2f0(%rsp)
movq 0x2f0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0xa8, %rdi
movq %rdi, 0x28(%rsp)
xorl %eax, %eax
movl %eax, %edx
movl $0x2e, %esi
callq 0x3190
movq 0x28(%rsp), %rdi
movq %rax, 0x2e8(%rsp)
callq 0x3260
movq 0x28(%rsp), %rdi
movq %rax, 0x2c0(%rsp)
callq 0x3260
movq %rax, 0x2b0(%rsp)
movq 0x2e8(%rsp), %rsi
leaq 0x2b0(%rsp), %rdi
callq 0xcd70
movq %rax, 0x2b8(%rsp)
leaq 0x2af(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x34a0
movq 0x30(%rsp), %rcx
movq 0x2c0(%rsp), %rsi
movq 0x2b8(%rsp), %rdx
leaq 0x2c8(%rsp), %rdi
callq 0xcdb0
jmp 0x6de5
leaq 0x2af(%rsp), %rdi
callq 0x32f0
leaq 0xd42d(%rip), %rdx      # 0x14226
leaq 0x78(%rsp), %rdi
leaq 0x2c8(%rsp), %rsi
callq 0xc750
jmp 0x6e0d
movl $0x4, %edi
movl $0x10, %esi
callq 0xd140
movl %eax, 0x1c(%rsp)
jmp 0x6e22
movl 0x1c(%rsp), %edx
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x34e0
jmp 0x6e3a
leaq 0x78(%rsp), %rdi
callq 0x3180
movb $0xa, 0x77(%rsp)
leaq 0xd3d9(%rip), %rax      # 0x14229
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
cmpl $0x8, 0x64(%rsp)
jae 0x6f21
movl 0x64(%rsp), %eax
leaq 0xd3b6(%rip), %rsi      # 0x14229
addq %rax, %rsi
leaq 0x98(%rsp), %rdi
movl $0x1, %edx
callq 0x33c0
jmp 0x6e8a
jmp 0x6e8c
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x6e5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x2af(%rsp), %rdi
callq 0x32f0
jmp 0x7107
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x70fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x3180
jmp 0x70fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x3410
jmp 0x70fa
leaq 0x98(%rsp), %rdi
leaq 0x77(%rsp), %rsi
movl $0x1, %edx
callq 0x33c0
jmp 0x6f3a
movq 0x20(%rsp), %rdi
movl 0x90(%rdi), %eax
movl %eax, 0x60(%rsp)
addq $0x30, %rdi
callq 0xd160
movq 0x20(%rsp), %rdi
movl %eax, 0x5c(%rsp)
movl 0x60(%rsp), %edx
leaq 0x98(%rsp), %rsi
callq 0xb1a0
jmp 0x6f6e
leaq 0x98(%rsp), %rdi
leaq 0x77(%rsp), %rsi
movl $0x1, %edx
callq 0x33c0
jmp 0x6f87
movq 0x20(%rsp), %rdi
addq $0x60, %rdi
callq 0xce40
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movq 0x20(%rsp), %rcx
movl 0x4c(%rsp), %eax
cmpl 0x90(%rcx), %eax
jae 0x6ffa
leaq 0x50(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0xcf20
movq 0x10(%rsp), %rdi
movl 0x28(%rax), %eax
movl %eax, 0x48(%rsp)
callq 0xd170
movq 0x20(%rsp), %rdi
movl 0x48(%rsp), %edx
leaq 0x98(%rsp), %rsi
callq 0xb1a0
jmp 0x6feb
jmp 0x6fed
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x6fa2
leaq 0x98(%rsp), %rdi
leaq 0x77(%rsp), %rsi
movl $0x1, %edx
callq 0x33c0
jmp 0x7013
leaq 0x98(%rsp), %rdi
leaq 0x77(%rsp), %rsi
movl $0x1, %edx
callq 0x33c0
jmp 0x702c
movq 0x20(%rsp), %rdi
movl 0x5c(%rsp), %edx
leaq 0x98(%rsp), %rsi
callq 0xb1a0
jmp 0x7044
leaq 0x98(%rsp), %rdi
leaq 0x77(%rsp), %rsi
movl $0x1, %edx
callq 0x33c0
jmp 0x705d
movq 0x20(%rsp), %rdi
addq $0x30, %rdi
callq 0xd190
movq %rax, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jae 0x70c9
leaq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xd1c0
movq 0x8(%rsp), %rdi
movl 0x4(%rax), %eax
movl %eax, 0x38(%rsp)
callq 0xd1e0
movq 0x20(%rsp), %rdi
movl 0x38(%rsp), %edx
leaq 0x98(%rsp), %rsi
callq 0xb1a0
jmp 0x70ba
jmp 0x70bc
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x7078
leaq 0x98(%rsp), %rdi
callq 0x33f0
jmp 0x70d8
leaq 0x98(%rsp), %rdi
callq 0x3410
leaq 0x2c8(%rsp), %rdi
callq 0x3180
addq $0x2f8, %rsp            # imm = 0x2F8
retq
leaq 0x2c8(%rsp), %rdi
callq 0x3180
movq 0x2a0(%rsp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)