// File: /workspace/github_cpp_makefile_star2_O2/czmccrory[P]ManchesterBaby/assembler.cpp
// Function: Assembler::assembleLineSecondPass(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, int*, std::_List_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>)
void Assembler::assembleLineSecondPass(string* line, int* lineCounter, list<string>::iterator it)
{
	string bufferLine;

	string parsed = parse(line);

	if (parsed == "")
	{
		return;
	}

	// if theres a "LABEL": add it to symbol table along with current line number as address
	if (parsed.find(":") != string::npos)
	{
		string label = parsed.substr(0, parsed.length()-1);

		addVariableToSymbolTable(label, *lineCounter);

		parsed = parse(line);

	}

	// if "VAR", declareVar(next word) and push to buffer
	if (parsed == "VAR")
	{
		int var;
		stringstream ss;
		ss << parse(line);
		ss >> var;
		

		bufferLine = declareVariable(var);

		(*lineCounter)++;
		it = buffer.insert(it, bufferLine);
		cout << "Machine Code: " << *it << "\n" << endl;
	}
	else if(iSet.contains(parsed)) // recognised instruction
	{
		if (parsed != "STP" && parsed != "CMP" && parsed != "INC" && parsed != "DEC") // requires operand
		{
			string instrOperand = parse(line);
			if (instrOperand.find("#") == string::npos) // absolute addressing (mem address)
			{
				bufferLine = encodeInstructionAddress(parsed, instrOperand);
			}
			else //direct addressing (#value)
			{
				int value;
				stringstream ss;
				// remove #
				instrOperand = instrOperand.substr(1, instrOperand.length() - 1); 
				//convert to int
				ss << instrOperand;
				ss >> value;
				// encode
				bufferLine = encodeInstructionValue(parsed, value);
			}
		}
		else // blank operand
		{
			bufferLine = encodeInstructionAddress(parsed, "");
		}

		(*lineCounter) = (*lineCounter + 1);

		it = buffer.insert(it, bufferLine);
		cout << "Machine Code: " << *it << "\n" << endl;

 	}
 	else
 	{

 	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x28(%rsp), %rdi
movq %rsi, %rdx
callq 0x38d6
leaq 0x232c(%rip), %rsi      # 0x60e4
leaq 0x28(%rsp), %rdi
callq 0x4949
testb %al, %al
jne 0x41c3
leaq 0x2366(%rip), %rsi      # 0x6137
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x2360
cmpq $-0x1, %rax
je 0x3e6b
leaq 0x28(%rsp), %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x170(%rsp), %rdi
xorl %edx, %edx
callq 0x2210
leaq 0x150(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x2250
movl (%r15), %edx
leaq 0x150(%rsp), %rsi
movq %r14, %rdi
callq 0x3b48
leaq 0x150(%rsp), %rdi
callq 0x20e0
leaq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x38d6
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x20e0
leaq 0x170(%rsp), %rdi
callq 0x20e0
leaq 0x225e(%rip), %rsi      # 0x60d0
leaq 0x28(%rsp), %rdi
callq 0x4949
testb %al, %al
je 0x3f5d
leaq 0x170(%rsp), %rdi
callq 0x2160
leaq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x38d6
leaq 0x180(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x21d0
leaq 0x8(%rsp), %rdi
callq 0x20e0
leaq 0x170(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2150
movl 0x70(%rsp), %esi
leaq 0x8(%rsp), %rdi
pushq $0x20
popq %rdx
callq 0x2dd6
leaq 0x50(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x20e0
incl (%r15)
addq $0x20, %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4960
movq %rax, %rbx
movq 0x60b7(%rip), %rdi      # 0x9fd0
leaq 0x21b4(%rip), %rsi      # 0x60d4
callq 0x21e0
addq $0x10, %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
leaq 0x21a8(%rip), %rsi      # 0x60e3
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
leaq 0x170(%rsp), %rdi
callq 0x2190
jmp 0x41c3
leaq 0x130(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2250
leaq 0x130(%rsp), %rsi
movq %r14, %rdi
callq 0x289c
movl %eax, %ebp
leaq 0x130(%rsp), %rdi
callq 0x20e0
testb %bpl, %bpl
je 0x41c3
leaq 0x207a(%rip), %rsi      # 0x6018
leaq 0x28(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x40ee
leaq 0x205d(%rip), %rsi      # 0x6014
leaq 0x28(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x40ee
leaq 0x205f(%rip), %rsi      # 0x602f
leaq 0x28(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x40ee
leaq 0x204a(%rip), %rsi      # 0x6033
leaq 0x28(%rsp), %rdi
callq 0x4956
testb %al, %al
je 0x40ee
leaq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x38d6
leaq 0x20d6(%rip), %rsi      # 0x60e5
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x2360
cmpq $-0x1, %rax
je 0x41e9
leaq 0x170(%rsp), %rdi
callq 0x2160
leaq 0x8(%rsp), %rsi
movq 0x8(%rsi), %rcx
decq %rcx
leaq 0x70(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2210
leaq 0x8(%rsp), %r12
leaq 0x70(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x20e0
leaq 0x180(%rsp), %rdi
movq %r12, %rsi
callq 0x21d0
leaq 0x170(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
callq 0x2150
leaq 0xd0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2250
movl 0x4c(%rsp), %ecx
leaq 0x70(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movq %r14, %rsi
callq 0x4370
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x2230
movq %r12, %rdi
callq 0x20e0
leaq 0xd0(%rsp), %rdi
callq 0x20e0
leaq 0x170(%rsp), %rdi
callq 0x2190
jmp 0x4264
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2250
leaq 0x1fdd(%rip), %rsi      # 0x60e4
leaq 0x90(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x4630
leaq 0x170(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %r14, %rsi
callq 0x3c40
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2230
movq %r12, %rdi
callq 0x20e0
leaq 0x90(%rsp), %rdi
callq 0x20e0
leaq 0xb0(%rsp), %rdi
callq 0x20e0
incl (%r15)
addq $0x20, %r14
leaq 0x50(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4960
movq %rax, %rbx
movq 0x5e3f(%rip), %rdi      # 0x9fd0
leaq 0x1f3c(%rip), %rsi      # 0x60d4
callq 0x21e0
addq $0x10, %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
leaq 0x1f30(%rip), %rsi      # 0x60e3
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
leaq 0x28(%rsp), %rdi
callq 0x20e0
leaq 0x50(%rsp), %rdi
callq 0x20e0
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x110(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2250
leaq 0xf0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2250
leaq 0x170(%rsp), %rdi
leaq 0x110(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
movq %r14, %rsi
callq 0x3c40
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %r12
movq %r12, %rsi
callq 0x2230
movq %r12, %rdi
callq 0x20e0
leaq 0xf0(%rsp), %rdi
callq 0x20e0
leaq 0x110(%rsp), %rdi
callq 0x20e0
leaq 0x8(%rsp), %rdi
jmp 0x416b
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x20e0
jmp 0x4283
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x20e0
jmp 0x42c4
jmp 0x42aa
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x20e0
jmp 0x42b7
jmp 0x42b4
jmp 0x42b4
movq %rax, %rbx
jmp 0x42c4
jmp 0x433a
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x2190
leaq 0x8(%rsp), %rdi
jmp 0x4331
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20e0
jmp 0x42e0
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x4331
jmp 0x433a
jmp 0x433a
jmp 0x4344
jmp 0x4344
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20e0
jmp 0x4347
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
jmp 0x4331
jmp 0x4344
jmp 0x433a
jmp 0x4326
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x20e0
jmp 0x4329
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x20e0
jmp 0x4354
jmp 0x433a
movq %rax, %rbx
jmp 0x4354
movq %rax, %rbx
jmp 0x435e
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x2190
leaq 0x28(%rsp), %rdi
callq 0x20e0
leaq 0x50(%rsp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x22f0