// File: /workspace/github_cpp_makefile_star2_O3/sopanpatra160[P]OOO_Superscalar_Processor_Simulator/cache.cc
// Function: cache::write(unsigned int)
unsigned int cache::write(unsigned int addr){

	unsigned int addr_ev;
	num_w++;
	calc_tag_ind(addr);
		
	if(lev == 1){
		//#1 check for the element in all the blocks of that set	
		for (unsigned int j=0; j<a; j++){
			if((tag_store[ind][j]==tag) && (valid_store[ind][j]==1)){
				if(rp==2)
					lru_update(ind,j);
				else if(rp==3) 
					lfu_update_block(ind,j,1);
				if(wp==0){
					dirty_store[ind][j]=1;
				}
				return 1;
			}
		}
		if(vptr != NULL){
			unsigned int tag_vc = vptr->calc_tag(addr);
			unsigned int found_loc;
			unsigned int found_vc = 0;
			for (unsigned int j=0; j<vptr->a; j++){
				
				if((vptr->tag_store[0][j]==tag_vc) && (vptr->valid_store[0][j]==1)){
					found_vc = 1;
					found_loc = j;
				}
			}
			if(found_vc==1){
				num_swap++;
				swap(found_loc,1);
				return 1;
			}
		}

		num_miss_w++;
		
		//#2 MISS in L1 but invalid case
		for (unsigned int j=0; j<a; j++){
			if(valid_store[ind][j]==0){
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][j]=tag;
				valid_store[ind][j]=1;
				dirty_store[ind][j]=1;
				if(rp==2)
					lru_update(ind,j);
				else 
					lfu_update_block(ind,j,0);
				return 0;
			}
		}
		
		//#3 MISS IN L1 FOR ALL BITS VALID : EVICTION
		//LRU
		if(rp==2){
			ev_blk= lru_block2evict(ind);
		}
		//LFU policy
		else if(rp==3){
			ev_blk= lfu_block2evict(ind);
		}


		if(vptr != NULL){
			addr_ev = ev_blk_addr(ind,tag_store[ind][ev_blk]);
			vptr->insert_vc(addr_ev,dirty_store[ind][ev_blk]);
			if(next!=NULL)
				next->read(addr);
			tag_store[ind][ev_blk]=tag;
			dirty_store[ind][ev_blk]=1;
			

		}
		else if(vptr == NULL){
			if(dirty_store[ind][ev_blk]==0){
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][ev_blk]=tag;
				dirty_store[ind][ev_blk]=1;
			}
			else if(dirty_store[ind][ev_blk]==1){
				num_wb+=1;
				addr_ev = ev_blk_addr(ind,tag_store[ind][ev_blk]);
				if(next!=NULL)
					next->write(addr_ev);
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][ev_blk]=tag;
				dirty_store[ind][ev_blk]=1;
			}
		}

		if(rp==2){
			lru_update(ind,ev_blk);
		}
		else if(rp==3){
			lfu_update_set(ind,ev_blk);
			lfu_update_block(ind,ev_blk,0);
		}
		return 0;

	}	
		
	if(lev == 2){
		//#1 check for the element in all the blocks of that set
		for (unsigned int j=0; j<a; j++){
			if((tag_store[ind][j]==tag) && (valid_store[ind][j]==1)){

				if(rp==2)
					lru_update(ind,j);
				else if(rp==3) 
					lfu_update_block(ind,j,1);
				if(wp==0){
					dirty_store[ind][j]=1;
				}

				return 1;
			}
		}

		num_miss_w+=1;

		//#2 MISS in L1 but invalid case	
		for (unsigned int j=0; j<a; j++){
			if(valid_store[ind][j]==0){
				if(wp == 0){
					tag_store[ind][j]=tag;
					valid_store[ind][j]=1;
					dirty_store[ind][j]=1;
					if(rp==2)
						lru_update(ind,j);
					else 
						lfu_update_block(ind,j,0);
				}

				return 0;
			}
		}
		
		//#3 MISS IN L1 FOR ALL BITS VALID : EVICTION	
		//LRU policy
		if(rp==2){
			ev_blk= lru_block2evict(ind);
		}
		//LFU policy
		else if(rp==3){
			ev_blk= lfu_block2evict(ind);
		}

		//update dirty according to write policy
		//WB policy
		if(wp == 0){
			if(dirty_store[ind][ev_blk]==0){
				tag_store[ind][ev_blk]=tag;
				dirty_store[ind][ev_blk]=1;
				if(rp==2){
					lru_update(ind,ev_blk);
				}
				else if(rp==3){
					lfu_update_set(ind,ev_blk);
					lfu_update_block(ind,ev_blk,0);
				}
			}
			else if(dirty_store[ind][ev_blk]==1){
				num_wb+=1;
				tag_store[ind][ev_blk]=tag;
				if(rp==2){
					lru_update(ind,ev_blk);
				}
				else if(rp==3){
					lfu_update_set(ind,ev_blk);
					lfu_update_block(ind,ev_blk,0);
				}
			}
		}
		return 0;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
incl 0x88(%rdi)
movl %esi, %r14d
movl 0x58(%rdi), %eax
movl %eax, %ecx
shrl %cl, %esi
movl 0x24(%rdi), %r8d
movl 0x60(%rdi), %ecx
addl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
movl %esi, 0x68(%rdi)
movl 0x5c(%rdi), %edx
addl %eax, %edx
movl %r14d, %edi
movl %edx, %ecx
shrl %cl, %edi
movl %edi, 0x64(%rbx)
xorl %ebp, %ebp
cmpl $0x1, %r8d
je 0x24f4
cmpl $0x2, %r8d
jne 0x2b29
movl 0x18(%rbx), %eax
testq %rax, %rax
je 0x2621
movl %esi, %edx
movq 0x28(%rbx), %rcx
movq 0x30(%rbx), %r8
movq (%rcx,%rdx,8), %r9
xorl %ecx, %ecx
cmpl %edi, (%r9,%rcx,4)
jne 0x24bc
movq (%r8,%rdx,8), %r10
cmpl $0x1, (%r10,%rcx,4)
je 0x26ae
incq %rcx
cmpq %rcx, %rax
jne 0x24a7
incl 0x8c(%rbx)
testl %eax, %eax
je 0x2627
movq 0x30(%rbx), %r8
movq (%r8,%rdx,8), %r9
xorl %ecx, %ecx
cmpl $0x0, (%r9,%rcx,4)
je 0x28b0
incq %rcx
cmpq %rcx, %rax
jne 0x24dc
jmp 0x2627
movl 0x18(%rbx), %r8d
testq %r8, %r8
je 0x252b
movl %esi, %r9d
movq 0x28(%rbx), %rcx
movq 0x30(%rbx), %r10
movq (%rcx,%r9,8), %r11
xorl %ecx, %ecx
cmpl %edi, (%r11,%rcx,4)
jne 0x2523
movq (%r10,%r9,8), %r15
cmpl $0x1, (%r15,%rcx,4)
je 0x26fa
incq %rcx
cmpq %rcx, %r8
jne 0x250e
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x25a3
movl 0x5c(%rdi), %ecx
addl 0x58(%rdi), %ecx
movl %r14d, %r9d
shrl %cl, %r9d
movl 0x18(%rdi), %r10d
movl %r9d, 0x64(%rdi)
testq %r10, %r10
je 0x25a3
movq 0x28(%rdi), %rcx
movq (%rcx), %r11
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpl %r9d, (%r11,%r15,4)
jne 0x2579
movq 0x30(%rdi), %r13
movq (%r13), %r13
movl (%r13,%r15,4), %r13d
cmpl $0x1, %r13d
cmovel %r15d, %ecx
cmovel %r13d, %r12d
incq %r15
cmpq %r15, %r10
jne 0x255a
cmpl $0x1, %r12d
jne 0x25a3
incl 0x7c(%rbx)
movl $0x1, %ebp
movq %rbx, %rdi
movl %ecx, %esi
movl $0x1, %edx
callq 0x1ffc
jmp 0x2b29
incl 0x8c(%rbx)
testl %r8d, %r8d
je 0x25cf
movq 0x30(%rbx), %rcx
movl %esi, %r9d
movq (%rcx,%r9,8), %r10
xorl %r15d, %r15d
cmpl $0x0, (%r10,%r15,4)
je 0x2743
incq %r15
cmpq %r15, %r8
jne 0x25bc
movl 0x1c(%rbx), %ecx
cmpl $0x3, %ecx
je 0x2873
cmpl $0x2, %ecx
jne 0x2938
testl %r8d, %r8d
je 0x2933
movq 0x40(%rbx), %rcx
movl %esi, %r9d
movq (%rcx,%r9,8), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl %r11d, %ecx
movl (%r9,%rcx,4), %r15d
movl %r10d, %ecx
cmpl (%r9,%r10,4), %r15d
jb 0x2611
movl %r11d, %ecx
incq %r10
movl %ecx, %r11d
cmpq %r10, %r8
jne 0x25fe
jmp 0x2935
incl 0x8c(%rbx)
movl 0x1c(%rbx), %ecx
cmpl $0x3, %ecx
je 0x2673
cmpl $0x2, %ecx
jne 0x27d6
testl %eax, %eax
je 0x27d1
movq 0x40(%rbx), %rcx
movl %esi, %edx
movq (%rcx,%rdx,8), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r9d, %ecx
movl (%rdx,%rcx,4), %r10d
movl %r8d, %ecx
cmpl (%rdx,%r8,4), %r10d
jb 0x2663
movl %r9d, %ecx
incq %r8
movl %ecx, %r9d
cmpq %r8, %rax
jne 0x2650
jmp 0x27d3
testl %eax, %eax
je 0x27d1
movq 0x48(%rbx), %rcx
movl %esi, %edx
movq (%rcx,%rdx,8), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r9d, %ecx
movl (%rdx,%rcx,4), %r10d
movl %r8d, %ecx
cmpl (%rdx,%r8,4), %r10d
ja 0x269e
movl %r9d, %ecx
incq %r8
movl %ecx, %r9d
cmpq %r8, %rax
jne 0x268b
jmp 0x27d3
movl 0x1c(%rbx), %esi
cmpl $0x3, %esi
je 0x2af7
cmpl $0x2, %esi
jne 0x2b0c
movq 0x40(%rbx), %rsi
movq (%rsi,%rdx,8), %rdx
movl (%rdx,%rcx,4), %esi
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x26e3
incl %r8d
movl %r8d, (%rdx,%rdi,4)
movl 0x18(%rbx), %eax
incq %rdi
movl %eax, %r8d
cmpq %r8, %rdi
jb 0x26d0
movl $0x0, (%rdx,%rcx,4)
jmp 0x2b0c
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2b01
cmpl $0x2, %eax
jne 0x2b0c
movq 0x40(%rbx), %rax
movq (%rax,%r9,8), %rax
movl (%rax,%rcx,4), %edx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
cmpl %edx, %edi
jae 0x272c
incl %edi
movl %edi, (%rax,%rsi,4)
movl 0x18(%rbx), %r8d
incq %rsi
movl %r8d, %edi
cmpq %rdi, %rsi
jb 0x271c
movl $0x0, (%rax,%rcx,4)
jmp 0x2b0c
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x275b
movl %r14d, %esi
callq 0x1840
movl 0x68(%rbx), %r9d
movq 0x30(%rbx), %rcx
movl 0x64(%rbx), %eax
movq 0x28(%rbx), %rdx
movq (%rdx,%r9,8), %rdx
movl %eax, (%rdx,%r15,4)
movl 0x68(%rbx), %eax
movq (%rcx,%rax,8), %rax
movl $0x1, %ecx
movl %ecx, (%rax,%r15,4)
movq 0x38(%rbx), %rax
movl 0x68(%rbx), %edx
movq (%rax,%rdx,8), %rax
movl %ecx, (%rax,%r15,4)
movl 0x68(%rbx), %eax
cmpl $0x2, 0x1c(%rbx)
jne 0x2a4f
movq 0x40(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x18(%rbx), %ecx
testl %ecx, %ecx
je 0x27c4
movl (%rax,%r15,4), %edx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
cmpl %edx, %edi
jae 0x27ba
incl %edi
movl %edi, (%rax,%rsi,4)
movl 0x18(%rbx), %ecx
incq %rsi
movl %ecx, %edi
cmpq %rdi, %rsi
jb 0x27ab
movl $0x0, (%rax,%r15,4)
jmp 0x2a64
xorl %ecx, %ecx
movl %ecx, 0x6c(%rbx)
cmpl $0x0, 0x20(%rbx)
jne 0x2b29
movq 0x38(%rbx), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rsi
movl 0x6c(%rbx), %edx
movl (%rsi,%rdx,4), %esi
cmpl $0x1, %esi
je 0x2a6b
testl %esi, %esi
jne 0x2b29
movq 0x28(%rbx), %rsi
movq (%rsi,%rcx,8), %rcx
movl %edi, (%rcx,%rdx,4)
movl 0x68(%rbx), %ecx
movl 0x6c(%rbx), %edx
movq (%rax,%rcx,8), %rax
movl $0x1, (%rax,%rdx,4)
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2acb
cmpl $0x2, %eax
jne 0x2b29
movl 0x68(%rbx), %ecx
movl 0x6c(%rbx), %eax
movq 0x40(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x18(%rbx), %edx
testl %edx, %edx
je 0x29fa
movl (%rcx,%rax,4), %esi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x2863
incl %r8d
movl %r8d, (%rcx,%rdi,4)
movl 0x18(%rbx), %edx
incq %rdi
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x2850
jmp 0x29fa
testl %r8d, %r8d
je 0x2933
movq 0x48(%rbx), %rcx
movl %esi, %r9d
movq (%rcx,%r9,8), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl %r11d, %ecx
movl (%r9,%rcx,4), %r15d
movl %r10d, %ecx
cmpl (%r9,%r10,4), %r15d
ja 0x28a0
movl %r11d, %ecx
incq %r10
movl %ecx, %r11d
cmpq %r10, %r8
jne 0x288d
jmp 0x2935
xorl %ebp, %ebp
cmpl $0x0, 0x20(%rbx)
jne 0x2b29
movq 0x28(%rbx), %rax
movq (%rax,%rdx,8), %rax
movl %edi, (%rax,%rcx,4)
movl 0x68(%rbx), %eax
movq (%r8,%rax,8), %rax
movl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq 0x38(%rbx), %rax
movl 0x68(%rbx), %esi
movq (%rax,%rsi,8), %rax
movl %edx, (%rax,%rcx,4)
movl 0x68(%rbx), %eax
cmpl $0x2, 0x1c(%rbx)
jne 0x2b6b
movq 0x40(%rbx), %rdx
movq (%rdx,%rax,8), %rax
movl 0x18(%rbx), %edx
testl %edx, %edx
je 0x2b78
movl (%rax,%rcx,4), %esi
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x291c
incl %r8d
movl %r8d, (%rax,%rdi,4)
movl 0x18(%rbx), %edx
incq %rdi
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x2909
movl $0x0, (%rax,%rcx,4)
jmp 0x2a64
xorl %ecx, %ecx
movl %ecx, 0x6c(%rbx)
movl %esi, %r9d
movl 0x6c(%rbx), %r10d
testq %rdi, %rdi
je 0x2a06
movq 0x28(%rbx), %rcx
movq (%rcx,%r9,8), %rcx
movl (%rcx,%r10,4), %r8d
movl %eax, %ecx
shll %cl, %esi
movl %edx, %ecx
shll %cl, %r8d
movq 0x38(%rbx), %rax
orl %esi, %r8d
movq (%rax,%r9,8), %rax
movl (%rax,%r10,4), %edx
movl %r8d, %esi
callq 0x224e
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2984
movl %r14d, %esi
callq 0x1840
movl 0x64(%rbx), %eax
movq 0x28(%rbx), %rcx
movl 0x68(%rbx), %edx
movq (%rcx,%rdx,8), %rcx
movl 0x6c(%rbx), %edx
movl %eax, (%rcx,%rdx,4)
movq 0x38(%rbx), %rcx
movl 0x68(%rbx), %eax
movl 0x6c(%rbx), %edx
movq (%rcx,%rax,8), %rax
movl $0x1, (%rax,%rdx,4)
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2acb
cmpl $0x2, %eax
jne 0x2b29
movl 0x68(%rbx), %ecx
movl 0x6c(%rbx), %eax
movq 0x40(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x18(%rbx), %edx
testl %edx, %edx
je 0x29fa
movl (%rcx,%rax,4), %esi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x29ef
incl %r8d
movl %r8d, (%rcx,%rdi,4)
movl 0x18(%rbx), %edx
incq %rdi
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x29dc
movl $0x0, (%rcx,%rax,4)
jmp 0x2b29
movq 0x38(%rbx), %rcx
movq (%rcx,%r9,8), %rdi
movl (%rdi,%r10,4), %edi
cmpl $0x1, %edi
je 0x2b3a
testl %edi, %edi
jne 0x29ad
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2a3b
movl %r14d, %esi
callq 0x1840
movl 0x68(%rbx), %r9d
movl 0x6c(%rbx), %r10d
movq 0x38(%rbx), %rcx
movl 0x64(%rbx), %eax
movq 0x28(%rbx), %rdx
movq (%rdx,%r9,8), %rdx
movl %eax, (%rdx,%r10,4)
jmp 0x299c
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rdx
movl (%rdx,%rax,4), %edx
incl %edx
movq (%rcx,%rax,8), %rax
movl %edx, (%rax,%r15,4)
xorl %ebp, %ebp
jmp 0x2b29
incl 0x74(%rbx)
movq 0x28(%rbx), %rax
movq (%rax,%rcx,8), %rax
movl %edi, (%rax,%rdx,4)
movl 0x1c(%rbx), %eax
cmpl $0x3, %eax
je 0x2acb
cmpl $0x2, %eax
jne 0x2b29
movl 0x68(%rbx), %ecx
movl 0x6c(%rbx), %eax
movq 0x40(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x18(%rbx), %edx
testl %edx, %edx
je 0x29fa
movl (%rcx,%rax,4), %esi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
cmpl %esi, %r8d
jae 0x2abb
incl %r8d
movl %r8d, (%rcx,%rdi,4)
movl 0x18(%rbx), %edx
incq %rdi
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x2aa8
jmp 0x29fa
movl 0x68(%rbx), %eax
movl 0x6c(%rbx), %ecx
movq 0x48(%rbx), %rdx
movq 0x50(%rbx), %rsi
movq (%rdx,%rax,8), %rdi
movl (%rdi,%rcx,4), %ecx
movl %ecx, (%rsi,%rax,4)
movl 0x68(%rbx), %eax
movl 0x6c(%rbx), %ecx
movl (%rsi,%rax,4), %esi
incl %esi
movq (%rdx,%rax,8), %rax
movl %esi, (%rax,%rcx,4)
jmp 0x2b29
movq 0x48(%rbx), %rax
movq (%rax,%rdx,8), %rax
jmp 0x2b09
movq 0x48(%rbx), %rax
movq (%rax,%r9,8), %rax
incl (%rax,%rcx,4)
movl $0x1, %ebp
cmpl $0x0, 0x20(%rbx)
jne 0x2b29
movq 0x38(%rbx), %rax
movl 0x68(%rbx), %edx
movq (%rax,%rdx,8), %rax
movl $0x1, (%rax,%rcx,4)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl 0x74(%rbx)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2984
movq 0x28(%rbx), %rcx
movq (%rcx,%r9,8), %rcx
movl (%rcx,%r10,4), %r8d
movl %edx, %ecx
shll %cl, %r8d
movl %eax, %ecx
shll %cl, %esi
orl %r8d, %esi
callq 0x243c
jmp 0x2974
movq 0x48(%rbx), %rdx
movq 0x50(%rbx), %rsi
jmp 0x2ae9
movl $0x0, (%rax,%rcx,4)
jmp 0x2b29
nop