// File: saumya-prakash[P]AssemblerEmulator/Assembler.cpp
// Function: Assembler::second_pass()
void Assembler::second_pass()
{
    for(list<struct line>::iterator it=lines.begin(); it!=lines.end(); ++it)
    {
        string s = it->s;

        istringstream iss(s);

        string mnemonic;

        iss>>mnemonic;

        map<string, pair<unsigned, unsigned> >::iterator mt=optab.find(mnemonic);

        if(mt==optab.end())     // unknown mnemonic error
        {
            errors.insert({it->line_no, Error(3)});
            continue;           // process next line
        } 

        unsigned i_type = mt->second.first;
        unsigned opcode = mt->second.second;

        if(i_type==0)   // 0-operand mnemonic
        {
            unsigned res = encode_zeroth(s, opcode, it->line_no);

            if(res!=0xffffffff)
                it->encoding = res;
        }

        else        // 1-operand mnemonic
        {
            unsigned res = encode_first(s, opcode, it->line_no, it->addr);

            if(res!=0xffffffff)
                it->encoding = res;
        }
    }


    return ;
}

// Assembly:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x2c0(%rsp)
movq 0x2c0(%rsp), %rdi
movq %rdi, 0x60(%rsp)
addq $0x60, %rdi
callq 0xc890
movq %rax, 0x2b8(%rsp)
movq 0x60(%rsp), %rdi
addq $0x60, %rdi
callq 0xc8e0
movq %rax, 0x2b0(%rsp)
leaq 0x2b8(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
callq 0xc8c0
testb $0x1, %al
jne 0x6841
jmp 0x6b9a
leaq 0x2b8(%rsp), %rdi
callq 0xc910
movq %rax, %rsi
leaq 0x290(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x3110
movq 0x58(%rsp), %rsi
leaq 0x110(%rsp), %rdi
movl $0x8, %edx
callq 0x3480
jmp 0x687c
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x33e0
movq 0x50(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x33a0
jmp 0x68a2
leaq 0x159ff(%rip), %rdi     # 0x1c2a8
leaq 0xe0(%rsp), %rsi
callq 0xc930
movq %rax, 0x48(%rsp)
jmp 0x68bd
movq 0x48(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x159d7(%rip), %rdi     # 0x1c2a8
callq 0xc980
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xc960
testb $0x1, %al
jne 0x68fc
jmp 0x6a1a
movq 0x60(%rsp), %rax
addq $0xc8, %rax
movq %rax, 0x38(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0xc910
addq $0x20, %rax
movq %rax, 0x40(%rsp)
leaq 0x88(%rsp), %rdi
movl $0x3, %esi
callq 0xc550
jmp 0x6936
movq 0x40(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0xc590
jmp 0x6952
movq 0x38(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xc520
movq %rax, 0x30(%rsp)
jmp 0x696b
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x5cb0
leaq 0x88(%rsp), %rdi
callq 0x5cd0
movl $0x4, 0x7c(%rsp)
jmp 0x6b2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
jmp 0x6b8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
jmp 0x6b71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
jmp 0x6a08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x5cb0
leaq 0x88(%rsp), %rdi
callq 0x5cd0
jmp 0x6b71
leaq 0xd8(%rsp), %rdi
callq 0xc9b0
movl 0x20(%rax), %eax
movl %eax, 0x78(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0xc9b0
movl 0x24(%rax), %eax
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x78(%rsp)
jne 0x6aa7
leaq 0x2b8(%rsp), %rdi
callq 0xc910
movq 0x60(%rsp), %rdi
movq %rax, %rcx
addq $0x20, %rcx
leaq 0x290(%rsp), %rsi
leaq 0x74(%rsp), %rdx
callq 0x93f0
movl %eax, 0x2c(%rsp)
jmp 0x6a7a
movl 0x2c(%rsp), %eax
movl %eax, 0x70(%rsp)
cmpl $-0x1, 0x70(%rsp)
je 0x6aa5
movl 0x70(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0xc910
movl 0x28(%rsp), %ecx
movl %ecx, 0x28(%rax)
jmp 0x6b22
leaq 0x2b8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0xc910
movq 0x10(%rsp), %rdi
addq $0x20, %rax
movq %rax, 0x18(%rsp)
callq 0xc910
movq 0x60(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %rax, %r8
addq $0x24, %r8
leaq 0x290(%rsp), %rsi
leaq 0x74(%rsp), %rdx
callq 0x9630
movl %eax, 0x24(%rsp)
jmp 0x6af5
movl 0x24(%rsp), %eax
movl %eax, 0x6c(%rsp)
cmpl $-0x1, 0x6c(%rsp)
je 0x6b20
movl 0x6c(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0xc910
movl 0xc(%rsp), %ecx
movl %ecx, 0x28(%rax)
jmp 0x6b22
movl $0x0, 0x7c(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x3180
leaq 0x110(%rsp), %rdi
callq 0x3140
leaq 0x290(%rsp), %rdi
callq 0x3180
movl 0x7c(%rsp), %eax
testl %eax, %eax
je 0x6b5d
jmp 0x6b5b
jmp 0x6b5f
jmp 0x6b5f
leaq 0x2b8(%rsp), %rdi
callq 0xc9d0
jmp 0x680d
leaq 0xe0(%rsp), %rdi
callq 0x3180
leaq 0x110(%rsp), %rdi
callq 0x3140
leaq 0x290(%rsp), %rdi
callq 0x3180
jmp 0x6ba2
addq $0x2c8, %rsp            # imm = 0x2C8
retq
movq 0x108(%rsp), %rdi
callq 0x3490
nop