// File: /workspace/github_cpp_makefile_star2_O3/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 0x10225
movq %rdi, %r14
movq 0x68(%rdi), %rdi
testq %rdi, %rdi
je 0x10244
movl %esi, %ebx
testb %dl, %dl
cmovneq %rax, %rdi
movzbl %dl, %eax
movq %rax, (%rsp)
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, %ebp
movl 0x338(%r14), %esi
testq %rsi, %rsi
je 0x101fa
movslq %ebx, %rax
movq (%rsp), %rcx
movq 0x8(%r14,%rcx,8), %rcx
movq (%rcx), %rcx
leaq (%rax,%rax,2), %rax
movslq %ebp, %rdx
movq (%rcx,%rax,8), %rax
movq (%rax,%rdx,8), %r15
movq 0x18(%r15), %rcx
movl 0x33c(%r14), %r8d
xorl %edi, %edi
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq 0x2e8(%r14), %rax
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rax
movq (%rax,%rdi,8), %r13
cmpq %rcx, 0x20(%r13)
je 0xff53
incq %rdi
cmpq %rdi, %rsi
jne 0xff3c
jmp 0x101fa
cmpl $0x2, 0x28(%r13)
je 0x101fa
movl 0x11c(%r15), %eax
testl %eax, %eax
jne 0xff77
cmpl $0x0, 0x120(%r15)
je 0x1021b
movl $0x1, %ecx
addl 0xb8(%r13), %ecx
movl %ecx, 0xb8(%r13)
leaq 0x35fbf(%rip), %rdx     # 0x45f50
movl 0x9c(%rdx), %edx
cmpl %edx, %ecx
jg 0xffa1
negl %edx
cmpl %edx, %ecx
jge 0xffa8
movl %edx, 0xb8(%r13)
testl %eax, %eax
je 0xffb5
leaq 0x24ae7(%rip), %r12     # 0x34a9a
jmp 0xffcf
cmpl $0x0, 0x120(%r15)
leaq 0x24ad1(%rip), %rax     # 0x34a95
leaq 0x24acf(%rip), %r12     # 0x34a9a
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 0xcc6c
leaq 0x35670(%rip), %rdi     # 0x45668
leaq 0x24b03(%rip), %rsi     # 0x34b02
movl $0xb, %edx
callq 0x5360
movl 0x18(%r13), %esi
leaq 0x35654(%rip), %rdi     # 0x45668
callq 0x5580
movq %rax, %r12
leaq 0x24a7e(%rip), %rsi     # 0x34aa1
movl $0x10, %edx
movq %rax, %rdi
callq 0x5360
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rdi
callq 0x5360
movq %rax, %r12
leaq 0x24a66(%rip), %rsi     # 0x34ab2
movl $0xe, %edx
movq %rax, %rdi
callq 0x5360
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(%r15), %rsi
movq %r12, %rdi
callq 0x51f0
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 0x24a23(%rip), %rsi     # 0x34ac1
movl $0xd, %edx
movq %r12, %rdi
callq 0x5360
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 0x52c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5200
movl 0xb8(%r13), %eax
leaq 0x35e5e(%rip), %rcx     # 0x45f50
addl 0x9c(%rcx), %eax
jne 0x101e6
cmpb $0x1, 0xa8(%rcx)
jne 0x101e6
movl $0x2, 0x28(%r13)
leaq 0x3554e(%rip), %rdi     # 0x45668
leaq 0x249e1(%rip), %rsi     # 0x34b02
movl $0xb, %edx
callq 0x5360
movl 0x18(%r13), %esi
leaq 0x35532(%rip), %rdi     # 0x45668
callq 0x5580
movq %rax, %r12
leaq 0x2498a(%rip), %rsi     # 0x34acf
movl $0x19, %edx
movq %rax, %rdi
callq 0x5360
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x52c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5200
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 0x101a7
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 0x101e6
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 0x101fa
callq 0x52b0
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
movq (%rsp), %rcx
callq 0x264ec
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 0xff7c
leaq 0x23f49(%rip), %rdi     # 0x34175
leaq 0x24819(%rip), %rsi     # 0x34a4c
leaq 0x24828(%rip), %rcx     # 0x34a62
movl $0x13d, %edx            # imm = 0x13D
callq 0x51d0
leaq 0x23f83(%rip), %rdi     # 0x341ce
leaq 0x247fa(%rip), %rsi     # 0x34a4c
leaq 0x24809(%rip), %rcx     # 0x34a62
movl $0x13e, %edx            # imm = 0x13E
callq 0x51d0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1027a
callq 0x52b0
movq %rbx, %rdi
callq 0x55a0