// File: 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
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpb $0x0, 0x92bb(%rip)      # 0xc1e8
jne 0x2f5c
leaq 0x92b2(%rip), %rdi      # 0xc1e8
callq 0x2320
cmpl $0x0, %eax
je 0x2f5c
movq -0x90(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x9294(%rip)      # 0xc1e4
leaq 0x9291(%rip), %rdi      # 0xc1e8
callq 0x2160
cmpb $0x0, 0x929d(%rip)      # 0xc200
jne 0x2f8d
leaq 0x9294(%rip), %rdi      # 0xc200
callq 0x2320
cmpl $0x0, %eax
je 0x2f8d
movq -0x18(%rbp), %rax
movq %rax, 0x9277(%rip)      # 0xc1f8
leaq 0x9278(%rip), %rdi      # 0xc200
callq 0x2160
leaq -0x48(%rbp), %rdi
callq 0x2270
movq -0x90(%rbp), %rcx
movl 0x924d(%rip), %eax      # 0xc1f0
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x9242(%rip)      # 0xc1f0
cmpl 0x30(%rcx), %eax
je 0x2ff6
movq 0x923e(%rip), %rdi      # 0xc1f8
leaq -0x48(%rbp), %rsi
callq 0x2070
movq %rax, -0x98(%rbp)
jmp 0x2fcc
movq -0x98(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2330
movb %al, -0x99(%rbp)
jmp 0x2fea
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x2ff6
jmp 0x301b
movq $0x0, -0x8(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x31bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x31d1
leaq -0x48(%rbp), %rdi
callq 0x2290
movq %rax, %rdi
incq %rdi
callq 0x2030
movq %rax, -0xa8(%rbp)
jmp 0x3038
movq -0xa8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2100
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x21c0
movq -0x60(%rbp), %rdi
leaq 0x50ae(%rip), %rsi      # 0x811f
callq 0x22c0
movq %rax, -0x68(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x515d(%rip), %rsi      # 0x81e2
callq 0x22c0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x20d0
movq -0x90(%rbp), %rcx
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movslq 0x24(%rcx), %rcx
cqto
idivq %rcx
movl %eax, -0x7c(%rbp)
movb $0x0, -0x7d(%rbp)
movq -0x70(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x57, %eax
jne 0x30cf
movb $0x1, -0x7d(%rbp)
movl $0xc, %edi
callq 0x21f0
movq %rax, -0xb8(%rbp)
jmp 0x30e2
movq -0xb8(%rbp), %rdi
leaq 0x90e0(%rip), %rsi      # 0xc1d0
leaq -0x7c(%rbp), %rdx
leaq -0x7d(%rbp), %rcx
callq 0x4330
jmp 0x30ff
movq -0xb8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x312a
movq -0xc0(%rbp), %rdi
callq 0x2240
movq -0x90(%rbp), %rcx
movl 0x90ad(%rip), %eax      # 0xc1e4
cmpl 0x28(%rcx), %eax
jne 0x315f
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
jmp 0x315f
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x21d0
jmp 0x31d1
movl 0x907f(%rip), %eax      # 0xc1e4
addl $-0x1, %eax
movl %eax, 0x9076(%rip)      # 0xc1e4
cmpl $0x0, %eax
jne 0x31a9
movq -0x90(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x9061(%rip)      # 0xc1e4
leaq 0x9046(%rip), %rdi      # 0xc1d0
callq 0x39a0
jmp 0x3191
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x9058(%rip), %rdi      # 0xc1f8
callq 0x39d0
jmp 0x31a7
jmp 0x31a9
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x58(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2140
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x2140
movq -0x50(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)