// File: saumya-prakash[P]AssemblerEmulator/Assembler.cpp
// Function: Assembler::generate_listing_file() const
void Assembler::generate_listing_file() const
{
    if(errors.empty())
    {
        string::size_type ind = filename.find_first_of('.');

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

        ofstream lst(lst_file+".lst", ofstream::out);

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


        lst<<setw(10)<<"MemAddress"<<setw(10)<<"Encoding"<<'\t'<<"Statement"<<endl<<endl;

        lst<<uppercase<<setfill('0');

        while(it!=lines.end() && jt!=aux_lines.end())
        {
            if(it->line_no == jt->line_no)
            {

                lst<<setw(10)<<it->addr<<'\t';
                
                lst<<"0x"<<setw(8);
                lst<<hex;
                lst<<it->encoding<<'\t';
                lst<<dec;

                lst<<jt->s<<it->s;
                lst<<endl;

                it++;
                jt++;
            }

            else if(it->line_no > jt->line_no)
            {
                lst<<setw(10)<<jt->addr<<'\t';
                
                if(only_label(jt->s)==false)
                {
                    lst<<"0x"<<setw(8);
                    lst<<hex;
                    lst<<jt->encoding<<'\t';
                    lst<<dec;
                }
                else
                {
                    lst<<"        \t";
                }
                
                lst<<jt->s;
                lst<<endl;

                jt++;
            }

            else
            {
                lst<<setw(10)<<it->addr<<'\t';
                
                lst<<"0x"<<setw(8);
                lst<<hex;
                lst<<it->encoding<<'\t';
                lst<<dec;
                
                lst<<it->s;
                lst<<endl;

                it++;
            }
        }

        while(it!=lines.end())
        {
            lst<<setw(10)<<it->addr<<'\t';

            lst<<"0x"<<setw(8);
            lst<<hex;
            lst<<it->encoding<<'\t';
            lst<<dec;
            
            lst<<it->s;
            lst<<endl;

            it++;
        }

        while(jt!=aux_lines.end())
        {
            lst<<setw(10)<<jt->addr<<'\t';
            
            if(only_label(jt->s)==false)
            {   lst<<"0x"<<setw(8);
                lst<<hex;
                lst<<jt->encoding<<'\t';
                lst<<dec;
            }
            else
            {
                lst<<"        \t";
            }
            

            lst<<jt->s;
            lst<<endl;

            jt++;
        }
        
        lst<<setfill(' ')<<nouppercase;

        lst.close();
    }

    // ELSE, some error in source program - leave the previous listing file as it is
    
}

// Assembly:
subq $0x498, %rsp            # imm = 0x498
movq %rdi, 0x490(%rsp)
movq 0x490(%rsp), %rdi
movq %rdi, 0x170(%rsp)
addq $0xc8, %rdi
callq 0xc350
testb $0x1, %al
jne 0x9ff4
jmp 0xad25
movq 0x170(%rsp), %rdi
addq $0xa8, %rdi
movq %rdi, 0x160(%rsp)
xorl %eax, %eax
movl %eax, %edx
movl $0x2e, %esi
callq 0x3190
movq 0x160(%rsp), %rdi
movq %rax, 0x488(%rsp)
callq 0x3260
movq 0x160(%rsp), %rdi
movq %rax, 0x460(%rsp)
callq 0x3260
movq %rax, 0x450(%rsp)
movq 0x488(%rsp), %rsi
leaq 0x450(%rsp), %rdi
callq 0xcd70
movq %rax, 0x458(%rsp)
leaq 0x44f(%rsp), %rdi
movq %rdi, 0x168(%rsp)
callq 0x34a0
movq 0x168(%rsp), %rcx
movq 0x460(%rsp), %rsi
movq 0x458(%rsp), %rdx
leaq 0x468(%rsp), %rdi
callq 0xcdb0
jmp 0xa0a4
leaq 0x44f(%rsp), %rdi
callq 0x32f0
leaq 0xa11f(%rip), %rdx      # 0x141d7
leaq 0x218(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0xc750
jmp 0xa0cf
leaq 0x238(%rsp), %rdi
leaq 0x218(%rsp), %rsi
movl $0x10, %edx
callq 0x34e0
jmp 0xa0eb
leaq 0x218(%rsp), %rdi
callq 0x3180
movq 0x170(%rsp), %rdi
addq $0x60, %rdi
callq 0xce40
movq 0x170(%rsp), %rdi
movq %rax, 0x210(%rsp)
addq $0x78, %rdi
callq 0xce40
movq %rax, 0x208(%rsp)
movl $0xa, %edi
callq 0xce70
movl %eax, 0x15c(%rsp)
jmp 0xa13d
movl 0x15c(%rsp), %eax
movl %eax, 0x204(%rsp)
movl 0x204(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0x150(%rsp)
jmp 0xa169
movq 0x150(%rsp), %rdi
leaq 0xa064(%rip), %rsi      # 0x141dc
callq 0x3270
movq %rax, 0x148(%rsp)
jmp 0xa187
movl $0xa, %edi
callq 0xce70
movl %eax, 0x144(%rsp)
jmp 0xa19a
movq 0x148(%rsp), %rdi
movl 0x144(%rsp), %eax
movl %eax, 0x200(%rsp)
movl 0x200(%rsp), %esi
callq 0x3340
movq %rax, 0x138(%rsp)
jmp 0xa1c6
movq 0x138(%rsp), %rdi
leaq 0xa012(%rip), %rsi      # 0x141e7
callq 0x3270
movq %rax, 0x130(%rsp)
jmp 0xa1e4
movq 0x130(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
movq %rax, 0x128(%rsp)
jmp 0xa200
movq 0x128(%rsp), %rdi
leaq 0x9fe1(%rip), %rsi      # 0x141f0
callq 0x3270
movq %rax, 0x120(%rsp)
jmp 0xa21e
movq 0x120(%rsp), %rdi
movq 0x11d9b(%rip), %rsi     # 0x1bfc8
callq 0x32d0
movq %rax, 0x118(%rsp)
jmp 0xa23c
movq 0x118(%rsp), %rdi
movq 0x11d7d(%rip), %rsi     # 0x1bfc8
callq 0x32d0
jmp 0xa252
leaq 0x2c37(%rip), %rsi      # 0xce90
leaq 0x238(%rsp), %rdi
callq 0x30e0
movq %rax, 0x110(%rsp)
jmp 0xa270
movl $0x30, %edi
callq 0xceb0
movb %al, 0x10f(%rsp)
jmp 0xa283
movq 0x110(%rsp), %rdi
movb 0x10f(%rsp), %al
movb %al, 0x1ff(%rsp)
movzbl 0x1ff(%rsp), %esi
callq 0x3430
jmp 0xa2a8
jmp 0xa2aa
movq 0x170(%rsp), %rdi
addq $0x60, %rdi
callq 0xcef0
movq %rax, 0x1f0(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0xced0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x10e(%rsp)
jne 0xa2ea
jmp 0xa31f
movq 0x170(%rsp), %rdi
addq $0x78, %rdi
callq 0xcef0
movq %rax, 0x1e8(%rsp)
leaq 0x208(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0xced0
movb %al, 0x10e(%rsp)
movb 0x10e(%rsp), %al
testb $0x1, %al
jne 0xa32f
jmp 0xa92e
leaq 0x210(%rsp), %rdi
callq 0xcf20
movl 0x20(%rax), %eax
movl %eax, 0x108(%rsp)
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rcx
movl 0x108(%rsp), %eax
cmpl 0x20(%rcx), %eax
jne 0xa5b6
movl $0xa, %edi
callq 0xce70
movl %eax, 0x104(%rsp)
jmp 0xa379
movl 0x104(%rsp), %eax
movl %eax, 0x1e4(%rsp)
movl 0x1e4(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0xf8(%rsp)
jmp 0xa3a5
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq 0xf8(%rsp), %rdi
movl 0x24(%rax), %esi
callq 0x33d0
movq %rax, 0xf0(%rsp)
jmp 0xa3cc
movq 0xf0(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa3e0
leaq 0x9e13(%rip), %rsi      # 0x141fa
leaq 0x238(%rsp), %rdi
callq 0x3270
movq %rax, 0xe8(%rsp)
jmp 0xa3fe
movl $0x8, %edi
callq 0xce70
movl %eax, 0xe4(%rsp)
jmp 0xa411
movq 0xe8(%rsp), %rdi
movl 0xe4(%rsp), %eax
movl %eax, 0x1e0(%rsp)
movl 0x1e0(%rsp), %esi
callq 0x3340
jmp 0xa435
leaq 0x1e04(%rip), %rsi      # 0xc240
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa44b
leaq 0x210(%rsp), %rdi
callq 0xcf20
movl 0x28(%rax), %esi
leaq 0x238(%rsp), %rdi
callq 0x33d0
movq %rax, 0xd8(%rsp)
jmp 0xa472
movq 0xd8(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa486
leaq 0x1df3(%rip), %rsi      # 0xc280
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa49c
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rsi
leaq 0x238(%rsp), %rdi
callq 0x3240
movq %rax, 0xd0(%rsp)
jmp 0xa4c3
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq 0xd0(%rsp), %rdi
movq %rax, %rsi
callq 0x3240
jmp 0xa4e2
movq 0x11adf(%rip), %rsi     # 0x1bfc8
leaq 0x238(%rsp), %rdi
callq 0x32d0
jmp 0xa4f8
leaq 0x210(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x1d8(%rsp)
leaq 0x208(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x1d0(%rsp)
jmp 0xa929
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x440(%rsp)
movl %eax, 0x43c(%rsp)
leaq 0x44f(%rsp), %rdi
callq 0x32f0
jmp 0xad2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x440(%rsp)
movl %eax, 0x43c(%rsp)
jmp 0xad16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x440(%rsp)
movl %eax, 0x43c(%rsp)
leaq 0x218(%rsp), %rdi
callq 0x3180
jmp 0xad16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x440(%rsp)
movl %eax, 0x43c(%rsp)
leaq 0x238(%rsp), %rdi
callq 0x3410
jmp 0xad16
leaq 0x210(%rsp), %rdi
callq 0xcf20
movl 0x20(%rax), %eax
movl %eax, 0xcc(%rsp)
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rcx
movl 0xcc(%rsp), %eax
cmpl 0x20(%rcx), %eax
jbe 0xa7b1
movl $0xa, %edi
callq 0xce70
movl %eax, 0xc8(%rsp)
jmp 0xa600
movl 0xc8(%rsp), %eax
movl %eax, 0x1cc(%rsp)
movl 0x1cc(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0xc0(%rsp)
jmp 0xa62c
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq 0xc0(%rsp), %rdi
movl 0x24(%rax), %esi
callq 0x33d0
movq %rax, 0xb8(%rsp)
jmp 0xa653
movq 0xb8(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa667
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rdi
callq 0x9eb0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xa748
leaq 0x9b69(%rip), %rsi      # 0x141fa
leaq 0x238(%rsp), %rdi
callq 0x3270
movq %rax, 0xb0(%rsp)
jmp 0xa6a8
movl $0x8, %edi
callq 0xce70
movl %eax, 0xac(%rsp)
jmp 0xa6bb
movq 0xb0(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x1c8(%rsp)
movl 0x1c8(%rsp), %esi
callq 0x3340
jmp 0xa6df
leaq 0x1b5a(%rip), %rsi      # 0xc240
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa6f5
leaq 0x208(%rsp), %rdi
callq 0xcf20
movl 0x28(%rax), %esi
leaq 0x238(%rsp), %rdi
callq 0x33d0
movq %rax, 0xa0(%rsp)
jmp 0xa71c
movq 0xa0(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa730
leaq 0x1b49(%rip), %rsi      # 0xc280
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa746
jmp 0xa760
leaq 0x9aae(%rip), %rsi      # 0x141fd
leaq 0x238(%rsp), %rdi
callq 0x3270
jmp 0xa75e
jmp 0xa760
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rsi
leaq 0x238(%rsp), %rdi
callq 0x3240
jmp 0xa77f
movq 0x11842(%rip), %rsi     # 0x1bfc8
leaq 0x238(%rsp), %rdi
callq 0x32d0
jmp 0xa795
leaq 0x208(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x1c0(%rsp)
jmp 0xa927
movl $0xa, %edi
callq 0xce70
movl %eax, 0x9c(%rsp)
jmp 0xa7c4
movl 0x9c(%rsp), %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0x90(%rsp)
jmp 0xa7f0
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq 0x90(%rsp), %rdi
movl 0x24(%rax), %esi
callq 0x33d0
movq %rax, 0x88(%rsp)
jmp 0xa817
movq 0x88(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa82b
leaq 0x99c8(%rip), %rsi      # 0x141fa
leaq 0x238(%rsp), %rdi
callq 0x3270
movq %rax, 0x80(%rsp)
jmp 0xa849
movl $0x8, %edi
callq 0xce70
movl %eax, 0x7c(%rsp)
jmp 0xa859
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x1b8(%rsp)
movl 0x1b8(%rsp), %esi
callq 0x3340
jmp 0xa87a
leaq 0x19bf(%rip), %rsi      # 0xc240
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa890
leaq 0x210(%rsp), %rdi
callq 0xcf20
movl 0x28(%rax), %esi
leaq 0x238(%rsp), %rdi
callq 0x33d0
movq %rax, 0x70(%rsp)
jmp 0xa8b4
movq 0x70(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa8c5
leaq 0x19b4(%rip), %rsi      # 0xc280
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xa8db
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq %rax, %rsi
leaq 0x238(%rsp), %rdi
callq 0x3240
jmp 0xa8fa
movq 0x116c7(%rip), %rsi     # 0x1bfc8
leaq 0x238(%rsp), %rdi
callq 0x32d0
jmp 0xa910
leaq 0x210(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x1b0(%rsp)
jmp 0xa929
jmp 0xa2aa
jmp 0xa930
movq 0x170(%rsp), %rdi
addq $0x60, %rdi
callq 0xcef0
movq %rax, 0x1a8(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xced0
testb $0x1, %al
jne 0xa967
jmp 0xaaca
movl $0xa, %edi
callq 0xce70
movl %eax, 0x6c(%rsp)
jmp 0xa977
movl 0x6c(%rsp), %eax
movl %eax, 0x1a4(%rsp)
movl 0x1a4(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0x60(%rsp)
jmp 0xa99d
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq 0x60(%rsp), %rdi
movl 0x24(%rax), %esi
callq 0x33d0
movq %rax, 0x58(%rsp)
jmp 0xa9be
movq 0x58(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xa9cf
leaq 0x9824(%rip), %rsi      # 0x141fa
leaq 0x238(%rsp), %rdi
callq 0x3270
movq %rax, 0x50(%rsp)
jmp 0xa9ea
movl $0x8, %edi
callq 0xce70
movl %eax, 0x4c(%rsp)
jmp 0xa9fa
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %eax
movl %eax, 0x1a0(%rsp)
movl 0x1a0(%rsp), %esi
callq 0x3340
jmp 0xaa18
leaq 0x1821(%rip), %rsi      # 0xc240
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xaa2e
leaq 0x210(%rsp), %rdi
callq 0xcf20
movl 0x28(%rax), %esi
leaq 0x238(%rsp), %rdi
callq 0x33d0
movq %rax, 0x40(%rsp)
jmp 0xaa52
movq 0x40(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xaa63
leaq 0x1816(%rip), %rsi      # 0xc280
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xaa79
leaq 0x210(%rsp), %rdi
callq 0xcf20
movq %rax, %rsi
leaq 0x238(%rsp), %rdi
callq 0x3240
jmp 0xaa98
movq 0x11529(%rip), %rsi     # 0x1bfc8
leaq 0x238(%rsp), %rdi
callq 0x32d0
jmp 0xaaae
leaq 0x210(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x198(%rsp)
jmp 0xa930
jmp 0xaacc
movq 0x170(%rsp), %rdi
addq $0x78, %rdi
callq 0xcef0
movq %rax, 0x190(%rsp)
leaq 0x208(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xced0
testb $0x1, %al
jne 0xab03
jmp 0xaca3
movl $0xa, %edi
callq 0xce70
movl %eax, 0x3c(%rsp)
jmp 0xab13
movl 0x3c(%rsp), %eax
movl %eax, 0x18c(%rsp)
movl 0x18c(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3340
movq %rax, 0x30(%rsp)
jmp 0xab39
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq 0x30(%rsp), %rdi
movl 0x24(%rax), %esi
callq 0x33d0
movq %rax, 0x28(%rsp)
jmp 0xab5a
movq 0x28(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xab6b
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rdi
callq 0x9eb0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xac3a
leaq 0x9665(%rip), %rsi      # 0x141fa
leaq 0x238(%rsp), %rdi
callq 0x3270
movq %rax, 0x20(%rsp)
jmp 0xaba9
movl $0x8, %edi
callq 0xce70
movl %eax, 0x1c(%rsp)
jmp 0xabb9
movq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %eax
movl %eax, 0x188(%rsp)
movl 0x188(%rsp), %esi
callq 0x3340
jmp 0xabd7
leaq 0x1662(%rip), %rsi      # 0xc240
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xabed
leaq 0x208(%rsp), %rdi
callq 0xcf20
movl 0x28(%rax), %esi
leaq 0x238(%rsp), %rdi
callq 0x33d0
movq %rax, 0x10(%rsp)
jmp 0xac11
movq 0x10(%rsp), %rdi
movl $0x9, %esi
callq 0x3360
jmp 0xac22
leaq 0x1657(%rip), %rsi      # 0xc280
leaq 0x238(%rsp), %rdi
callq 0x30e0
jmp 0xac38
jmp 0xac52
leaq 0x95bc(%rip), %rsi      # 0x141fd
leaq 0x238(%rsp), %rdi
callq 0x3270
jmp 0xac50
jmp 0xac52
leaq 0x208(%rsp), %rdi
callq 0xcf20
movq %rax, %rsi
leaq 0x238(%rsp), %rdi
callq 0x3240
jmp 0xac71
movq 0x11350(%rip), %rsi     # 0x1bfc8
leaq 0x238(%rsp), %rdi
callq 0x32d0
jmp 0xac87
leaq 0x208(%rsp), %rdi
xorl %esi, %esi
callq 0xcf50
movq %rax, 0x180(%rsp)
jmp 0xaacc
movl $0x20, %edi
callq 0xceb0
movb %al, 0xf(%rsp)
jmp 0xacb3
movb 0xf(%rsp), %al
movb %al, 0x17f(%rsp)
movzbl 0x17f(%rsp), %esi
leaq 0x238(%rsp), %rdi
callq 0x3430
movq %rax, (%rsp)
jmp 0xacd9
movq (%rsp), %rdi
leaq 0x229c(%rip), %rsi      # 0xcf80
callq 0x30e0
jmp 0xaceb
leaq 0x238(%rsp), %rdi
callq 0x33f0
jmp 0xacfa
leaq 0x238(%rsp), %rdi
callq 0x3410
leaq 0x468(%rsp), %rdi
callq 0x3180
jmp 0xad25
leaq 0x468(%rsp), %rdi
callq 0x3180
jmp 0xad2d
addq $0x498, %rsp            # imm = 0x498
retq
movq 0x440(%rsp), %rdi
callq 0x3490
nopw (%rax,%rax)