// File: /workspace/github_cpp_makefile_star2_O3/iPapatsoris[P]Paging-Simulator/Simulator.cpp
// Function: Simulator::getTrace(std::basic_ifstream<char, std::char_traits<char>>&, std::basic_ifstream<char, std::char_traits<char>>&, bool&)
Address *Simulator::getTrace(ifstream& trace1, ifstream& trace2, bool& processSwitch) {
	static int processId = 1;
	static int curQuantum = quantum;
	static int referenceNumber = 0;
	static istream *trace = &trace1;
	string line;

	if (referenceNumber++ == maxReferences || getline(*trace, line).eof()) {
		return NULL;
	}
	char *c_line = new char[line.length()+1];
	strcpy(c_line, line.c_str());
	char *c_hexAddress = strtok(c_line, " ");
	char *c_operation = strtok(NULL, " \n\0");

	long decAddress = strtoul(c_hexAddress, NULL, 16);
	int pageNumber = decAddress / frameSize;
	bool dirty = false;
	if (*c_operation == 'W') {
		dirty = true;
	}
	Address *address = new Address(processId, pageNumber, dirty);
	delete[] c_line;

	/* Mark start of a new process for working set restoration */
	if (curQuantum == quantum) {
		processSwitch = true;
	}

	/* Switch trace between processes */
	if (! --curQuantum) {
		curQuantum = quantum;
		toggleProcessId(processId);
		toggleTrace(&trace, trace1, trace2);
	}
	return address;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movb 0x5479(%rip), %al       # 0x8188
testb %al, %al
je 0x2ea1
movb 0x5483(%rip), %al       # 0x81a0
testb %al, %al
je 0x2eae
leaq -0x58(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movl 0x5452(%rip), %eax      # 0x8190
leal 0x1(%rax), %ecx
movl %ecx, 0x5449(%rip)      # 0x8190
cmpl 0x30(%r12), %eax
je 0x2e7e
movq %r14, -0x48(%rbp)
movq 0x543b(%rip), %r14      # 0x8198
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %edx
leaq -0x68(%rbp), %rsi
movq %r14, %rdi
callq 0x2290
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x20(%rax,%rcx)
jne 0x2e7e
movq %r13, -0x38(%rbp)
movq %r15, -0x40(%rbp)
movq -0x60(%rbp), %rdi
incq %rdi
callq 0x2030
movq %rax, %r14
movq -0x68(%rbp), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0x237e(%rip), %rsi      # 0x513a
movq %r14, %rdi
callq 0x2230
movq %rax, %r13
leaq 0x242f(%rip), %rsi      # 0x51fd
xorl %edi, %edi
callq 0x2230
movq %rax, %r15
movq %r13, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x20a0
movslq 0x24(%r12), %rcx
cqto
idivq %rcx
movl %eax, -0x30(%rbp)
cmpb $0x57, (%r15)
sete -0x29(%rbp)
movl $0xc, %edi
callq 0x2190
movq %rax, %r13
leaq 0x5360(%rip), %rsi      # 0x8170
leaq -0x30(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %rax, %rdi
callq 0x3548
movq %r14, %rdi
callq 0x21f0
movl 0x5356(%rip), %ecx      # 0x8184
movl 0x28(%r12), %eax
cmpl %eax, %ecx
jne 0x2e3e
movq -0x48(%rbp), %rdx
movb $0x1, (%rdx)
decl %ecx
movl %ecx, 0x533e(%rip)      # 0x8184
jne 0x2e81
movl %eax, 0x5336(%rip)      # 0x8184
xorl %eax, %eax
cmpl $0x1, 0x5319(%rip)      # 0x8170
sete %al
incl %eax
movq -0x38(%rbp), %rcx
cmpq %rcx, 0x5331(%rip)      # 0x8198
movl %eax, 0x5303(%rip)      # 0x8170
movq -0x40(%rbp), %rax
cmovneq %rcx, %rax
movq %rax, 0x531c(%rip)      # 0x8198
jmp 0x2e81
xorl %r13d, %r13d
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2e8f
callq 0x2160
movq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x22c0
jmp 0x2d17
movq %r13, %rdi
callq 0x22fb
jmp 0x2d25
movq %rax, %r14
movq %r13, %rdi
callq 0x2160
jmp 0x2ecf
jmp 0x2ecc
jmp 0x2ecc
movq %rax, %r14
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2edd
callq 0x2160
movq %r14, %rdi
callq 0x2260
nop