// File: /workspace/github_cpp_makefile_star2_O1/gulshan216[P]Perceptron_Based_Reuse_Prediction/replacement_state.cpp
// Function: CACHE_REPLACEMENT_STATE::Get_My_Victim(unsigned int, unsigned int, unsigned long long, unsigned long long)
INT32 CACHE_REPLACEMENT_STATE::Get_My_Victim(UINT32 tid, UINT32 setIndex, Addr_t PC, Addr_t paddr) {
	// return first way always
	
	unsigned int hash[6];
	//Addr_t feature[6];
	UINT32 way = 0;
	Addr_t tag = paddr>>(12+6);

	// feature[0]= (PC>>2)%256;
	// feature[1]= (PC_History[0]>>1)%256;
	// feature[2]= (PC_History[1]>>2)%256;
	// feature[3]= (PC_History[2]>>3)%256;
	// feature[4]= (tag>>4)%256;
	// feature[5]= (tag>>7)%256;

	// hash[0] = (unsigned char)((PC^feature[0])&0xFF);
	// hash[1] = (unsigned char)((PC^feature[1])&0xFF);
	// hash[2] = (unsigned char)((PC^feature[2])&0xFF);
	// hash[3] = (unsigned char)((PC^feature[3])&0xFF);
	// hash[4] = (unsigned char)((PC^feature[4])&0xFF);
	// hash[5] = (unsigned char)((PC^feature[5])&0xFF);

	hash[0] = ((PC^(PC>>2))%PRED_TABLE_SIZE);
	hash[1] = ((PC^(PC_History[tid][0]>>1))%PRED_TABLE_SIZE);
	hash[2] = ((PC^(PC_History[tid][1]>>2))%PRED_TABLE_SIZE);
	hash[3] = ((PC^(PC_History[tid][2]>>3))%PRED_TABLE_SIZE);
	hash[4] = ((PC^(tag>>4))%PRED_TABLE_SIZE);
	hash[5] = ((PC^(tag))%PRED_TABLE_SIZE);

	bool bypass = ((int)pred_table1[hash[0]]+(int)pred_table2[hash[1]]+(int)pred_table3[hash[2]]+(int)pred_table4[hash[3]]+(int)pred_table5[hash[4]]+(int)pred_table6[hash[5]])>=Tbypass;
	if(bypass==true)
	{
		return -1;
	}
	else
	{
		for(way=0; way<assoc; way++)
		{
			if(repl[setIndex][way].pred_dead == true)
				break;
		}
		if(way!=assoc)
		{
			return way;
		}
		else
		{
			// if((setIndex%64) == 0)
			// {
			// 	for(way=0; way<assoc; way++)
			// 	{
			// 		if(sampler.sets[setIndex/64].blocks[way].Yout>Treplace) break;
			// 	}
			// 	if(way!=assoc)
			// 	{
			// 		return way;
			// 	}
			// }

			return Get_LRU_Victim(setIndex);
		}
	}
	return 0;
}

// Assembly:
pushq %r14
pushq %rbx
movl %r8d, %eax
shrl $0x12, %eax
movl %ecx, %r9d
shrl $0x2, %r9d
xorl %ecx, %r9d
movl $0x1ff, %r10d           # imm = 0x1FF
andl %r10d, %r9d
movl %esi, %esi
leaq (%rsi,%rsi,2), %rbx
leaq 0x1faa69(%rip), %r14    # 0x601170
movl (%r14,%rbx,8), %r11d
movl 0x8(%r14,%rbx,8), %esi
shrl %r11d
xorl %ecx, %r11d
andl %r10d, %r11d
shrl $0x2, %esi
xorl %ecx, %esi
andl %r10d, %esi
movl 0x10(%r14,%rbx,8), %ebx
shrl $0x3, %ebx
xorl %ecx, %ebx
andl %r10d, %ebx
shrl $0x16, %r8d
xorl %ecx, %r8d
andl %r10d, %r8d
xorl %ecx, %eax
andl %r10d, %eax
leaq 0x1f9e2c(%rip), %rcx    # 0x600570
movsbl (%r9,%rcx), %ecx
leaq 0x1fa020(%rip), %r9     # 0x600770
movsbl (%r11,%r9), %r9d
addl %ecx, %r9d
leaq 0x1fa211(%rip), %rcx    # 0x600970
movsbl (%rsi,%rcx), %ecx
leaq 0x1fa406(%rip), %rsi    # 0x600b70
movsbl (%rbx,%rsi), %esi
addl %ecx, %esi
addl %r9d, %esi
leaq 0x1fa5f6(%rip), %rcx    # 0x600d70
movsbl (%r8,%rcx), %ecx
leaq 0x1fa7ea(%rip), %r8     # 0x600f70
movsbl (%rax,%r8), %r8d
addl %ecx, %r8d
addl %esi, %r8d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, %r8d
jg 0x4067e9
movl 0xc(%rdi), %ecx
movl %edx, %edx
testq %rcx, %rcx
je 0x4067c2
movq (%rdi), %rax
movq (%rax,%rdx,8), %rsi
xorl %eax, %eax
cmpb $0x0, 0x4(%rsi,%rax,8)
jne 0x4067c4
incq %rax
cmpq %rax, %rcx
jne 0x4067af
movl %ecx, %eax
jmp 0x4067c4
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x4067e9
testq %rcx, %rcx
je 0x4067e7
movq (%rdi), %rax
movq (%rax,%rdx,8), %rdx
movl %ecx, %esi
decl %esi
xorl %eax, %eax
cmpl %esi, (%rdx,%rax,8)
je 0x4067e9
incq %rax
cmpq %rax, %rcx
jne 0x4067da
xorl %eax, %eax
popq %rbx
popq %r14
retq
nop