// File: gvaumour[P]cache-simulator/src/Cerebron.cc
// Function: CerebronPredictor::insertionPolicy(unsigned long, unsigned long, bool, Access)
void
CerebronPredictor::insertionPolicy(uint64_t set, uint64_t index, bool inNVM, Access element)
{
	CacheEntry* entry = get_entry(set , index , inNVM);
	entry->missPC = element.m_pc;
	RD_TYPE rd = RD_NOT_ACCURATE;
	if( element.isSRAMerror)
	{
		stats_cptSRAMerror++;
		rd = RD_MEDIUM;	
	}
	entry->policyInfo = m_cpt++;
			

	if(entry->isLearning_policy)
		recordAccess(entry, element.block_addr, entry->missPC, set , element.isWrite() , inNVM, index, rd , false);

	if(entry->isLearning_weight)
	{
		if( element.isSRAMerror)
		{
			debug_file << "SRAM error detected on Block Addr = 0x" \
					<< std::hex << element.block_addr << std::dec << endl;
			
			uint64_t missPC = missingTagMissPC(element.block_addr , set);
			uint64_t cost_value = missingTagCostValue(element.block_addr , set);
			entry->missPC = missPC;
			for(unsigned i = 0 ; i < m_features.size() ; i++)
			{
				int hash = hashing_function1(m_features_hash[i] , element.block_addr , entry->missPC);
				m_features[i]->decreaseConfidence(hash);
				if(cost_value > 0)
					m_features[i]->decreaseConfidence(hash);
			}
			stats_cptLearningSRAMerror++;
		}
	}
	
	for(unsigned i = 0 ; i < m_features.size() ; i++)
	{
		pair<int , allocDecision> dummy;
		dummy.first = hashing_function1(m_features_hash[i] , element.block_addr , entry->missPC);
		dummy.second = m_features[i]->getAllocDecision(dummy.first, false);
		entry->PHC_allocation_pred[i] = dummy;
	}		
	
	if( !simu_parameters.Cerebron_RDmodel)
	{
		entry->cost_value = m_costAccess[element.isWrite()][RD_SHORT];
		if( element.isSRAMerror)
			entry->cost_value += m_costAccess[element.isWrite()][RD_MEDIUM];		

	}

	reportAccess( element, entry , set,  entry->isNVM, string("INSERTION"), string(str_RD_status[rd]));
				
	stats_allocate++;
	stats_access_class[rd][element.isWrite()]++;
	Predictor::insertionPolicy(set , index , inNVM , element);
}

// Assembly:
pushq %rbx
subq $0x270, %rsp            # imm = 0x270
movb %cl, %al
leaq 0x280(%rsp), %rcx
movq %rcx, 0xf0(%rsp)
movq %rdi, 0x268(%rsp)
movq %rsi, 0x260(%rsp)
movq %rdx, 0x258(%rsp)
andb $0x1, %al
movb %al, 0x257(%rsp)
movq 0x268(%rsp), %rdi
movq %rdi, 0xe8(%rsp)
movq 0x260(%rsp), %rsi
movq 0x258(%rsp), %rdx
movb 0x257(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x8c90
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, 0x248(%rsp)
movq 0x10(%rax), %rdx
movq 0x248(%rsp), %rcx
movq %rdx, 0x20(%rcx)
movl $0x2, 0x244(%rsp)
testb $0x1, 0x28(%rax)
je 0x8405
movq 0xe8(%rsp), %rax
movq 0x448(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x448(%rax)
movl $0x1, 0x244(%rsp)
movq 0xe8(%rsp), %rcx
movq 0x2f8(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x2f8(%rcx)
movl %eax, %ecx
movq 0x248(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x248(%rsp), %rax
testb $0x1, 0x149(%rax)
je 0x84fd
movq 0xf0(%rsp), %rdi
movq 0x248(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x30(%rdi), %rax
movq %rax, 0xd0(%rsp)
movq 0x248(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0x260(%rsp), %rax
movl %eax, 0xe4(%rsp)
callq 0xf7c0
movq 0xe8(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movl 0xe4(%rsp), %r8d
movb 0x257(%rsp), %r10b
movq 0x258(%rsp), %r9
movl %r9d, %r11d
movl 0x244(%rsp), %ebx
movq %rsp, %r9
movl %ebx, 0x10(%r9)
movl %r11d, 0x8(%r9)
movzbl %r10b, %r10d
andl $0x1, %r10d
movl %r10d, (%r9)
movl $0x0, 0x18(%r9)
movzbl %al, %r9d
andl $0x1, %r9d
callq 0x8d20
movq 0x248(%rsp), %rax
testb $0x1, 0x14a(%rax)
je 0x8766
movq 0xf0(%rsp), %rax
testb $0x1, 0x28(%rax)
je 0x8764
leaq 0x8afd5(%rip), %rdi     # 0x93500
leaq 0x68be2(%rip), %rsi     # 0x71114
callq 0x5380
movq %rax, %rdi
leaq 0x72bf(%rip), %rsi      # 0xf800
callq 0x5160
movq %rax, %rdi
movq 0xf0(%rsp), %rax
movq 0x30(%rax), %rsi
callq 0x50f0
movq %rax, %rdi
leaq 0x72bc(%rip), %rsi      # 0xf820
callq 0x5160
movq %rax, %rdi
movq 0x8aa45(%rip), %rsi     # 0x92fb8
callq 0x53d0
movq 0xe8(%rsp), %rdi
movq 0xf0(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x260(%rsp), %rax
movl %eax, %edx
callq 0x56510
movq 0xe8(%rsp), %rdi
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, 0x238(%rsp)
movq 0x30(%rax), %rsi
movq 0x260(%rsp), %rax
movl %eax, %edx
callq 0x56400
cltq
movq %rax, 0x230(%rsp)
movq 0x238(%rsp), %rcx
movq 0x248(%rsp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, 0x22c(%rsp)
movq 0xe8(%rsp), %rdi
movl 0x22c(%rsp), %eax
movq %rax, 0xc0(%rsp)
addq $0x270, %rdi            # imm = 0x270
callq 0xf840
movq %rax, %rcx
movq 0xc0(%rsp), %rax
cmpq %rcx, %rax
jae 0x874a
movq 0xe8(%rsp), %rdi
addq $0x288, %rdi            # imm = 0x288
movl 0x22c(%rsp), %eax
movl %eax, %esi
callq 0xf860
movq %rax, %rsi
leaq 0x200(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0xf880
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rdi
movq 0x30(%rax), %rsi
movq 0x248(%rsp), %rax
movq 0x20(%rax), %rdx
callq 0x2ff40
movl %eax, 0xbc(%rsp)
jmp 0x868c
leaq 0x200(%rsp), %rdi
callq 0xf7b0
movl 0xbc(%rsp), %eax
movq 0xe8(%rsp), %rdi
movl %eax, 0x228(%rsp)
addq $0x270, %rdi            # imm = 0x270
movl 0x22c(%rsp), %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movl 0x228(%rsp), %esi
callq 0x2d5a0
cmpq $0x0, 0x230(%rsp)
jbe 0x8732
movq 0xe8(%rsp), %rdi
addq $0x270, %rdi            # imm = 0x270
movl 0x22c(%rsp), %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movl 0x228(%rsp), %esi
callq 0x2d5a0
jmp 0x8732
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0x200(%rsp), %rdi
callq 0xf7b0
jmp 0x8c7c
jmp 0x8734
movl 0x22c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x22c(%rsp)
jmp 0x85f2
movq 0xe8(%rsp), %rax
movq 0x440(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x440(%rax)
jmp 0x8766
movl $0x0, 0x1f0(%rsp)
movq 0xe8(%rsp), %rdi
movl 0x1f0(%rsp), %eax
movq %rax, 0xa8(%rsp)
addq $0x270, %rdi            # imm = 0x270
callq 0xf840
movq %rax, %rcx
movq 0xa8(%rsp), %rax
cmpq %rcx, %rax
jae 0x88d1
leaq 0x1e8(%rsp), %rdi
callq 0xf8e0
movq 0xe8(%rsp), %rdi
addq $0x288, %rdi            # imm = 0x288
movl 0x1f0(%rsp), %eax
movl %eax, %esi
callq 0xf860
movq %rax, %rsi
leaq 0x1c0(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0xf880
movq 0xf0(%rsp), %rax
movq 0x98(%rsp), %rdi
movq 0x30(%rax), %rsi
movq 0x248(%rsp), %rax
movq 0x20(%rax), %rdx
callq 0x2ff40
movl %eax, 0xa4(%rsp)
jmp 0x8818
movl 0xa4(%rsp), %eax
movl %eax, 0x1e8(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0xf7b0
movq 0xe8(%rsp), %rdi
addq $0x270, %rdi            # imm = 0x270
movl 0x1f0(%rsp), %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movl 0x1e8(%rsp), %esi
xorl %edx, %edx
callq 0x2d360
movl %eax, 0x1ec(%rsp)
movq 0x248(%rsp), %rdi
addq $0xd0, %rdi
movl 0x1f0(%rsp), %eax
movl %eax, %esi
callq 0xf900
movq %rax, %rdi
leaq 0x1e8(%rsp), %rsi
callq 0xf920
movl 0x1f0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f0(%rsp)
jmp 0x8771
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0xf7b0
jmp 0x8c7c
leaq 0x8b718(%rip), %rax     # 0x93ff0
testb $0x1, 0x226(%rax)
jne 0x89af
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rax
addq $0x2d8, %rax            # imm = 0x2D8
movq %rax, 0x90(%rsp)
callq 0xf7c0
movq 0x90(%rsp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, %esi
callq 0xef70
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xef90
movq %rax, %rcx
movq 0xf0(%rsp), %rax
cvtsi2sdl (%rcx), %xmm0
movq 0x248(%rsp), %rcx
movsd %xmm0, 0x130(%rcx)
testb $0x1, 0x28(%rax)
je 0x89ad
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rax
addq $0x2d8, %rax            # imm = 0x2D8
movq %rax, 0x88(%rsp)
callq 0xf7c0
movq 0x88(%rsp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, %esi
callq 0xef70
movq %rax, %rdi
movl $0x1, %esi
callq 0xef90
cvtsi2sdl (%rax), %xmm0
movq 0x248(%rsp), %rax
addsd 0x130(%rax), %xmm0
movsd %xmm0, 0x130(%rax)
jmp 0x89af
movq 0xf0(%rsp), %rax
movq 0x30(%rax), %rcx
movq %rcx, 0x1b0(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movaps %xmm2, 0x1a0(%rsp)
movaps %xmm1, 0x190(%rsp)
movaps %xmm0, 0x180(%rsp)
movq 0x248(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0x260(%rsp), %ecx
movl %ecx, 0x78(%rsp)
movb 0x40(%rax), %al
movb %al, 0x7f(%rsp)
leaq 0x15f(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x56a0
movq 0x80(%rsp), %rdx
leaq 0x68712(%rip), %rsi     # 0x7113b
leaq 0x160(%rsp), %rdi
callq 0x5530
jmp 0x8a38
movl 0x244(%rsp), %eax
movl %eax, %ecx
leaq 0x8aa88(%rip), %rax     # 0x934d0
movq (%rax,%rcx,8), %rax
movq %rax, 0x60(%rsp)
leaq 0x137(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x56a0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x138(%rsp), %rdi
callq 0x5530
jmp 0x8a7c
movl 0x78(%rsp), %edx
movq 0x70(%rsp), %rsi
movq 0xe8(%rsp), %rdi
movb 0x7f(%rsp), %al
movq 0x1b0(%rsp), %r8
movq %rsp, %rcx
movq %r8, 0x30(%rcx)
movaps 0x180(%rsp), %xmm0
movaps 0x190(%rsp), %xmm1
movaps 0x1a0(%rsp), %xmm2
movups %xmm2, 0x20(%rcx)
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movzbl %al, %ecx
andl $0x1, %ecx
leaq 0x160(%rsp), %r8
leaq 0x138(%rsp), %r9
callq 0x91d0
jmp 0x8ae0
leaq 0x138(%rsp), %rdi
callq 0x5768
leaq 0x137(%rsp), %rdi
callq 0x53e0
leaq 0x160(%rsp), %rdi
callq 0x5768
leaq 0x15f(%rsp), %rdi
callq 0x53e0
movq 0xe8(%rsp), %rdi
movq 0x420(%rdi), %rax
addq $0x1, %rax
movq %rax, 0x420(%rdi)
addq $0x4a8, %rdi            # imm = 0x4A8
movl 0x244(%rsp), %eax
movl %eax, %esi
callq 0xef70
movq 0xf0(%rsp), %rdi
movq %rax, 0x40(%rsp)
callq 0xf7c0
movq 0x40(%rsp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, %esi
callq 0xef90
movq 0xf0(%rsp), %rsi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x260(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x258(%rsp), %rax
movq %rax, 0x58(%rsp)
movb 0x257(%rsp), %al
movb %al, 0x4f(%rsp)
leaq 0xf8(%rsp), %rdi
movl $0x38, %edx
callq 0x52f0
movb 0x4f(%rsp), %al
movq 0xe8(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x128(%rsp), %r8
movq %rsp, %rcx
movq %r8, 0x30(%rcx)
movups 0xf8(%rsp), %xmm0
movups 0x108(%rsp), %xmm1
movups 0x118(%rsp), %xmm2
movups %xmm2, 0x20(%rcx)
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x55140
addq $0x270, %rsp            # imm = 0x270
popq %rbx
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x8c6f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x8c55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x5768
leaq 0x137(%rsp), %rdi
callq 0x53e0
leaq 0x160(%rsp), %rdi
callq 0x5768
leaq 0x15f(%rsp), %rdi
callq 0x53e0
movq 0x1f8(%rsp), %rdi
callq 0x5690
nopl (%rax)