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

	CacheEntry* entry = NULL;
	if(inNVM)
	{	
		assoc_victim = m_replacementPolicyNVM_ptr->evictPolicy(set);
		entry = m_tableNVM[set][assoc_victim];
	}
	else
	{
		assoc_victim = m_replacementPolicySRAM_ptr->evictPolicy(set);
		entry = m_tableSRAM[set][assoc_victim];		
	}

	if(!entry->isValid)
		return assoc_victim;

	allocDecision des = getDecision(entry);
	if(simu_parameters.perceptron_drawFeatureMaps)
	{
		bool global_error = false;
		
		if(( des == ALLOCATE_IN_SRAM && inNVM ) || \
			( des == ALLOCATE_IN_NVM && !inNVM ))
			global_error = true;

		int hash1 = hashing_function1(m_features_hash[0] , entry->address , entry->missPC);
		int hash2 = (m_features.size() > 1) ? hashing_function1(m_features_hash[1], entry->address , entry->missPC) : 0;
		if(global_error)
			stats_global_error[hash1][hash2].first++;
		else
			stats_global_error[hash1][hash2].second++;
		
	
		for(unsigned i = 0; i < m_features.size() ; i++)
		{
			int hash = hashing_function1( m_features_hash[i] , entry->address , entry->missPC);

			bool local_error = false;
			if( des == ALLOCATE_IN_NVM && entry->PHC_allocation_pred[i].second == ALLOCATE_IN_SRAM)
				local_error = true;
			else if(  des == ALLOCATE_IN_SRAM && entry->PHC_allocation_pred[i].second == ALLOCATE_IN_NVM )
			 	local_error = true;
			 					
			if(local_error)	
				stats_local_error[i][hash].first++;
			else 
				stats_local_error[i][hash].second++;
				
		}

	}
		
	if( (des == ALLOCATE_IN_NVM && !inNVM) || \
		(des == ALLOCATE_IN_SRAM && inNVM) )
	{
		if(simu_parameters.printDebug)
		{
			string a = inNVM ? "NVM" : "SRAM"; 
			debug_file << cpt_time << ": Eviction of 0x" << std::hex  << entry->address \
				   << std::dec << " , wrongly allocated in " << a << ", cost=" << entry->cost_value << endl;
			
			for(unsigned i = 0; i < m_features.size() ; i++)
			{
				int hash = hashing_function1( m_features_hash[i] , entry->address , entry->missPC);
				int confidence = m_features[i]->getConfidence(hash, false);
				allocDecision des = m_features[i]->getAllocDecision(hash, false);
				debug_file << "\t-Feature " << i << "\t" << str_allocDecision[des] << "\t" << confidence << endl;
			}
		}
		
		stats_wrong_alloc++;
	}
	else
		stats_good_alloc++;
		
	/* Trigger the learning process */ 
	if( entry->isLearning_weight && !simu_parameters.Cerebron_fastlearning)
		doLearning(entry, inNVM);
	

	if(entry->nbWrite == 0 && entry->nbRead == 0)
		stats_nbDeadLine[stats_nbDeadLine.size()-1]++;

	evictRecording(set , assoc_victim , inNVM);	
	return assoc_victim;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x60(%rdi), %rax
testq %rax, %rax
je 0xa204
movq %rdi, %r14
movq 0x68(%rdi), %rdi
testq %rdi, %rdi
je 0xa223
movl %esi, %ebp
movslq %esi, %rbx
testb %dl, %dl
cmovneq %rax, %rdi
movl %edx, 0x1c(%rsp)
movzbl %dl, %r15d
movq (%rdi), %rax
movl %ebx, %esi
callq *0x20(%rax)
movq 0x8(%r14,%r15,8), %rcx
movq (%rcx), %rsi
leaq (%rbx,%rbx,2), %rcx
movl %eax, %ebx
movslq %eax, %rdx
movq (%rsi,%rcx,8), %rax
movq (%rax,%rdx,8), %rcx
cmpb $0x1, (%rcx)
jne 0xa1f0
leaq 0x3c334(%rip), %rdx     # 0x45f50
cmpb $0x1, 0x226(%rdx)
movq %rcx, 0x10(%rsp)
movl %ebp, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movl %ebx, 0x20(%rsp)
jne 0x9c4c
movq 0x138(%rcx), %rax
cmpq 0x140(%rcx), %rax
seta 0xf(%rsp)
jmp 0x9c61
movsd 0x130(%rcx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setbe 0xf(%rsp)
cmpb $0x1, 0x1cc(%rdx)
jne 0x9e89
movq 0x288(%r14), %r12
leaq 0xe0(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
callq 0xcc1c
movq 0x20(%r12), %rax
movq %rax, 0x20(%rbx)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x20(%rax), %rdx
movq %rbx, %rdi
callq 0x15bec
movl %eax, %ebx
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
je 0x9ccf
callq 0x52a0
movq 0x278(%r14), %r12
subq 0x270(%r14), %r12
xorl %ebp, %ebp
cmpq $0x9, %r12
jb 0x9d32
movq 0x288(%r14), %rbp
leaq 0xb8(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x28(%rbp), %rsi
movq 0x30(%rbp), %rdx
addq %rsi, %rdx
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0xcc1c
movq 0x48(%rbp), %rax
movq %rax, 0x20(%r15)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x20(%rax), %rdx
movq %r15, %rdi
callq 0x15bec
movl %eax, %ebp
movb 0xf(%rsp), %r15b
xorb 0x1c(%rsp), %r15b
cmpq $0x9, %r12
jb 0x9d58
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9d58
callq 0x52a0
movslq %ebx, %rax
movq 0x490(%r14), %rcx
leaq (%rax,%rax,2), %rax
movslq %ebp, %rdx
movq (%rcx,%rax,8), %rax
leaq (%rax,%rdx,8), %rcx
leaq (%rax,%rdx,8), %rax
addq $0x4, %rax
testb %r15b, %r15b
cmovneq %rcx, %rax
incl (%rax)
movq 0x278(%r14), %rax
cmpq 0x270(%r14), %rax
je 0x9e89
leaq 0x90(%rsp), %rbx
movb 0xf(%rsp), %al
xorb $0x1, %al
movzbl %al, %eax
movl %eax, 0x24(%rsp)
movl $0x1, %r12d
xorl %r15d, %r15d
movq %r14, 0x30(%rsp)
movq 0x288(%r14), %rbp
leaq (%r15,%r15,4), %r13
movq %rbx, 0x80(%rsp)
movq (%rbp,%r13,8), %rsi
movq 0x8(%rbp,%r13,8), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0xcc1c
movq 0x20(%rbp,%r13,8), %rax
movq %rax, 0xa0(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x20(%rax), %rdx
movq %r14, %rdi
callq 0x15bec
movl %eax, %ebp
movq 0x80(%rsp), %rdi
cmpq %rbx, %rdi
je 0x9e1f
callq 0x52a0
movq 0x10(%rsp), %rax
movq 0xd0(%rax), %rdx
movslq %ebp, %rax
leaq (%r15,%r15,2), %rcx
movl 0x24(%rsp), %edi
cmpl %edi, 0x4(%rdx,%r15,8)
movq 0x30(%rsp), %r14
jne 0x9e53
movq 0x478(%r14), %rdx
shlq $0x3, %rax
addq (%rdx,%rcx,8), %rax
jmp 0x9e66
movq 0x478(%r14), %rdx
movq (%rdx,%rcx,8), %rcx
leaq (%rcx,%rax,8), %rax
addq $0x4, %rax
incl (%rax)
movq 0x278(%r14), %rax
subq 0x270(%r14), %rax
movl %r12d, %r15d
sarq $0x3, %rax
incl %r12d
cmpq %r15, %rax
ja 0x9db9
movl $0x430, %ecx            # imm = 0x430
movl 0x1c(%rsp), %edx
cmpb %dl, 0xf(%rsp)
movl 0x18(%rsp), %ebx
movq 0x28(%rsp), %r15
movq 0x10(%rsp), %r12
je 0xa18f
movl $0x438, %ecx            # imm = 0x438
leaq 0x3c09a(%rip), %rax     # 0x45f50
cmpb $0x1, (%rax)
jne 0xa18f
leaq 0x2a946(%rip), %rax     # 0x3480c
leaq 0x2a948(%rip), %rsi     # 0x34815
testb %dl, %dl
cmovneq %rax, %rsi
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
negq %r15
leaq (%rsi,%r15), %rdx
addq $0x4, %rdx
leaq 0x38(%rsp), %rdi
callq 0xccac
leaq 0x3c020(%rip), %rax     # 0x45f18
movq (%rax), %rsi
leaq 0x3b55e(%rip), %rdi     # 0x45460
callq 0x51e0
movq %rax, %rbx
leaq 0x2a30d(%rip), %rsi     # 0x3421e
movl $0x10, %edx
movq %rax, %rdi
callq 0x5350
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%rbx,%rax)
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x51e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rbx,%rax)
leaq 0x2a2d0(%rip), %rsi     # 0x3422f
movl $0x18, %edx
movq %rbx, %rdi
callq 0x5350
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x5350
movq %rax, %rbx
leaq 0x2a2c0(%rip), %rsi     # 0x34248
movl $0x7, %edx
movq %rax, %rdi
callq 0x5350
movsd 0x130(%r12), %xmm0
movq %rbx, %rdi
callq 0x5500
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x52b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x51f0
movq 0x278(%r14), %rax
cmpq 0x270(%r14), %rax
je 0xa16d
movl $0x1, %ebp
xorl %r15d, %r15d
movq 0x288(%r14), %rbx
leaq (%r15,%r15,4), %r12
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq (%rbx,%r12,8), %rsi
movq 0x8(%rbx,%r12,8), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r13
movq %r13, %rdi
callq 0xcc1c
leaq (%rbx,%r12,8), %rax
movq 0x20(%rax), %rax
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %r12
movq 0x8(%r12), %rsi
movq 0x20(%r12), %rdx
movq %r13, %rdi
callq 0x15bec
movl %eax, %ebx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0xa055
callq 0x52a0
movq 0x270(%r14), %rax
movq (%rax,%r15,8), %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x14a94
movl %eax, %r13d
movq 0x270(%r14), %rax
movq (%rax,%r15,8), %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x1493e
movl %eax, %ebx
movl $0xa, %edx
leaq 0x3b3d2(%rip), %rdi     # 0x45460
leaq 0x2a1bb(%rip), %rsi     # 0x34250
callq 0x5350
leaq 0x3b3bf(%rip), %rdi     # 0x45460
movq %r15, %rsi
callq 0x51e0
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2bbc0(%rip), %rsi     # 0x35c7b
callq 0x5350
movl %ebx, %eax
leaq 0x3b2c7(%rip), %rcx     # 0x45390
movq (%rcx,%rax,8), %rbx
testq %rbx, %rbx
je 0xa0ea
movq %rbx, %rdi
callq 0x5160
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5350
jmp 0xa104
movq (%r15), %rax
movq -0x18(%rax), %rax
movq %r15, %rdi
addq %rax, %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x5560
movl $0x1, %edx
movq %r15, %rdi
leaq 0x2bb68(%rip), %rsi     # 0x35c7b
callq 0x5350
movq %r15, %rdi
movl %r13d, %esi
callq 0x5580
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x52b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x51f0
movq 0x278(%r14), %rax
subq 0x270(%r14), %rax
movl %ebp, %r15d
sarq $0x3, %rax
incl %ebp
cmpq %r15, %rax
ja 0x9fed
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xa181
callq 0x52a0
movl 0x18(%rsp), %ebx
movq 0x28(%rsp), %r15
movl $0x438, %ecx            # imm = 0x438
incq (%r14,%rcx)
cmpb $0x1, 0x14a(%r12)
jne 0xa1bc
leaq 0x3bdab(%rip), %rax     # 0x45f50
cmpb $0x0, 0x225(%rax)
jne 0xa1bc
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x95e6
cmpl $0x0, 0x11c(%r12)
jne 0xa1dd
cmpl $0x0, 0x120(%r12)
jne 0xa1dd
movq 0x3a8(%r14), %rax
incq -0x8(%rax)
movq %r14, %rdi
movl %ebx, %esi
movl 0x20(%rsp), %ebx
movl %ebx, %edx
movl %r15d, %ecx
callq 0x2671a
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29f96(%rip), %rdi     # 0x341a1
leaq 0x29e7d(%rip), %rsi     # 0x3408f
leaq 0x29fab(%rip), %rcx     # 0x341c4
movl $0xf0, %edx
callq 0x51d0
leaq 0x29fd0(%rip), %rdi     # 0x341fa
leaq 0x29e5e(%rip), %rsi     # 0x3408f
leaq 0x29f8c(%rip), %rcx     # 0x341c4
movl $0xf1, %edx
callq 0x51d0
movq %rax, %r14
jmp 0xa2b3
movq %rax, %r14
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
jne 0xa2ae
jmp 0xa2b3
movq %rax, %r14
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
jne 0xa2ae
jmp 0xa2b3
jmp 0xa29c
jmp 0xa29c
movq %rax, %r14
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0xa29f
callq 0x52a0
jmp 0xa29f
jmp 0xa29c
movq %rax, %r14
movq 0x80(%rsp), %rdi
cmpq %rbx, %rdi
jne 0xa2ae
jmp 0xa2b3
movq %rax, %r14
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xa2b3
callq 0x52a0
movq %r14, %rdi
callq 0x55a0
nop