// File: /workspace/github_cpp_makefile_star2_O1/gvaumour[P]cache-simulator/src/Hierarchy.cc
// Function: Hierarchy::signalWB(Access, bool, int)
void
Hierarchy::signalWB(Access wb_request, bool isKept, int idcore)
{

	if(idcore == -1)
	{
		// It is a WB from the LLC to the Main Mem
		if(wb_request.m_type == MemCmd::DIRTY_WRITEBACK)
			stats_dirtyWB_MainMem++;		
		else if(wb_request.m_type == MemCmd::CLEAN_WRITEBACK)
			stats_cleanWB_MainMem++;
		else
			assert(false && "Error on the type of the write back request");

		string state = directory_state_str[m_directory->getState(wb_request.m_address)];
		//cout << "WB Request address = " << std::hex << wb_request.m_address << ", DirectoryState = " << state  << endl;

		uint64_t addr =  wb_request.m_address;
		DirectoryState dir_state = m_directory->getEntry(addr)->coherence_state;
		
		if( dir_state == CLEAN_LLC || dir_state == DIRTY_LLC )
			m_directory->setCoherenceState(addr , NOT_PRESENT);
		else if( dir_state == SHARED_L1)
			m_directory->setCoherenceState(addr , SHARED_L1_NOT_LLC);
		else if( dir_state == EXCLUSIVE_L1)
			m_directory->setCoherenceState(addr , EXCLUSIVE_L1_NOT_LLC);
		else if( dir_state == MODIFIED_L1)
			m_directory->setCoherenceState(addr , MODIFIED_L1_NOT_LLC);
		else{
			cout << "Error dir_state = " << directory_state_str[dir_state] << endl;
			assert(false);
		}
	
		state = directory_state_str[m_directory->getState(wb_request.m_address)];
		//cout << "WB Request address = " << std::hex << wb_request.m_address << ", DirectoryState = " << state  << endl;
			
//		m_directory->removeEntry(wb_request.m_address);
	}			
	else{
	
		string dir_state = string( directory_state_str[m_directory->getState(wb_request.m_address)]);
		DPRINTF( DebugHierarchy , "Write Back Block %#lx , Id Core = %d , Dir State = %s \n" , wb_request.m_address , idcore , dir_state.c_str()); 
		//Remove the idcore from the tracker list and update the state
		if(!isKept)
			m_directory->removeTracker(wb_request.m_address , idcore);
		
		if( m_LLC->handleAccess(wb_request) == BYPASS_CACHE)
		{
			if(m_directory->getTrackers(wb_request.m_address).size() == 0)
				m_directory->setCoherenceState(wb_request.m_address , NOT_PRESENT);
		
			if(wb_request.m_type == MemCmd::DIRTY_WRITEBACK)
				stats_dirtyWB_MainMem++;		
			else if(wb_request.m_type == MemCmd::CLEAN_WRITEBACK)
				stats_cleanWB_MainMem++;
			else
				assert(false && "Error on the type of the write back request");
		}
		else
		{
			
			if(m_directory->getTrackers(wb_request.m_address).size() == 0)
				m_directory->setCoherenceState(wb_request.m_address , CLEAN_LLC);
			else
				m_directory->setCoherenceState(wb_request.m_address , SHARED_L1);
		}
		dir_state = directory_state_str[m_directory->getState(wb_request.m_address)];
		DPRINTF( DebugHierarchy , "End Write Back Block %#lx Dir State is now %s \n" , wb_request.m_address , dir_state.c_str()); 
		//m_directory->updateEntry(wb_request.m_address);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebp
movq %rdi, %rbx
leaq 0xd0(%rsp), %rdx
cmpl $-0x1, %ebp
je 0x17396
movl %esi, %r12d
movq 0x20(%rbx), %rdi
movq (%rdx), %r14
movq %r14, %rsi
callq 0x133c2
movl %eax, %eax
leaq 0x2e150(%rip), %rcx     # 0x453b0
movq (%rcx,%rax,8), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x5140
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1e105(%rip), %rsi     # 0x35388
leaq 0x1e10c(%rip), %rdx     # 0x35396
leaq 0x40(%rsp), %rdi
callq 0xccac
leaq 0x2ecb5(%rip), %r15     # 0x45f50
leaq 0x8(%r15), %rdi
leaq 0x40(%rsp), %rsi
movq %rdi, 0x70(%rsp)
callq 0x182c0
movq %rax, %r13
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x172c5
callq 0x52a0
addq $0x10, %r15
cmpq %r15, %r13
je 0x172e6
movq 0x78(%rsp), %rcx
leaq 0x1e23e(%rip), %rdi     # 0x35518
movq %r14, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x5050
testb %r12b, %r12b
jne 0x172f9
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x133f2
movq 0x18(%rbx), %rdi
leaq 0xd0(%rsp), %rcx
movq 0x30(%rcx), %rax
movq %rax, 0x30(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x1ec36
cmpl $0x2, %eax
jne 0x1745d
movq 0x20(%rbx), %rsi
leaq 0x40(%rsp), %rdi
movq %r14, %rdx
callq 0x1337e
leaq 0x40(%rsp), %rdi
movq 0x28(%rdi), %r12
callq 0x138d6
testq %r12, %r12
jne 0x1736a
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x13482
leaq 0xd0(%rsp), %rax
movl 0x1c(%rax), %ecx
movl $0x78, %eax
cmpl $0x6, %ecx
je 0x1738d
cmpl $0x5, %ecx
jne 0x175ca
movl $0x70, %eax
incq (%rbx,%rax)
jmp 0x17495
movl 0x1c(%rdx), %ecx
movl $0x78, %eax
cmpl $0x6, %ecx
je 0x173b1
cmpl $0x5, %ecx
jne 0x1755c
movl $0x70, %eax
incq (%rbx,%rax)
movq 0x20(%rbx), %rdi
movq (%rdx), %r14
movq %r14, %rsi
callq 0x133c2
movl %eax, %eax
leaq 0x2dfe3(%rip), %r15     # 0x453b0
movq (%r15,%rax,8), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x5140
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x13236
movl 0x38(%rax), %r12d
movl %r12d, %eax
andl $-0x2, %eax
movl $0x8, %edx
cmpl $0x6, %eax
je 0x17411
cmpl $0x2, %r12d
ja 0x1757b
addl $0x3, %r12d
movl %r12d, %edx
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x13482
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x133c2
movl %eax, %eax
movq (%r15,%rax,8), %rbx
leaq 0x40(%rsp), %r14
movq 0x8(%r14), %r15
movq %rbx, %rdi
callq 0x5160
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x54b0
leaq 0x50(%rsp), %rax
jmp 0x1753c
movq 0x20(%rbx), %rsi
leaq 0x40(%rsp), %rdi
movq %r14, %rdx
callq 0x1337e
xorl %eax, %eax
leaq 0x40(%rsp), %rdi
cmpq $0x0, 0x28(%rdi)
movl $0x6, %ebp
cmovnel %eax, %ebp
callq 0x138d6
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x13482
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x133c2
movl %eax, %eax
leaq 0x2df06(%rip), %rcx     # 0x453b0
movq (%rcx,%rax,8), %rbx
leaq 0x78(%rsp), %r12
movq 0x8(%r12), %r13
movq %rbx, %rdi
callq 0x5160
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x54b0
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
movq %rax, (%rdi)
leaq 0x1dea1(%rip), %rsi     # 0x35388
leaq 0x1dea8(%rip), %rdx     # 0x35396
callq 0xccac
leaq 0x40(%rsp), %rsi
movq 0x70(%rsp), %rdi
callq 0x182c0
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x17519
callq 0x52a0
cmpq %r15, %rbx
je 0x17534
movq 0x78(%rsp), %rdx
leaq 0x1e026(%rip), %rdi     # 0x35550
movq %r14, %rsi
xorl %eax, %eax
callq 0x5050
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1754a
callq 0x52a0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1df28(%rip), %rdi     # 0x3548b
leaq 0x1df58(%rip), %rsi     # 0x354c2
leaq 0x1df62(%rip), %rcx     # 0x354d3
movl $0xa4, %edx
callq 0x51d0
movq 0x2da4e(%rip), %rdi     # 0x44fd0
leaq 0x1df76(%rip), %rsi     # 0x354ff
movl $0x12, %edx
callq 0x5350
movq (%r15,%r12,8), %rsi
movq 0x2da32(%rip), %rdi     # 0x44fd0
callq 0x52c0
movq %rax, %rdi
callq 0x5180
leaq 0x1df60(%rip), %rdi     # 0x35512
leaq 0x1df09(%rip), %rsi     # 0x354c2
leaq 0x1df13(%rip), %rcx     # 0x354d3
movl $0xb6, %edx
callq 0x51d0
leaq 0x1deba(%rip), %rdi     # 0x3548b
leaq 0x1deea(%rip), %rsi     # 0x354c2
leaq 0x1def4(%rip), %rcx     # 0x354d3
movl $0xd0, %edx
callq 0x51d0
jmp 0x1761d
jmp 0x1761d
jmp 0x1760e
jmp 0x175f3
jmp 0x1761d
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x17620
callq 0x52a0
jmp 0x17620
jmp 0x1761d
movq %rax, %rbx
jmp 0x17636
movq %rax, %rbx
leaq 0x50(%rsp), %rax
jmp 0x17628
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17636
callq 0x52a0
movq %rbx, %rdi
callq 0x55a0