// File: /workspace/github_cpp_makefile_star2_O1/sopanpatra160[P]OOO_Superscalar_Processor_Simulator/cache.cc
// Function: cache::insert_vc(unsigned int, unsigned int)
unsigned int cache::insert_vc(unsigned int addr_ev, unsigned int dirty2victim){
	unsigned int  reconstr_tag = calc_tag(addr_ev);
	//unsigned int  addr_ev;	
	
	for (unsigned int j=0; j<a; j++){
		if(valid_store[0][j] == 0){
			tag_store[0][j]=reconstr_tag;
			dirty_store[0][j]=dirty2victim;
			valid_store[0][j]=1;
			if(rp==2)
				lru_update(0,j);
			else 
				lfu_update_block(0,j,0);
			return 0;
		}
	}
	
	//#3. IF ALL VALID
	if(rp==2){
		ev_blk= lru_block2evict(0);
	}
	else if(rp==3){
		ev_blk= lru_block2evict(0);
	}

	if(dirty_store[0][ev_blk]==0){
		tag_store[0][ev_blk]=reconstr_tag;
		dirty_store[0][ev_blk]=dirty2victim;
	}
	else if(dirty_store[0][ev_blk]==1){
		num_wb_v+=1;
		addr_ev = ev_blk_addr(0,tag_store[0][ev_blk]);
		if(next!=NULL){
			next->write(addr_ev);
		}
		tag_store[0][ev_blk]=reconstr_tag;
		dirty_store[0][ev_blk]=dirty2victim;
	}
	if(rp==2)
		lru_update(0,ev_blk);
	else if(rp==3){
		lfu_update_set(0,ev_blk);
		lfu_update_block(0,ev_blk,0);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %esi, %r14d
movq %rdi, %rbx
movl 0x5c(%rdi), %ecx
addl 0x58(%rdi), %ecx
shrl %cl, %r14d
movl %edx, %ebp
movl 0x18(%rdi), %ecx
movl %r14d, 0x64(%rdi)
testq %rcx, %rcx
je 0x20c3
movq 0x30(%rbx), %rax
movq (%rax), %rdx
xorl %eax, %eax
cmpl $0x0, (%rdx,%rax,4)
je 0x210e
incq %rax
cmpl %eax, %ecx
jne 0x20b6
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2166
cmpl $0x2, %eax
jne 0x219a
movl 0x18(%rbx), %ecx
testq %rcx, %rcx
je 0x2195
movq 0x40(%rbx), %rax
movq (%rax), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl %edi, %eax
movl (%rdx,%rax,4), %r8d
movl %esi, %eax
cmpl (%rdx,%rsi,4), %r8d
jb 0x20ff
movl %edi, %eax
incq %rsi
movl %eax, %edi
cmpq %rsi, %rcx
jne 0x20ef
jmp 0x2197
movq 0x28(%rbx), %rcx
movq (%rcx), %rcx
movl %r14d, (%rcx,%rax,4)
movq 0x38(%rbx), %rcx
movq (%rcx), %rcx
movl %ebp, (%rcx,%rax,4)
movl $0x1, (%rdx,%rax,4)
cmpl $0x2, 0x1c(%rbx)
jne 0x2259
movq 0x40(%rbx), %rcx
movq (%rcx), %rcx
cmpl $0x0, 0x18(%rbx)
je 0x2233
movl (%rcx,%rax,4), %edx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpl %edx, %edi
jae 0x2156
incl %edi
movl %edi, (%rcx,%rsi,4)
incq %rsi
movl 0x18(%rbx), %edi
cmpq %rdi, %rsi
jb 0x214a
jmp 0x2233
movl 0x18(%rbx), %ecx
testq %rcx, %rcx
je 0x2195
movq 0x40(%rbx), %rax
movq (%rax), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl %edi, %eax
movl (%rdx,%rax,4), %r8d
movl %esi, %eax
cmpl (%rdx,%rsi,4), %r8d
jb 0x2189
movl %edi, %eax
incq %rsi
movl %eax, %edi
cmpq %rsi, %rcx
jne 0x2179
jmp 0x2197
xorl %eax, %eax
movl %eax, 0x6c(%rbx)
movq 0x38(%rbx), %rax
movq (%rax), %rax
movl 0x6c(%rbx), %ecx
movl (%rax,%rcx,4), %edx
cmpl $0x1, %edx
je 0x21bd
testl %edx, %edx
jne 0x21fa
movq 0x28(%rbx), %rdx
movq (%rdx), %rdx
movl %r14d, (%rdx,%rcx,4)
jmp 0x21f4
incl 0x78(%rbx)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x21df
movq 0x28(%rbx), %rax
movq (%rax), %rax
movl (%rax,%rcx,4), %esi
movl 0x58(%rbx), %ecx
addl 0x5c(%rbx), %ecx
shll %cl, %esi
callq 0x2272
movq 0x28(%rbx), %rax
movq (%rax), %rax
movl 0x6c(%rbx), %ecx
movl %r14d, (%rax,%rcx,4)
movq 0x38(%rbx), %rax
movq (%rax), %rax
movl 0x6c(%rbx), %ecx
movl %ebp, (%rax,%rcx,4)
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x223c
cmpl $0x2, %eax
jne 0x226b
movl 0x6c(%rbx), %eax
movq 0x40(%rbx), %rcx
movq (%rcx), %rcx
cmpl $0x0, 0x18(%rbx)
je 0x2233
movl (%rcx,%rax,4), %edx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpl %edx, %edi
jae 0x2228
incl %edi
movl %edi, (%rcx,%rsi,4)
incq %rsi
movl 0x18(%rbx), %edi
cmpq %rdi, %rsi
jb 0x221c
movl $0x0, (%rcx,%rax,4)
jmp 0x226b
movl 0x6c(%rbx), %eax
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rdx
movq (%rcx), %rcx
movl (%rcx,%rax,4), %eax
movl %eax, (%rdx)
movl 0x6c(%rbx), %edx
incl %eax
movl %eax, (%rcx,%rdx,4)
jmp 0x226b
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rdx
movl (%rdx), %edx
incl %edx
movq (%rcx), %rcx
movl %edx, (%rcx,%rax,4)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq