// File: /workspace/github_cpp_makefile_star2_O1/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 0x5461(%rip), %al       # 0x8190
testb %al, %al
je 0x2ec5
movb 0x546b(%rip), %al       # 0x81a8
testb %al, %al
je 0x2ed2
leaq -0x58(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movl 0x543a(%rip), %eax      # 0x8198
leal 0x1(%rax), %ecx
movl %ecx, 0x5431(%rip)      # 0x8198
cmpl 0x30(%r12), %eax
je 0x2ea2
movq %r14, -0x48(%rbp)
movq 0x5423(%rip), %r14      # 0x81a0
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 0x22a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x20(%rax,%rcx)
jne 0x2ea2
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 0x236d(%rip), %rsi      # 0x5149
movq %r14, %rdi
callq 0x2240
movq %rax, %r13
leaq 0x241e(%rip), %rsi      # 0x520c
xorl %edi, %edi
callq 0x2240
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)
movb $0x0, -0x29(%rbp)
cmpb $0x57, (%r15)
sete -0x29(%rbp)
movl $0xc, %edi
callq 0x2190
movq %rax, %r13
leaq 0x5344(%rip), %rsi      # 0x8178
leaq -0x30(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %rax, %rdi
callq 0x356c
movq %r14, %rdi
callq 0x21f0
movl 0x533a(%rip), %ecx      # 0x818c
movl 0x28(%r12), %eax
cmpl %eax, %ecx
jne 0x2e62
movq -0x48(%rbp), %rdx
movb $0x1, (%rdx)
decl %ecx
movl %ecx, 0x5322(%rip)      # 0x818c
jne 0x2ea5
movl %eax, 0x531a(%rip)      # 0x818c
xorl %eax, %eax
cmpl $0x1, 0x52fd(%rip)      # 0x8178
sete %al
incl %eax
movq -0x38(%rbp), %rcx
cmpq %rcx, 0x5315(%rip)      # 0x81a0
movl %eax, 0x52e7(%rip)      # 0x8178
movq -0x40(%rbp), %rax
cmovneq %rcx, %rax
movq %rax, 0x5300(%rip)      # 0x81a0
jmp 0x2ea5
xorl %r13d, %r13d
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2eb3
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 0x22d0
jmp 0x2d37
movq %r13, %rdi
callq 0x230b
jmp 0x2d45
movq %rax, %r14
movq %r13, %rdi
callq 0x2160
jmp 0x2ef3
jmp 0x2ef0
jmp 0x2ef0
movq %rax, %r14
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2f01
callq 0x2160
movq %r14, %rdi
callq 0x2270
nop