// File: AbraaoHonorio[P]Page-Replacement-Algorithms/PageReplacement.cpp
// Function: findIndex(std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>, int)
int findIndex(std::vector<int> myQueue, std::vector<int> myPages, int nPages)
{
	int index = 0;


	// erase the first nPages elements
	myPages.erase(myPages.begin(), myPages.begin() + nPages);

	// initialize an vector Priority with 1000
	std::vector<int> arrayPriority(myQueue.size(),1000);

	if (!myPages.empty())
	{
		for (unsigned int i = 0; i < myQueue.size(); i++)
		{
			for (unsigned int j = 0; j < myPages.size(); j++)
			{
				if (myQueue[i] == myPages[j])
				{
					arrayPriority[i] = j;
					break;
				}

			}
		}
		/*					DEBUG                             */
		/*std::cout << "\n ***  arrayPriority *** " << std::endl; //DEBUG
		for each (int element in arrayPriority)
		{
		std::cout << index << " *** " << element << " " << std::endl; //DEBUG
		}*/


		/* return o index with highest priority*/
		int winner = arrayPriority[0];
		for (unsigned int i = 0; i < arrayPriority.size(); i++)
		{
			if (winner < arrayPriority[i])
			{
				winner = arrayPriority[i];
				index = i;
			}
		}
	}


	return (index);
}

// Assembly:
subq $0xd8, %rsp
movq %rsi, 0x28(%rsp)
movq %rdi, %rax
movq 0x28(%rsp), %rdi
movq %rax, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq %rax, 0xd0(%rsp)
movq %rdi, 0xc8(%rsp)
movl %edx, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
callq 0x36e0
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3860
movq 0x30(%rsp), %rdi
callq 0x36e0
movq %rax, 0x98(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x3940
movq %rax, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3860
movq 0x30(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x38a0
movq 0x38(%rsp), %rdi
movq %rax, 0x90(%rsp)
callq 0x37d0
movq %rax, 0x40(%rsp)
movl $0x3e8, 0x74(%rsp)      # imm = 0x3E8
leaq 0x73(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x3980
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rcx
leaq 0x78(%rsp), %rdi
leaq 0x74(%rsp), %rdx
callq 0x3990
jmp 0x2979
leaq 0x73(%rsp), %rdi
callq 0x3a10
movq 0x30(%rsp), %rdi
callq 0x35f0
testb $0x1, %al
jne 0x2b10
movl $0x0, 0x60(%rsp)
movq 0x38(%rsp), %rdi
movl 0x60(%rsp), %eax
movq %rax, 0x20(%rsp)
callq 0x37d0
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x2a7f
movl $0x0, 0x5c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x5c(%rsp), %eax
movq %rax, 0x18(%rsp)
callq 0x37d0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x2a6d
movq 0x38(%rsp), %rdi
movl 0x60(%rsp), %eax
movl %eax, %esi
callq 0x3740
movq 0x30(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, %esi
callq 0x3740
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpl (%rcx), %eax
jne 0x2a5b
movl 0x5c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x60(%rsp), %eax
movl %eax, %esi
leaq 0x78(%rsp), %rdi
callq 0x3740
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x2a6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x73(%rsp), %rdi
callq 0x3a10
jmp 0x2b2f
jmp 0x2a5d
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x29c9
jmp 0x2a6f
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x299d
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3740
movl (%rax), %eax
movl %eax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq %rax, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x37d0
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x2b0e
movl 0x58(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x54(%rsp), %eax
movl %eax, %esi
leaq 0x78(%rsp), %rdi
callq 0x3740
movq %rax, %rcx
movl 0x4(%rsp), %eax
cmpl (%rcx), %eax
jge 0x2aff
movl 0x54(%rsp), %eax
movl %eax, %esi
leaq 0x78(%rsp), %rdi
callq 0x3740
movl (%rax), %eax
movl %eax, 0x58(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0xc0(%rsp)
jmp 0x2b01
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x2a9b
jmp 0x2b10
movl 0xc0(%rsp), %eax
movl %eax, (%rsp)
leaq 0x78(%rsp), %rdi
callq 0x3590
movl (%rsp), %eax
addq $0xd8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x2190
nopl (%rax)