// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rdi, %rbx
movl 0x5c(%rdi), %ecx
addl 0x58(%rdi), %ecx
shrl %cl, %ebp
movl %edx, %r14d
movl 0x18(%rdi), %eax
movl %ebp, 0x64(%rdi)
testq %rax, %rax
je 0x2284
movq 0x30(%rbx), %rdx
movq (%rdx), %rsi
xorl %edx, %edx
cmpl $0x0, (%rsi,%rdx,4)
je 0x22d0
incq %rdx
cmpq %rdx, %rax
jne 0x2276
movl 0x1c(%rbx), %edx
cmpl $0x2, %edx
je 0x2333
cmpl $0x3, %edx
jne 0x2363
testq %rax, %rax
je 0x2368
movq 0x40(%rbx), %rdx
movq (%rdx), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl %r8d, %edx
movl (%rsi,%rdx,4), %r9d
movl %edi, %edx
cmpl (%rsi,%rdi,4), %r9d
jb 0x22c0
movl %r8d, %edx
incq %rdi
movl %edx, %r8d
cmpq %rdi, %rax
jne 0x22ae
jmp 0x236a
movq 0x28(%rbx), %rax
movq (%rax), %rax
movl %ebp, (%rax,%rdx,4)
movq 0x38(%rbx), %rax
movq (%rax), %rax
movl %r14d, (%rax,%rdx,4)
movl $0x1, (%rsi,%rdx,4)
cmpl $0x2, 0x1c(%rbx)
jne 0x2422
movq 0x40(%rbx), %rax
movq (%rax), %rax
movl 0x18(%rbx), %ecx
testl %ecx, %ecx
je 0x2327
movl (%rax,%rdx,4), %esi
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x231c
incl %r8d
movl %r8d, (%rax,%rdi,4)
movl 0x18(%rbx), %ecx
incq %rdi
movl %ecx, %r8d
cmpq %r8, %rdi
jb 0x2309
movl $0x0, (%rax,%rdx,4)
jmp 0x2434
testq %rax, %rax
je 0x2368
movq 0x40(%rbx), %rdx
movq (%rdx), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl %r8d, %edx
movl (%rsi,%rdx,4), %r9d
movl %edi, %edx
cmpl (%rsi,%rdi,4), %r9d
jb 0x2356
movl %r8d, %edx
incq %rdi
movl %edx, %r8d
cmpq %rdi, %rax
jne 0x2344
jmp 0x236a
movl 0x6c(%rbx), %edx
jmp 0x236d
xorl %edx, %edx
movl %edx, 0x6c(%rbx)
movq 0x38(%rbx), %rax
movq (%rax), %rsi
movl %edx, %eax
movl (%rsi,%rax,4), %edx
cmpl $0x1, %edx
je 0x2384
testl %edx, %edx
je 0x23aa
jmp 0x23bb
incl 0x78(%rbx)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x23aa
movq 0x28(%rbx), %rdx
movq (%rdx), %rdx
movl (%rdx,%rax,4), %esi
shll %cl, %esi
callq 0x243c
movl 0x6c(%rbx), %eax
movq 0x38(%rbx), %rcx
movq (%rcx), %rsi
movq 0x28(%rbx), %rcx
movq (%rcx), %rcx
movl %ebp, (%rcx,%rax,4)
movl 0x6c(%rbx), %eax
movl %r14d, (%rsi,%rax,4)
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2405
cmpl $0x2, %eax
jne 0x2434
movl 0x6c(%rbx), %eax
movq 0x40(%rbx), %rcx
movq (%rcx), %rcx
movl 0x18(%rbx), %edx
testl %edx, %edx
je 0x23fc
movl (%rcx,%rax,4), %esi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x23f1
incl %r8d
movl %r8d, (%rcx,%rdi,4)
movl 0x18(%rbx), %edx
incq %rdi
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x23de
movl $0x0, (%rcx,%rax,4)
jmp 0x2434
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 0x2434
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rcx
movl (%rcx), %ecx
incl %ecx
movq (%rax), %rax
movl %ecx, (%rax,%rdx,4)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
nop