// File: /workspace/github_cpp_makefile_star2_O3/iPapatsoris[P]Paging-Simulator/Simulator.cpp
// Function: Simulator::runWorkingSet(Address*, bool&)
void Simulator::runWorkingSet(Address *address, bool& processSwitch) {
	WorkingSet *workingSet = workingSetManager->getWorkingSetByProcess(address->getProcessId());
	if (processSwitch) {
		cout << "Restoring process " << address->getProcessId() << " working set:" << endl;
		processSwitch = false;
		bool change = false;
		this->restoreWorkingSetMemory(workingSet->getWorkingSet(), change);
		if (! change) {
			cout << "\tWorking set is already in memory" << endl;
		}
	}
	Address *victimAddress = NULL;
	workingSet->update(*address, &victimAddress);
	Address **mappingFrame = invertedPageTable.getFrameByAddress(*address);
	if (mappingFrame != NULL) {
		/* Page already in memory */
		this->checkWrite(address, *mappingFrame);
		delete address;
	}
	else {
		/* Page not in memory */
		statistics.pageFaults++;
		Address **freeFrame = invertedPageTable.getFreeFrame();
		if (freeFrame != NULL) {
			invertedPageTable.occupyFrame(freeFrame, address);
			printPageFault(NULL, address);
		}
		else {
			/* Remove a page not in working set.
			 * Prioritize the one just removed from working set (if any) */
			Address **victimFrame;
			if (victimAddress != NULL) {
				victimFrame = invertedPageTable.getFrameByAddress(*victimAddress);
			}
			else {
				victimFrame = invertedPageTable.getVictimFrameNotInSet(workingSet->getWorkingSet());
			}
			Address *toDelete = *victimFrame;
			*victimFrame = address;
			printPageFault(toDelete, address);
			if (toDelete->getDirty()) {
				cout << " Saving victim to disk.";
				statistics.diskWrites++;
			}
			delete toDelete;
		}
		cout << endl;
	}
	if (victimAddress != NULL) {
		delete victimAddress;
	}
	//workingSet->print();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movq 0x50(%rdi), %rdi
movl (%rsi), %eax
leaq -0x3c(%rbp), %rsi
movl %eax, (%rsi)
callq 0x40f6
movq %rax, %r15
cmpb $0x1, (%r13)
jne 0x315a
movq 0x4f4e(%rip), %r12      # 0x7fd8
leaq 0x20c3(%rip), %rsi      # 0x5154
movl $0x12, %edx
movq %r12, %rdi
callq 0x21c0
movl (%rbx), %esi
movq %r12, %rdi
callq 0x2250
movq %rax, %r12
leaq 0x20b5(%rip), %rsi      # 0x5167
movl $0xd, %edx
movq %rax, %rdi
callq 0x21c0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq %r15, %rdi
xorl %r15d, %r15d
movb %r15b, (%r13)
leaq -0x29(%rbp), %r12
movb %r15b, (%r12)
movq %rdi, %r13
callq 0x4244
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x33fa
cmpb %r15b, (%r12)
jne 0x3157
leaq 0x2059(%rip), %rsi      # 0x5175
movl $0x21, %edx
movq 0x4eb0(%rip), %r15      # 0x7fd8
movq %r15, %rdi
callq 0x21c0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq %r13, %r15
leaq -0x38(%rbp), %rdx
movq $0x0, (%rdx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4108
leaq 0x38(%r14), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3c08
testq %rax, %rax
je 0x319c
movq (%rax), %rdx
movq %rbx, %rsi
callq 0x3274
movq %rbx, %rdi
callq 0x2160
jmp 0x3257
incl 0x5c(%r14)
movq %r12, %rdi
callq 0x3c64
testq %rax, %rax
je 0x31c7
movq %r12, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3c8a
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3318
jmp 0x3229
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
je 0x31da
movq %r12, %rdi
callq 0x3c08
jmp 0x31ed
movq %r15, %rdi
callq 0x4244
movq %r12, %rdi
movq %rax, %rsi
callq 0x3c94
movq (%rax), %r15
movq %rbx, (%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3318
cmpb $0x1, 0x8(%r15)
jne 0x3221
movq 0x4dcc(%rip), %rdi      # 0x7fd8
leaq 0x1f29(%rip), %rsi      # 0x513c
movl $0x17, %edx
callq 0x21c0
incl 0x60(%r14)
movq %r15, %rdi
callq 0x2160
movq 0x4da8(%rip), %rbx      # 0x7fd8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3265
callq 0x2160
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq