// File: /workspace/github_cpp_makefile_star2_O1/gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::evictPolicy(int, bool)
int
DBAMBPredictor::evictPolicy(int set, bool inNVM)
{	
	int assoc_victim = -1;
	assert(m_replacementPolicyNVM_ptr != NULL);
	assert(m_replacementPolicySRAM_ptr != NULL);

	CacheEntry* current = NULL;
	if(inNVM)
	{	
		assoc_victim = m_replacementPolicyNVM_ptr->evictPolicy(set);
		current = m_tableNVM[set][assoc_victim];
	}
	else
	{
		assoc_victim = m_replacementPolicySRAM_ptr->evictPolicy(set);
		current = m_tableSRAM[set][assoc_victim];		
	}
		
	//DPRINTF("DBAMBPredictor::evictPolicy set %d n assoc_victim %d \n" , set , assoc_victim);
	DHPEntry* rap_current = lookup(current->signature);
	//We didn't create an entry for this dataset, probably a good reason =) (instruction mainly) 
	if(rap_current != NULL)
	{
		if(rap_current->des != BYPASS_CACHE)
		{
			// A learning cache line on dead dataset goes here
			if( current->nbWrite == 0 && current->nbRead == 0)
				rap_current->dead_counter--;
			else
				rap_current->dead_counter++;
		
			if(rap_current->dead_counter > simu_parameters.deadSaturationCouter)
				rap_current->dead_counter = simu_parameters.deadSaturationCouter;
			else if(rap_current->dead_counter < -simu_parameters.deadSaturationCouter)
				rap_current->dead_counter = -simu_parameters.deadSaturationCouter;


			string a =  current->nbWrite == 0 && current->nbRead == 0 ? "DEAD" : "LIVELY";
			dataset_file << "Dataset n°" << rap_current->id << ": Eviction of a " << a << " CL, Address :" << \
				std::hex << current->address << std::dec << " DeadCounter=" << rap_current->dead_counter << endl;
				
			if(rap_current->dead_counter == -simu_parameters.deadSaturationCouter && simu_parameters.enableBP)
			{
				/* Switch the state of the dataset to dead */ 
				rap_current->des = BYPASS_CACHE;			
				dataset_file << "Dataset n°" << rap_current->id << ": Going into BYPASS mode " << endl;
				// Reset the window 
				RW_TYPE old_rw = rap_current->state_rw;
				RD_TYPE old_rd = rap_current->state_rd;

				rap_current->state_rd = RD_NOT_ACCURATE;
				rap_current->state_rw = DEAD;
				rap_current->rd_history.clear();
				rap_current->rw_history.clear();
				rap_current->cptWindow = 0;	
				rap_current->dead_counter = 0;

				// Monitor state switching 
				if(!m_isWarmup)
					stats_ClassErrors[old_rd + NUM_RD_TYPE*old_rw][rap_current->state_rd + NUM_RD_TYPE*rap_current->state_rw]++;
			}
		}
	}
	
	evictRecording(set , assoc_victim , inNVM);	
	return assoc_victim;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x60(%rdi), %rax
testq %rax, %rax
je 0x1028e
movq %rdi, %r14
movq 0x68(%rdi), %rdi
testq %rdi, %rdi
je 0x102ad
movl %esi, 0x4(%rsp)
movslq %esi, %r12
testb %dl, %dl
cmovneq %rax, %rdi
movzbl %dl, %r15d
movq (%rdi), %rax
movl %r12d, %esi
callq *0x20(%rax)
movl %eax, %ebp
movq 0x8(%r14,%r15,8), %rax
movq (%rax), %rax
leaq (%r12,%r12,2), %rcx
movslq %ebp, %rdx
movq (%rax,%rcx,8), %rax
movq (%rax,%rdx,8), %rbx
movl 0x338(%r14), %esi
testq %rsi, %rsi
je 0xffb2
movq 0x18(%rbx), %rcx
movl 0x33c(%r14), %edi
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
movq 0x2e8(%r14), %rax
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rax
xorl %edx, %edx
movq (%rax,%rdx,8), %r13
cmpq %rcx, 0x20(%r13)
je 0xffb5
incq %rdx
cmpq %rdx, %rsi
jne 0xffa0
xorl %r13d, %r13d
testq %r13, %r13
je 0x10262
cmpl $0x2, 0x28(%r13)
je 0x10262
movl 0x11c(%rbx), %eax
testl %eax, %eax
jne 0xffe0
cmpl $0x0, 0x120(%rbx)
je 0x10284
movl $0x1, %ecx
addl 0xb8(%r13), %ecx
movl %ecx, 0xb8(%r13)
leaq 0x35f56(%rip), %rdx     # 0x45f50
movl 0x9c(%rdx), %edx
cmpl %edx, %ecx
jg 0x1000a
negl %edx
cmpl %edx, %ecx
jge 0x10011
movl %edx, 0xb8(%r13)
testl %eax, %eax
je 0x1001e
leaq 0x24a9e(%rip), %r12     # 0x34aba
jmp 0x10037
cmpl $0x0, 0x120(%rbx)
leaq 0x24a89(%rip), %rax     # 0x34ab5
leaq 0x24a87(%rip), %r12     # 0x34aba
cmoveq %rax, %r12
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r12, %rdi
callq 0x5160
leaq (%rax,%r12), %rdx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0xccac
leaq 0x35608(%rip), %rdi     # 0x45668
leaq 0x24abb(%rip), %rsi     # 0x34b22
movl $0xb, %edx
callq 0x5350
movl 0x18(%r13), %esi
leaq 0x355ec(%rip), %rdi     # 0x45668
callq 0x5580
movq %rax, %r12
leaq 0x24a36(%rip), %rsi     # 0x34ac1
movl $0x10, %edx
movq %rax, %rdi
callq 0x5350
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rdi
callq 0x5350
movq %rax, %r12
leaq 0x24a1e(%rip), %rsi     # 0x34ad2
movl $0xe, %edx
movq %rax, %rdi
callq 0x5350
movq (%r12), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r12,%rax)
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x51e0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rax
movl 0x18(%r12,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r12,%rax)
leaq 0x249db(%rip), %rsi     # 0x34ae1
movl $0xd, %edx
movq %r12, %rdi
callq 0x5350
movl 0xb8(%r13), %esi
movq %r12, %rdi
callq 0x5580
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x52b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x51f0
movl 0xb8(%r13), %eax
leaq 0x35df6(%rip), %rcx     # 0x45f50
addl 0x9c(%rcx), %eax
jne 0x1024e
cmpb $0x1, 0xa8(%rcx)
jne 0x1024e
movl $0x2, 0x28(%r13)
leaq 0x354e6(%rip), %rdi     # 0x45668
leaq 0x24999(%rip), %rsi     # 0x34b22
movl $0xb, %edx
callq 0x5350
movl 0x18(%r13), %esi
leaq 0x354ca(%rip), %rdi     # 0x45668
callq 0x5580
movq %rax, %r12
leaq 0x24942(%rip), %rsi     # 0x34aef
movl $0x19, %edx
movq %rax, %rdi
callq 0x5350
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x52b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x51f0
movslq 0x98(%r13), %rcx
movslq 0x9c(%r13), %rax
movabsq $0x200000000, %rdx      # imm = 0x200000000
movq %rdx, 0x98(%r13)
movq 0x40(%r13), %rdx
cmpq %rdx, 0x48(%r13)
je 0x1020f
movq %rdx, 0x48(%r13)
movq 0x58(%r13), %rdx
movq %rdx, 0x68(%r13)
xorl %edx, %edx
movl %edx, 0x70(%r13)
movl %edx, 0x84(%r13)
movl %edx, 0xb8(%r13)
cmpb $0x0, 0x81(%r14)
jne 0x1024e
leaq (%rcx,%rcx,2), %rcx
addq %rcx, %rax
movq 0x3a0(%r14), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rax
incl 0x8(%rax)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x10262
callq 0x52a0
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x2671a
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0xffe5
leaq 0x23f0c(%rip), %rdi     # 0x341a1
leaq 0x247d0(%rip), %rsi     # 0x34a6c
leaq 0x247df(%rip), %rcx     # 0x34a82
movl $0x13d, %edx            # imm = 0x13D
callq 0x51d0
leaq 0x23f46(%rip), %rdi     # 0x341fa
leaq 0x247b1(%rip), %rsi     # 0x34a6c
leaq 0x247c0(%rip), %rcx     # 0x34a82
movl $0x13e, %edx            # imm = 0x13E
callq 0x51d0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x102e3
callq 0x52a0
movq %rbx, %rdi
callq 0x55a0
nop