// File: /workspace/github_cpp_makefile_star2_O3/AbraaoHonorio[P]Page-Replacement-Algorithms/PageReplacement.cpp
// Function: PageReplacement::lru()
void PageReplacement::lru(void)
{
	/* initializing */
	pageFaults_m = 0;
	myQueue.clear();


	/*inserting the value to queue*/
	for (int i = (sizeRam_m-1); i>=0; i--)
	{
	    if (std::find(myQueue.begin(), myQueue.end(), myPages[i]) != myQueue.end())
				;
        else
        {
            pageFaults_m++;
            myQueue.push_back(myPages[i]);
        }
	}

	// deleting Erase the first sizeRam_m elements
	myPages.erase(myPages.begin(), myPages.begin() + sizeRam_m);

	if (!myPages.empty() && !myQueue.empty()) //verify exist elemen t
	{
		for (unsigned int i = 0; i <myPages.size(); i++)
		{
				auto it = std::find(myQueue.begin(), myQueue.end(), myPages[i]);
				if (it != myQueue.end())
				{
					auto index = std::distance(myQueue.begin(), it); // calculating the index of the element
					myQueue.erase(myQueue.begin() + index); // deleting the element
					myQueue.insert(myQueue.begin(), myPages[i]); //Inserting the element at the top( highest priority )

				}
				else
				{
					myQueue.insert(myQueue.begin(), myPages[i]); //Inserting the element at the top( highest  priority )
					myQueue.erase(myQueue.end()-1); //delete to lower priority
					pageFaults_m++;
				}
		}
	}
	else
	{
		std::cout << " The Queue have empty or sizeRam is null " << std::endl;
	}

	std::cout << " LRU  " << pageFaults_m << " " << std::endl; //output
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movl $0x0, 0x8(%rdi)
movq 0x10(%rdi), %rax
cmpq %rax, 0x18(%rdi)
je 0x196a
movq %rax, 0x18(%rbx)
leaq 0x10(%rbx), %r14
movslq 0xc(%rbx), %r12
testq %r12, %r12
jle 0x19d6
leaq -0x4(,%r12,4), %r15
incq %r12
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %r15, %rdx
callq 0x1f34
movq 0x18(%rbx), %rsi
cmpq %rsi, %rax
jne 0x19c5
incl 0x8(%rbx)
movq 0x28(%rbx), %rdx
addq %r15, %rdx
cmpq 0x20(%rbx), %rax
je 0x19bd
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rbx)
jmp 0x19c5
movq %r14, %rdi
callq 0x1fda
decq %r12
addq $-0x4, %r15
cmpq $0x1, %r12
ja 0x1982
movl 0xc(%rbx), %r12d
movq 0x30(%rbx), %rdx
testl %r12d, %r12d
je 0x1a1b
movq 0x28(%rbx), %r15
movslq %r12d, %rax
leaq (%r15,%rax,4), %r12
subq %r12, %rdx
je 0x1a03
movq %r15, %rdi
movq %r12, %rsi
callq 0x1150
movq 0x30(%rbx), %rdx
movq %rdx, %rax
jmp 0x1a09
movq %r12, %rdx
movq %r12, %rax
subq %r12, %rax
addq %rax, %r15
cmpq %r15, %rdx
je 0x1a1b
movq %r15, 0x30(%rbx)
movq %r15, %rdx
movq 0x28(%rbx), %rax
cmpq %rdx, %rax
je 0x1af4
movq 0x10(%rbx), %rcx
cmpq 0x18(%rbx), %rcx
je 0x1af4
movl $0x1, %ebp
xorl %r15d, %r15d
movb $0x1, %r12b
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
leaq (%rax,%r15,4), %rdx
callq 0x1f34
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
cmpq %rdx, %rax
je 0x1a9b
leaq 0x4(%rax), %rcx
cmpq %rdx, %rcx
je 0x1a7e
subq %rcx, %rdx
movq %rax, %rdi
movq %rcx, %rsi
callq 0x1150
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
addq $-0x4, %rdx
movq %rdx, 0x18(%rbx)
shlq $0x2, %r15
addq 0x28(%rbx), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x1eba
jmp 0x1ad5
shlq $0x2, %r15
addq 0x28(%rbx), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x1eba
movq 0x18(%rbx), %rsi
testb %r12b, %r12b
jne 0x1aca
leaq -0x4(%rsi), %rdi
movq %rsi, %rdx
subq %rsi, %rdx
callq 0x1150
movq 0x18(%rbx), %rsi
addq $-0x4, %rsi
movq %rsi, 0x18(%rbx)
incl 0x8(%rbx)
movl %ebp, %r15d
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
incl %ebp
cmpq %r15, %rcx
ja 0x1a41
jmp 0x1b36
movq 0x34dd(%rip), %r14      # 0x4fd8
leaq 0x153e(%rip), %rsi      # 0x3040
movl $0x29, %edx
movq %r14, %rdi
callq 0x1120
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
movq 0x349b(%rip), %r14      # 0x4fd8
leaq 0x1536(%rip), %rsi      # 0x307a
movl $0x6, %edx
movq %r14, %rdi
callq 0x1120
movl 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x1160
movq %rax, %rbx
leaq 0x1512(%rip), %rsi      # 0x3078
movl $0x1, %edx
movq %rax, %rdi
callq 0x1120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10a0