// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]MQSim/src/ssd/Data_Cache_Manager_Flash_Advanced.cpp
// Function: SSD_Components::Data_Cache_Manager_Flash_Advanced::write_to_destage_buffer(SSD_Components::User_Request*)
void Data_Cache_Manager_Flash_Advanced::write_to_destage_buffer(User_Request* user_request)
	{
		//To eliminate race condition, MQSim assumes the management information and user data are stored in separate DRAM modules
		unsigned int cache_eviction_read_size_in_sectors = 0;//The size of data evicted from cache
		unsigned int flash_written_back_write_size_in_sectors = 0;//The size of data that is both written back to flash and written to DRAM
		unsigned int dram_write_size_in_sectors = 0;//The size of data written to DRAM (must be >= flash_written_back_write_size_in_sectors)
		std::list<NVM_Transaction*>* evicted_cache_slots = new std::list<NVM_Transaction*>;
		std::list<NVM_Transaction*> writeback_transactions;
		auto it = user_request->Transaction_list.begin();

		int queue_id = user_request->Stream_id;
		if (shared_dram_request_queue) {
			queue_id = 0;
		}

		while (it != user_request->Transaction_list.end() 
			&& (back_pressure_buffer_depth[queue_id] + cache_eviction_read_size_in_sectors + flash_written_back_write_size_in_sectors) < back_pressure_buffer_max_depth) {
			NVM_Transaction_Flash_WR* tr = (NVM_Transaction_Flash_WR*)(*it);
			//If the logical address already exists in the cache
			if (per_stream_cache[tr->Stream_id]->Exists(tr->Stream_id, tr->LPA)) {
				/*MQSim should get rid of writting stale data to the cache.
				* This situation may result from out-of-order transaction execution*/
				Data_Cache_Slot_Type slot = per_stream_cache[tr->Stream_id]->Get_slot(tr->Stream_id, tr->LPA);
				sim_time_type timestamp = slot.Timestamp;
				NVM::memory_content_type content = slot.Content;
				if (tr->DataTimeStamp > timestamp) {
					timestamp = tr->DataTimeStamp;
					content = tr->Content;
				}
				per_stream_cache[tr->Stream_id]->Update_data(tr->Stream_id, tr->LPA, content, timestamp, tr->write_sectors_bitmap | slot.State_bitmap_of_existing_sectors);
			} else {//the logical address is not in the cache
				if (!per_stream_cache[tr->Stream_id]->Check_free_slot_availability()) {
					Data_Cache_Slot_Type evicted_slot = per_stream_cache[tr->Stream_id]->Evict_one_slot_lru();
					if (evicted_slot.Status == Cache_Slot_Status::DIRTY_NO_FLASH_WRITEBACK) {
						evicted_cache_slots->push_back(new NVM_Transaction_Flash_WR(Transaction_Source_Type::CACHE,
							tr->Stream_id, count_sector_no_from_status_bitmap(evicted_slot.State_bitmap_of_existing_sectors) * SECTOR_SIZE_IN_BYTE,
							evicted_slot.LPA, NULL, IO_Flow_Priority_Class::URGENT, evicted_slot.Content, evicted_slot.State_bitmap_of_existing_sectors, evicted_slot.Timestamp));
						cache_eviction_read_size_in_sectors += count_sector_no_from_status_bitmap(evicted_slot.State_bitmap_of_existing_sectors);
						//DEBUG2("Evicting page" << evicted_slot.LPA << " from write buffer ")
					}
				}
				per_stream_cache[tr->Stream_id]->Insert_write_data(tr->Stream_id, tr->LPA, tr->Content, tr->DataTimeStamp, tr->write_sectors_bitmap);
			}
			dram_write_size_in_sectors += count_sector_no_from_status_bitmap(tr->write_sectors_bitmap);
			//hot/cold data separation
			if (bloom_filter[tr->Stream_id].find(tr->LPA) == bloom_filter[tr->Stream_id].end()) {
				per_stream_cache[tr->Stream_id]->Change_slot_status_to_writeback(tr->Stream_id, tr->LPA); //Eagerly write back cold data
				flash_written_back_write_size_in_sectors += count_sector_no_from_status_bitmap(tr->write_sectors_bitmap);
				bloom_filter[user_request->Stream_id].insert(tr->LPA);
				writeback_transactions.push_back(tr);
			}
			user_request->Transaction_list.erase(it++);
		}
		
		user_request->Sectors_serviced_from_cache += dram_write_size_in_sectors;//This is very important update. It is used to decide when all data sectors of a user request are serviced
		back_pressure_buffer_depth[queue_id] += cache_eviction_read_size_in_sectors + flash_written_back_write_size_in_sectors;

		//Issue memory read for cache evictions
		if (evicted_cache_slots->size() > 0) {
			Memory_Transfer_Info* read_transfer_info = new Memory_Transfer_Info;
			read_transfer_info->Size_in_bytes = cache_eviction_read_size_in_sectors * SECTOR_SIZE_IN_BYTE;
			read_transfer_info->Related_request = evicted_cache_slots;
			read_transfer_info->next_event_type = Data_Cache_Simulation_Event_Type::MEMORY_READ_FOR_CACHE_EVICTION_FINISHED;
			read_transfer_info->Stream_id = user_request->Stream_id;
			service_dram_access_request(read_transfer_info);
		}

		//Issue memory write to write data to DRAM
		if (dram_write_size_in_sectors) {
			Memory_Transfer_Info* write_transfer_info = new Memory_Transfer_Info;
			write_transfer_info->Size_in_bytes = dram_write_size_in_sectors * SECTOR_SIZE_IN_BYTE;
			write_transfer_info->Related_request = user_request;
			write_transfer_info->next_event_type = Data_Cache_Simulation_Event_Type::MEMORY_WRITE_FOR_USERIO_FINISHED;
			write_transfer_info->Stream_id = user_request->Stream_id;
			service_dram_access_request(write_transfer_info);
		}

		//If any writeback should be performed, then issue flash write transactions
		if (writeback_transactions.size() > 0) {
					static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(writeback_transactions);
		}
		
		//Reset control data structures used for hot/cold separation 
		if (Simulator->Time() > next_bloom_filter_reset_milestone) {
			bloom_filter[user_request->Stream_id].clear();
			next_bloom_filter_reset_milestone = Simulator->Time() + bloom_filter_reset_step;
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl $0x18, %edi
callq 0x73a0
movq %rax, 0x8(%rax)
movq %rax, (%rax)
xorl %r15d, %r15d
movq %rax, 0x10(%rsp)
movq %r15, 0x10(%rax)
leaq 0x20(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %r15, 0x10(%rax)
leaq 0x40(%r14), %rcx
movq 0x40(%r14), %r12
movq %r14, 0x8(%rsp)
movzwl 0x68(%r14), %eax
cmpb $0x0, 0xe0(%rbx)
cmovnel %r15d, %eax
movl %eax, %eax
movq %rax, 0x18(%rsp)
movl $0x0, %r14d
movl $0x0, %eax
movq %rax, (%rsp)
movq %rcx, 0x38(%rsp)
cmpq %rcx, %r12
je 0x34a06
movq $0x0, (%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0xf0(%rbx), %rax
movq (%rsp), %rcx
addl %r14d, %ecx
movq 0x18(%rsp), %rdx
addl (%rax,%rdx,4), %ecx
cmpl 0xe8(%rbx), %ecx
jae 0x34a06
movq 0x10(%r12), %r13
movq 0xc0(%rbx), %rax
movzwl (%r13), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r13), %rdx
callq 0x31d3e
testb %al, %al
je 0x344d9
movq 0xc0(%rbx), %rax
movzwl (%r13), %edx
movq (%rax,%rdx,8), %rsi
movq 0x58(%r13), %rcx
leaq 0x40(%rsp), %rdi
callq 0x31dfc
movq 0x58(%rsp), %r8
movq 0xa0(%r13), %rax
cmpq %r8, %rax
jbe 0x3473c
movq 0x80(%r13), %rcx
movq %rax, %r8
jmp 0x34741
movq 0xc0(%rbx), %rax
movzwl (%r13), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x31ee0
testb %al, %al
jne 0x3470c
movq 0xc0(%rbx), %rax
movzwl (%r13), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0x40(%rsp), %rdi
callq 0x31fd0
cmpl $0x2, 0x60(%rsp)
jne 0x3470c
movl $0xb0, %edi
callq 0x73a0
movq %rax, %rbp
movzwl (%r13), %edx
movq 0x40(%rsp), %rax
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm1     # xmm1 = xmm0[0,1,0,1]
pxor %xmm0, %xmm0
movl $0x40, %ecx
movdqa 0x23fb3(%rip), %xmm2    # 0x58500
movdqa 0x23f9b(%rip), %xmm3    # 0x584f0
movdqa 0x23fb2(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x23faf(%rip), %xmm11   # 0x58520
movdqa %xmm8, %xmm4
psllq %xmm2, %xmm4
pshufd $0xee, %xmm2, %xmm5     # xmm5 = xmm2[2,3,2,3]
movdqa %xmm8, %xmm6
psllq %xmm5, %xmm6
movsd %xmm4, %xmm6            # xmm6 = xmm4[0],xmm6[1]
movdqa %xmm8, %xmm4
psllq %xmm3, %xmm4
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
movdqa %xmm8, %xmm7
psllq %xmm5, %xmm7
movsd %xmm4, %xmm7            # xmm7 = xmm4[0],xmm7[1]
andpd %xmm1, %xmm7
andpd %xmm1, %xmm6
pcmpeqd %xmm9, %xmm6
pcmpeqd %xmm9, %xmm7
movdqa %xmm6, %xmm4
shufps $0xdd, %xmm7, %xmm4     # xmm4 = xmm4[1,3],xmm7[1,3]
shufps $0x88, %xmm7, %xmm6     # xmm6 = xmm6[0,2],xmm7[0,2]
andps %xmm4, %xmm6
paddd %xmm6, %xmm0
psubd %xmm10, %xmm0
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rcx
jne 0x34571
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %ecx
shll $0x9, %ecx
movq 0x48(%rsp), %r8
movzwl %dx, %edx
movq %rbp, %rdi
movl $0x1, %esi
xorl %r9d, %r9d
pushq 0x58(%rsp)
pushq %rax
pushq 0x60(%rsp)
pushq $0x0
callq 0x48c74
addq $0x20, %rsp
movl $0x18, %edi
callq 0x73a0
movq %rbp, 0x10(%rax)
movq %rax, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0x7060
incq 0x10(%rbp)
pxor %xmm0, %xmm0
movl $0x40, %eax
pshufd $0x44, 0x40(%rsp), %xmm1 # xmm1 = mem[0,1,0,1]
movdqa 0x23ea5(%rip), %xmm2    # 0x58500
movdqa 0x23e8d(%rip), %xmm3    # 0x584f0
movdqa 0x23ea4(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x23ea1(%rip), %xmm11   # 0x58520
movdqa %xmm8, %xmm4
psllq %xmm2, %xmm4
pshufd $0xee, %xmm2, %xmm5     # xmm5 = xmm2[2,3,2,3]
movdqa %xmm8, %xmm6
psllq %xmm5, %xmm6
movsd %xmm4, %xmm6            # xmm6 = xmm4[0],xmm6[1]
movdqa %xmm8, %xmm4
psllq %xmm3, %xmm4
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
movdqa %xmm8, %xmm7
psllq %xmm5, %xmm7
movsd %xmm4, %xmm7            # xmm7 = xmm4[0],xmm7[1]
andpd %xmm1, %xmm7
andpd %xmm1, %xmm6
pcmpeqd %xmm9, %xmm6
pcmpeqd %xmm9, %xmm7
movdqa %xmm6, %xmm4
shufps $0xdd, %xmm7, %xmm4     # xmm4 = xmm4[1,3],xmm7[1,3]
shufps $0x88, %xmm7, %xmm6     # xmm6 = xmm6[0,2],xmm7[0,2]
andps %xmm4, %xmm6
paddd %xmm6, %xmm0
psubd %xmm10, %xmm0
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rax
jne 0x3467f
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %eax
addl (%rsp), %eax
movq %rax, (%rsp)
movq 0xc0(%rbx), %rax
movzwl (%r13), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r13), %rdx
movq 0x80(%r13), %rcx
movq 0xa0(%r13), %r8
movq 0x98(%r13), %r9
callq 0x32284
jmp 0x34766
movq 0x50(%rsp), %rcx
movq 0xc0(%rbx), %rax
movzwl (%r13), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r13), %rdx
movq 0x40(%rsp), %r9
orq 0x98(%r13), %r9
callq 0x32410
leaq 0x58(%r13), %rbp
movq 0x98(%r13), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x23d76(%rip), %xmm1    # 0x58500
movdqa 0x23d5e(%rip), %xmm2    # 0x584f0
movdqa 0x23d76(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x23d73(%rip), %xmm10   # 0x58520
movdqa %xmm7, %xmm3
psllq %xmm1, %xmm3
pshufd $0xee, %xmm1, %xmm4     # xmm4 = xmm1[2,3,2,3]
movdqa %xmm7, %xmm5
psllq %xmm4, %xmm5
movsd %xmm3, %xmm5            # xmm5 = xmm3[0],xmm5[1]
movdqa %xmm7, %xmm3
psllq %xmm2, %xmm3
pshufd $0xee, %xmm2, %xmm4     # xmm4 = xmm2[2,3,2,3]
movdqa %xmm7, %xmm6
psllq %xmm4, %xmm6
movsd %xmm3, %xmm6            # xmm6 = xmm3[0],xmm6[1]
andpd %xmm0, %xmm6
andpd %xmm0, %xmm5
pcmpeqd %xmm8, %xmm5
pcmpeqd %xmm8, %xmm6
movdqa %xmm5, %xmm3
shufps $0xdd, %xmm6, %xmm3     # xmm3 = xmm3[1,3],xmm6[1,3]
shufps $0x88, %xmm6, %xmm5     # xmm5 = xmm5[0,2],xmm6[0,2]
andps %xmm3, %xmm5
paddd %xmm5, %xmm11
psubd %xmm9, %xmm11
paddq %xmm10, %xmm1
paddq %xmm10, %xmm2
addq $-0x4, %rax
jne 0x347ad
movq 0xf8(%rbx), %rax
movzwl (%r13), %esi
leaq (%rsi,%rsi,2), %rcx
shll $0x4, %ecx
addq %rcx, %rax
addq $0x8, %rax
movq 0x8(%rax), %rdx
movq %rax, %rcx
testq %rdx, %rdx
je 0x34860
movq (%rbp), %rdi
movq %rax, %rcx
xorl %r8d, %r8d
cmpq %rdi, 0x20(%rdx)
setb %r8b
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%r8,8), %rdx
testq %rdx, %rdx
jne 0x34847
movq %rax, %rdx
cmpq %rax, %rcx
je 0x34877
movq (%rbp), %rdx
cmpq 0x20(%rcx), %rdx
cmovbq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
jne 0x349c0
movdqa %xmm11, 0x80(%rsp)
movq 0xc0(%rbx), %rax
movq (%rax,%rsi,8), %rdi
movq (%rbp), %rdx
callq 0x3205a
movq 0x98(%r13), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x23c42(%rip), %xmm1    # 0x58500
movdqa 0x23c2a(%rip), %xmm2    # 0x584f0
movdqa 0x23c42(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x23c3f(%rip), %xmm10   # 0x58520
movdqa %xmm7, %xmm3
psllq %xmm1, %xmm3
pshufd $0xee, %xmm1, %xmm4     # xmm4 = xmm1[2,3,2,3]
movdqa %xmm7, %xmm5
psllq %xmm4, %xmm5
movsd %xmm3, %xmm5            # xmm5 = xmm3[0],xmm5[1]
movdqa %xmm7, %xmm3
psllq %xmm2, %xmm3
pshufd $0xee, %xmm2, %xmm4     # xmm4 = xmm2[2,3,2,3]
movdqa %xmm7, %xmm6
psllq %xmm4, %xmm6
movsd %xmm3, %xmm6            # xmm6 = xmm3[0],xmm6[1]
andpd %xmm0, %xmm6
andpd %xmm0, %xmm5
pcmpeqd %xmm8, %xmm5
pcmpeqd %xmm8, %xmm6
movdqa %xmm5, %xmm3
shufps $0xdd, %xmm6, %xmm3     # xmm3 = xmm3[1,3],xmm6[1,3]
shufps $0x88, %xmm6, %xmm5     # xmm5 = xmm5[0,2],xmm6[0,2]
andps %xmm3, %xmm5
paddd %xmm5, %xmm11
psubd %xmm9, %xmm11
paddq %xmm10, %xmm1
paddq %xmm10, %xmm2
addq $-0x4, %rax
jne 0x348e1
movdqa %xmm11, 0x70(%rsp)
movq 0x8(%rsp), %rax
movzwl 0x68(%rax), %eax
leaq (%rax,%rax,2), %rdi
shll $0x4, %edi
addq 0xf8(%rbx), %rdi
movq %rbp, %rsi
callq 0x245a0
movl $0x18, %edi
callq 0x73a0
movdqa 0x70(%rsp), %xmm1
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
paddd %xmm1, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %ebp
addl %r14d, %ebp
movq %r13, 0x10(%rax)
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7060
incq 0x30(%rsp)
movl %ebp, %r14d
movdqa 0x80(%rsp), %xmm11
pshufd $0xee, %xmm11, %xmm0    # xmm0 = xmm11[2,3,2,3]
paddd %xmm11, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %eax
addl %eax, %r15d
movq (%r12), %r13
movq 0x8(%rsp), %rax
decq 0x50(%rax)
movq %r12, %rdi
callq 0x7580
movq %r12, %rdi
callq 0x7370
movq %r13, %r12
cmpq 0x38(%rsp), %r13
jne 0x34453
movq 0x8(%rsp), %rax
addl %r15d, 0x58(%rax)
addl (%rsp), %r14d
movq 0xf0(%rbx), %rax
movq 0x18(%rsp), %rcx
addl %r14d, (%rax,%rcx,4)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x34a6a
movl $0x18, %edi
callq 0x73a0
movq (%rsp), %rcx
shll $0x9, %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3412a
testl %r15d, %r15d
leaq 0x20(%rsp), %r14
je 0x34aa8
movl $0x18, %edi
callq 0x73a0
shll $0x9, %r15d
movl %r15d, (%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x3, 0x10(%rax)
movzwl 0x68(%rcx), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3412a
cmpq $0x0, 0x30(%rsp)
je 0x34ac3
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
callq *0x60(%rax)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
cmpq 0x108(%rbx), %rax
jbe 0x34b10
movq 0x8(%rsp), %rax
movzwl 0x68(%rax), %eax
leaq (%rax,%rax,2), %rdi
shll $0x4, %edi
addq 0xf8(%rbx), %rdi
callq 0x351b2
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
addq 0x100(%rbx), %rax
movq %rax, 0x108(%rbx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x34b2a
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x34b1a
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %rbp, %rdi
callq 0x7370
jmp 0x34b5c
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
jmp 0x34b59
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x34b7b
movq (%rdi), %r14
callq 0x7370
movq %r14, %rdi
cmpq %r15, %r14
jne 0x34b6b
movq %rbx, %rdi
callq 0x7620
nop