// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movb 0x567b(%rip), %al       # 0x8180
testb %al, %al
je 0x2c69
movb 0x5685(%rip), %al       # 0x8198
testb %al, %al
je 0x2c99
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0x5657(%rip), %eax      # 0x8188
leal 0x1(%rax), %ecx
movl %ecx, 0x564e(%rip)      # 0x8188
cmpl 0x30(%r12), %eax
je 0x2c4b
movq 0x5644(%rip), %rdi      # 0x8190
leaq -0x60(%rbp), %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x20(%rax,%rcx)
jne 0x2c4b
movq %r15, -0x40(%rbp)
movq %r14, -0x38(%rbp)
movq -0x58(%rbp), %rdi
incq %rdi
callq 0x2030
movq %rax, %r14
movq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
leaq 0x257f(%rip), %rsi      # 0x5110
movq %r14, %rdi
callq 0x2210
movq %rax, %r13
leaq 0x2630(%rip), %rsi      # 0x51d3
xorl %edi, %edi
callq 0x2210
movq %rax, %r15
pushq $0x10
popq %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x20a0
movslq 0x24(%r12), %rcx
cqto
idivq %rcx
movl %eax, -0x30(%rbp)
cmpb $0x57, (%r15)
sete -0x29(%rbp)
pushq $0xc
popq %rdi
callq 0x2190
movq %rax, %r13
leaq 0x5587(%rip), %rsi      # 0x8168
leaq -0x30(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %rax, %rdi
callq 0x32d0
movq %r14, %rdi
callq 0x21d0
movl 0x557d(%rip), %ecx      # 0x817c
movl 0x28(%r12), %eax
cmpl %eax, %ecx
jne 0x2c0f
movq -0x40(%rbp), %rdx
movb $0x1, (%rdx)
decl %ecx
movl %ecx, 0x5565(%rip)      # 0x817c
jne 0x2c4e
movl %eax, 0x555d(%rip)      # 0x817c
xorl %eax, %eax
cmpl $0x1, 0x5540(%rip)      # 0x8168
sete %al
incl %eax
movq -0x38(%rbp), %rcx
cmpq %rcx, 0x5558(%rip)      # 0x8190
movl %eax, 0x552a(%rip)      # 0x8168
cmovneq %rcx, %rbx
movq %rbx, 0x5547(%rip)      # 0x8190
jmp 0x2c4e
xorl %r13d, %r13d
leaq -0x60(%rbp), %rdi
callq 0x20d0
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5510(%rip), %rdi      # 0x8180
callq 0x2260
testl %eax, %eax
je 0x2b0d
movl 0x28(%r12), %eax
movl %eax, 0x54f4(%rip)      # 0x817c
leaq 0x54f1(%rip), %rdi      # 0x8180
callq 0x2120
jmp 0x2b0d
leaq 0x54f8(%rip), %rdi      # 0x8198
callq 0x2260
testl %eax, %eax
je 0x2b1b
movq %r14, 0x54dc(%rip)      # 0x8190
leaq 0x54dd(%rip), %rdi      # 0x8198
callq 0x2120
jmp 0x2b1b
movq %rax, %rbx
movq %r13, %rdi
callq 0x2170
jmp 0x2cd9
jmp 0x2cd6
jmp 0x2cd6
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2250