// File: /workspace/github_cpp_makefile_star2_O1/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 0x410a
movq %rax, %r15
cmpb $0x1, (%r13)
jne 0x317e
movq 0x4f2a(%rip), %r12      # 0x7fd8
leaq 0x20ae(%rip), %rsi      # 0x5163
movl $0x12, %edx
movq %r12, %rdi
callq 0x21c0
movl (%rbx), %esi
movq %r12, %rdi
callq 0x2260
movq %rax, %r12
leaq 0x20a0(%rip), %rsi      # 0x5176
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 0x425e
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x341e
cmpb %r15b, (%r12)
jne 0x317b
leaq 0x2044(%rip), %rsi      # 0x5184
movl $0x21, %edx
movq 0x4e8c(%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 0x411c
leaq 0x38(%r14), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3c32
testq %rax, %rax
je 0x31c0
movq (%rax), %rdx
movq %rbx, %rsi
callq 0x3298
movq %rbx, %rdi
callq 0x2160
jmp 0x327b
incl 0x5c(%r14)
movq %r12, %rdi
callq 0x3c8a
testq %rax, %rax
je 0x31eb
movq %r12, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3cb0
xorl %edi, %edi
movq %rbx, %rsi
callq 0x333c
jmp 0x324d
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
je 0x31fe
movq %r12, %rdi
callq 0x3c32
jmp 0x3211
movq %r15, %rdi
callq 0x425e
movq %r12, %rdi
movq %rax, %rsi
callq 0x3cba
movq (%rax), %r15
movq %rbx, (%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x333c
cmpb $0x1, 0x8(%r15)
jne 0x3245
movq 0x4da8(%rip), %rdi      # 0x7fd8
leaq 0x1f14(%rip), %rsi      # 0x514b
movl $0x17, %edx
callq 0x21c0
incl 0x60(%r14)
movq %r15, %rdi
callq 0x2160
movq 0x4d84(%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 0x3289
callq 0x2160
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq