// File: /workspace/github_cpp_makefile_star2_O1/gulshan216[P]Perceptron_Based_Reuse_Prediction/replacement_state.cpp
// Function: sampler_cache_access(unsigned int, unsigned long long, unsigned long long, unsigned int*)
void sampler_cache_access (UINT32 setIndex, Addr_t tag, unsigned long long int pc, unsigned int *hash)
{
	sampler.accesses++;
	int assoc = sampler.assoc;
	int set_valid = sampler.sets[setIndex].valid;
	sampler_block *v;
	v = &sampler.sets[setIndex].blocks[0];
	int i;
	signed int tmp_out;
	for(i=0;i<assoc;i++)
	{
		if(v[i].tag == tag)
		{
			sampler.hits++;
			if(v[i].lru_stack_position < (assoc-1))
			{
				// bool mispredict = v[i].Yout >= Treplace; 
				if((v[i].Yout > (-THETA)))
				{
					pred_table1[v[i].hash[0]] = (pred_table1[v[i].hash[0]]>(-32)) ? pred_table1[v[i].hash[0]]-1 : -32;
					pred_table2[v[i].hash[1]] = (pred_table2[v[i].hash[1]]>(-32)) ? pred_table2[v[i].hash[1]]-1 : -32;
					pred_table3[v[i].hash[2]] = (pred_table3[v[i].hash[2]]>(-32)) ? pred_table3[v[i].hash[2]]-1 : -32;
					pred_table4[v[i].hash[3]] = (pred_table4[v[i].hash[3]]>(-32)) ? pred_table4[v[i].hash[3]]-1 : -32;
					pred_table5[v[i].hash[4]] = (pred_table5[v[i].hash[4]]>(-32)) ? pred_table5[v[i].hash[4]]-1 : -32;
					pred_table6[v[i].hash[5]] = (pred_table6[v[i].hash[5]]>(-32)) ? pred_table6[v[i].hash[5]]-1 : -32;
				}
			}
			

			//Implement LRU update logic
			Update_sampler_LRU(setIndex,i);
			v[i].tag = tag;
			v[i].valid = 1;
			v[i].hash[0]=hash[0];
			v[i].hash[1]=hash[1];
			v[i].hash[2]=hash[2];
			v[i].hash[3]=hash[3];
			v[i].hash[4]=hash[4];
			v[i].hash[5]=hash[5];
			tmp_out = (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]];
			if(tmp_out > 255)
			{
				v[i].Yout = 255;
			}
			else if(tmp_out < (-256))
			{
				v[i].Yout = -256;
			}
			else
			{
				v[i].Yout = tmp_out;
			}
			return;
		}
	}
	sampler.misses++;

	if (!set_valid) {
		for (i=0; i<assoc; i++) {
			if (v[i].valid == 0) break;
		}
		if (i == assoc) {
			sampler.sets[setIndex].valid = 1; // mark this set as having only valid blocks so we don't search it again
			set_valid = 1;
		}
	}

	if(set_valid)
	{
		sampler.evictions++;
		// for(i=0;i<assoc;i++)
		// {
		// 	if(v[i].Yout>Treplace) break;
		// }
		// if(i==assoc)
		// {
			i = Get_sampler_LRU_victim(setIndex);
		// }
		
		// Find the LRU victim
		if(v[i].lru_stack_position == (assoc-1))
		{
			//Before evicting the block train the predictor
			bool mispredict = v[i].Yout < Treplace; 
			if((v[i].Yout < THETA) || (mispredict))
			{
				pred_table1[v[i].hash[0]] = (pred_table1[v[i].hash[0]]<32) ? pred_table1[v[i].hash[0]]+1 : 32;
				pred_table2[v[i].hash[1]] = (pred_table2[v[i].hash[1]]<32) ? pred_table2[v[i].hash[1]]+1 : 32;
				pred_table3[v[i].hash[2]] = (pred_table3[v[i].hash[2]]<32) ? pred_table3[v[i].hash[2]]+1 : 32;
				pred_table4[v[i].hash[3]] = (pred_table4[v[i].hash[3]]<32) ? pred_table4[v[i].hash[3]]+1 : 32;
				pred_table5[v[i].hash[4]] = (pred_table5[v[i].hash[4]]<32) ? pred_table5[v[i].hash[4]]+1 : 32;
				pred_table6[v[i].hash[5]] = (pred_table6[v[i].hash[5]]<32) ? pred_table6[v[i].hash[5]]+1 : 32;
			}
		}
	}

	//Store the features,tag,prediction_outcome(Yout)
	Update_sampler_LRU(setIndex,i);
	v[i].tag = tag;
	v[i].valid = 1;
	v[i].hash[0]=hash[0];
	v[i].hash[1]=hash[1];
	v[i].hash[2]=hash[2];
	v[i].hash[3]=hash[3];
	v[i].hash[4]=hash[4];
	v[i].hash[5]=hash[5];

	tmp_out = (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]];
	if(tmp_out > 255)
	{
		v[i].Yout = 255;
	}
	else if(tmp_out < (-256))
	{
		v[i].Yout = -256;
	}
	else
	{
		v[i].Yout = tmp_out;
	}
}

// Assembly:
pushq %rbp
pushq %rbx
incq 0x1fa447(%rip)          # 0x600550
movl 0x1fa41c(%rip), %r9d    # 0x60052c
movq 0x1fa451(%rip), %rdx    # 0x600568
movl %edi, %eax
imulq $0x308, %rax, %r11      # imm = 0x308
leaq (%rdx,%r11), %rax
movzbl 0x300(%rdx,%r11), %r10d
testl %r9d, %r9d
jle 0x406161
leaq (%r11,%rdx), %rdi
addq $0x2c, %rdi
movq %r9, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %rbx
xorl %r8d, %r8d
cmpq %rsi, -0x24(%rdi,%r8)
je 0x4063bf
addq $0x30, %r8
cmpq %r8, %rbx
jne 0x406148
movl %r9d, %edi
jmp 0x406163
xorl %edi, %edi
incq 0x1fa3de(%rip)          # 0x600548
testl %r10d, %r10d
jne 0x4061af
xorl %r10d, %r10d
movl $0x0, %edi
testl %r9d, %r9d
jle 0x40619b
leaq (%r11,%rdx), %r8
addq $0x14, %r8
xorl %edi, %edi
cmpb $0x0, (%r8)
je 0x40619b
incq %rdi
addq $0x30, %r8
cmpq %rdi, %r9
jne 0x406186
movl %r9d, %edi
cmpl %r9d, %edi
jne 0x4061af
movb $0x1, 0x300(%rdx,%r11)
movl $0x1, %r10d
testl %r10d, %r10d
je 0x4062d4
incq 0x1fa3a1(%rip)          # 0x600560
xorl %edi, %edi
movq %rax, %rdx
cmpl $0xf, (%rdx)
je 0x4061d8
incq %rdi
addq $0x30, %rdx
cmpq $0x10, %rdi
jne 0x4061c4
xorl %edi, %edi
movslq %edi, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
decl %r9d
cmpl %r9d, (%rax,%rdx)
jne 0x4062d4
addq %rax, %rdx
cmpl $0x7b, 0x10(%rdx)
jg 0x4062d4
movl 0x18(%rdx), %r9d
leaq 0x1fa368(%rip), %r10    # 0x600570
movb (%r9,%r10), %r8b
leal 0x1(%r8), %r11d
cmpb $0x20, %r8b
movzbl %r11b, %r11d
movl $0x20, %r8d
cmovgel %r8d, %r11d
movb %r11b, (%r9,%r10)
movl 0x1c(%rdx), %r9d
leaq 0x1fa53f(%rip), %r10    # 0x600770
movb (%r9,%r10), %r11b
leal 0x1(%r11), %ebx
cmpb $0x20, %r11b
movzbl %bl, %r11d
cmovgel %r8d, %r11d
movb %r11b, (%r9,%r10)
movl 0x20(%rdx), %r9d
leaq 0x1fa71c(%rip), %r10    # 0x600970
movb (%r9,%r10), %r11b
leal 0x1(%r11), %ebx
cmpb $0x20, %r11b
movzbl %bl, %r11d
cmovgel %r8d, %r11d
movb %r11b, (%r9,%r10)
movl 0x24(%rdx), %r9d
leaq 0x1fa8f9(%rip), %r10    # 0x600b70
movb (%r9,%r10), %r11b
leal 0x1(%r11), %ebx
cmpb $0x20, %r11b
movzbl %bl, %r11d
cmovgel %r8d, %r11d
movb %r11b, (%r9,%r10)
movl 0x28(%rdx), %r9d
leaq 0x1faad6(%rip), %r10    # 0x600d70
movb (%r9,%r10), %r11b
leal 0x1(%r11), %ebx
cmpb $0x20, %r11b
movzbl %bl, %r11d
cmovgel %r8d, %r11d
movb %r11b, (%r9,%r10)
movl 0x2c(%rdx), %edx
leaq 0x1facb4(%rip), %r9     # 0x600f70
movb (%rdx,%r9), %r10b
leal 0x1(%r10), %r11d
cmpb $0x20, %r10b
movzbl %r11b, %r10d
cmovgel %r8d, %r10d
movb %r10b, (%rdx,%r9)
movslq %edi, %rdx
leaq (%rdx,%rdx,2), %rdi
shlq $0x4, %rdi
leaq (%rax,%rdi), %rdx
movl (%rax,%rdi), %edi
xorl %r8d, %r8d
movl (%rax,%r8), %r9d
cmpl %edi, %r9d
jae 0x4062f9
incl %r9d
movl %r9d, (%rax,%r8)
addq $0x30, %r8
cmpq $0x300, %r8             # imm = 0x300
jne 0x4062e9
movl $0x0, (%rdx)
movq %rsi, 0x8(%rdx)
movb $0x1, 0x14(%rdx)
movl (%rcx), %eax
movl %eax, 0x18(%rdx)
movl 0x4(%rcx), %eax
movl %eax, 0x1c(%rdx)
movl 0x8(%rcx), %eax
movl %eax, 0x20(%rdx)
movl 0xc(%rcx), %eax
movl %eax, 0x24(%rdx)
movl 0x10(%rcx), %eax
movl %eax, 0x28(%rdx)
movl 0x14(%rcx), %eax
movl %eax, 0x2c(%rdx)
movl (%rcx), %eax
movl 0x4(%rcx), %esi
leaq 0x1fa22d(%rip), %rdi    # 0x600570
movsbl (%rax,%rdi), %eax
leaq 0x1fa422(%rip), %rdi    # 0x600770
movsbl (%rsi,%rdi), %esi
addl %eax, %esi
movl 0x8(%rcx), %eax
leaq 0x1fa612(%rip), %rdi    # 0x600970
movsbl (%rax,%rdi), %eax
movl 0xc(%rcx), %edi
leaq 0x1fa804(%rip), %r8     # 0x600b70
movsbl (%rdi,%r8), %edi
addl %eax, %edi
addl %esi, %edi
movl 0x10(%rcx), %eax
leaq 0x1fa9f1(%rip), %rsi    # 0x600d70
movsbl (%rax,%rsi), %esi
movl 0x14(%rcx), %eax
leaq 0x1fabe3(%rip), %rcx    # 0x600f70
movsbl (%rax,%rcx), %eax
addl %esi, %eax
addl %edi, %eax
cmpl $0x100, %eax            # imm = 0x100
jl 0x4063a8
movl $0xff, 0x10(%rdx)
jmp 0x4065af
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
jg 0x406593
movl $0xffffff00, 0x10(%rdx) # imm = 0xFFFFFF00
jmp 0x4065af
incq 0x1fa192(%rip)          # 0x600558
addq %r11, %rdx
decl %r9d
cmpl %r9d, (%r8,%rdx)
jae 0x4064b5
cmpl $-0x43, 0x10(%r8,%rdx)
jl 0x4064b5
movl 0x18(%r8,%rdx), %r10d
leaq 0x1fa182(%rip), %r11    # 0x600570
movb (%r10,%r11), %r9b
leal -0x1(%r9), %ebx
cmpb $-0x1f, %r9b
movzbl %bl, %ebx
movl $0xe0, %r9d
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl 0x1c(%r8,%rdx), %r10d
leaq 0x1fa359(%rip), %r11    # 0x600770
movb (%r10,%r11), %bl
leal -0x1(%rbx), %ebp
cmpb $-0x1f, %bl
movzbl %bpl, %ebx
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl 0x20(%r8,%rdx), %r10d
leaq 0x1fa537(%rip), %r11    # 0x600970
movb (%r10,%r11), %bl
leal -0x1(%rbx), %ebp
cmpb $-0x1f, %bl
movzbl %bpl, %ebx
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl 0x24(%r8,%rdx), %r10d
leaq 0x1fa715(%rip), %r11    # 0x600b70
movb (%r10,%r11), %bl
leal -0x1(%rbx), %ebp
cmpb $-0x1f, %bl
movzbl %bpl, %ebx
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl 0x28(%r8,%rdx), %r10d
leaq 0x1fa8f3(%rip), %r11    # 0x600d70
movb (%r10,%r11), %bl
leal -0x1(%rbx), %ebp
cmpb $-0x1f, %bl
movzbl %bpl, %ebx
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl 0x2c(%r8,%rdx), %r10d
leaq 0x1faad1(%rip), %r11    # 0x600f70
movb (%r10,%r11), %bl
leal -0x1(%rbx), %ebp
cmpb $-0x1f, %bl
movzbl %bpl, %ebx
cmovll %r9d, %ebx
movb %bl, (%r10,%r11)
movl (%r8,%rdx), %r9d
xorl %r10d, %r10d
movl (%rax,%r10), %r11d
cmpl %r9d, %r11d
jae 0x4064cc
incl %r11d
movl %r11d, (%rax,%r10)
addq $0x30, %r10
cmpq $0x300, %r10            # imm = 0x300
jne 0x4064bc
movl $0x0, -0x2c(%rdi,%r8)
movq %rsi, -0x24(%rdi,%r8)
movb $0x1, 0x14(%r8,%rdx)
movl (%rcx), %eax
movl %eax, 0x18(%r8,%rdx)
movl 0x4(%rcx), %eax
movl %eax, 0x1c(%r8,%rdx)
movl 0x8(%rcx), %eax
movl %eax, 0x20(%r8,%rdx)
movl 0xc(%rcx), %eax
movl %eax, 0x24(%r8,%rdx)
movl 0x10(%rcx), %eax
movl %eax, 0x28(%r8,%rdx)
movl 0x14(%rcx), %eax
movl %eax, 0x2c(%r8,%rdx)
movl (%rcx), %eax
movl 0x4(%rcx), %esi
leaq 0x1fa048(%rip), %r9     # 0x600570
movsbl (%rax,%r9), %eax
leaq 0x1fa23c(%rip), %r9     # 0x600770
movsbl (%rsi,%r9), %esi
addl %eax, %esi
movl 0x8(%rcx), %eax
leaq 0x1fa42b(%rip), %r9     # 0x600970
movsbl (%rax,%r9), %eax
movl 0xc(%rcx), %r9d
leaq 0x1fa61b(%rip), %r10    # 0x600b70
movsbl (%r9,%r10), %r9d
addl %eax, %r9d
addl %esi, %r9d
movl 0x10(%rcx), %eax
leaq 0x1fa806(%rip), %rsi    # 0x600d70
movsbl (%rax,%rsi), %esi
movl 0x14(%rcx), %eax
leaq 0x1fa9f8(%rip), %rcx    # 0x600f70
movsbl (%rax,%rcx), %eax
addl %esi, %eax
addl %r9d, %eax
cmpl $0x100, %eax            # imm = 0x100
jl 0x406598
movl $0xff, 0x10(%r8,%rdx)
jmp 0x4065af
movl %eax, 0x10(%rdx)
jmp 0x4065af
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
jg 0x4065aa
movl $0xffffff00, -0x1c(%rdi,%r8) # imm = 0xFFFFFF00
jmp 0x4065af
movl %eax, -0x1c(%rdi,%r8)
popq %rbx
popq %rbp
retq