// File: /workspace/github_cpp_makefile_star2_O3/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 %ecx, %ecx
movq %rax, 0x8(%rsp)
movq %rcx, 0x10(%rax)
leaq 0x20(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %rcx, 0x10(%rax)
leaq 0x40(%r14), %rcx
movq 0x40(%r14), %r12
movq %r14, (%rsp)
movzwl 0x68(%r14), %eax
xorl %ebp, %ebp
cmpb $0x0, 0xe0(%rbx)
cmovnel %ebp, %eax
movl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rcx, 0x38(%rsp)
cmpq %rcx, %r12
je 0x33fad
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0xf0(%rbx), %rax
leal (%rbp,%r13), %ecx
movq 0x10(%rsp), %rdx
addl (%rax,%rdx,4), %ecx
cmpl 0xe8(%rbx), %ecx
jae 0x33f9e
movq %r13, 0x18(%rsp)
movq 0x10(%r12), %r14
movq 0xc0(%rbx), %rax
movzwl (%r14), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r14), %rdx
callq 0x31522
testb %al, %al
je 0x33a8a
movq 0xc0(%rbx), %rax
movzwl (%r14), %edx
movq (%rax,%rdx,8), %rsi
movq 0x58(%r14), %rcx
leaq 0x40(%rsp), %rdi
callq 0x3158a
movq 0x58(%rsp), %r8
movq 0xa0(%r14), %rax
cmpq %r8, %rax
jbe 0x33ce6
movq 0x80(%r14), %rcx
movq %rax, %r8
jmp 0x33ceb
movq 0xc0(%rbx), %rax
movzwl (%r14), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x31624
testb %al, %al
jne 0x33cb7
movq 0xc0(%rbx), %rax
movzwl (%r14), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3172e
cmpl $0x2, 0x60(%rsp)
jne 0x33cb7
movl $0xb0, %edi
callq 0x73a0
movq %rax, %r13
movzwl (%r14), %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 0x24a05(%rip), %xmm2    # 0x58500
movdqa 0x249ed(%rip), %xmm3    # 0x584f0
movdqa 0x24a04(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x24a01(%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 0x33b1f
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 %r13, %rdi
movl $0x1, %esi
xorl %r9d, %r9d
pushq 0x58(%rsp)
pushq %rax
pushq 0x60(%rsp)
pushq $0x0
callq 0x48218
addq $0x20, %rsp
movl $0x18, %edi
callq 0x73a0
movq %r13, 0x10(%rax)
movq %rax, %rdi
movq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x7060
incq 0x10(%r13)
pxor %xmm0, %xmm0
movl $0x40, %eax
pshufd $0x44, 0x40(%rsp), %xmm1 # xmm1 = mem[0,1,0,1]
movdqa 0x248f7(%rip), %xmm2    # 0x58500
movdqa 0x248df(%rip), %xmm3    # 0x584f0
movdqa 0x248f6(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x248f3(%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 0x33c2d
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 %ebp, %eax
movl %eax, %ebp
movq 0xc0(%rbx), %rax
movzwl (%r14), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r14), %rdx
movq 0x80(%r14), %rcx
movq 0xa0(%r14), %r8
movq 0x98(%r14), %r9
callq 0x31920
jmp 0x33d0f
movq 0x50(%rsp), %rcx
movq 0xc0(%rbx), %rax
movzwl (%r14), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r14), %rdx
movq 0x40(%rsp), %r9
orq 0x98(%r14), %r9
callq 0x31a44
leaq 0x58(%r14), %r13
movq 0x98(%r14), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x247cd(%rip), %xmm1    # 0x58500
movdqa 0x247b5(%rip), %xmm2    # 0x584f0
movdqa 0x247cd(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x247ca(%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 0x33d56
movq 0xf8(%rbx), %rcx
movzwl (%r14), %esi
leaq (%rsi,%rsi,2), %rdi
shll $0x4, %edi
movq 0x10(%rcx,%rdi), %rax
movq 0x58(%r14), %rdx
testq %rax, %rax
je 0x33e15
addq %rcx, %rdi
addq $0x8, %rdi
movq %rdi, %rcx
xorl %r8d, %r8d
cmpq %rdx, 0x20(%rax)
setb %r8b
cmovaeq %rax, %rcx
movq 0x10(%rax,%r8,8), %rax
testq %rax, %rax
jne 0x33ded
cmpq %rdi, %rcx
je 0x33e15
cmpq 0x20(%rcx), %rdx
jae 0x33f97
movdqa %xmm11, 0x80(%rsp)
movq 0xc0(%rbx), %rax
movq (%rax,%rsi,8), %rdi
callq 0x317b4
movq 0x98(%r14), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x246b1(%rip), %xmm1    # 0x58500
movdqa 0x24699(%rip), %xmm2    # 0x584f0
movdqa 0x246b1(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x246ae(%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 0x33e72
movdqa %xmm11, 0x70(%rsp)
movq (%rsp), %rax
movzwl 0x68(%rax), %eax
leaq (%rax,%rax,2), %rdi
shll $0x4, %edi
addq 0xf8(%rbx), %rdi
movq %r13, %rsi
callq 0x24406
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, %r13d
addl 0x18(%rsp), %r13d
movq %r14, 0x10(%rax)
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7060
incq 0x30(%rsp)
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), %r14
movq (%rsp), %rax
decq 0x50(%rax)
movq %r12, %rdi
callq 0x7580
movq %r12, %rdi
callq 0x7370
movq %r14, %r12
cmpq 0x38(%rsp), %r14
jne 0x33a03
jmp 0x33f9e
movq 0x18(%rsp), %r13
jmp 0x33f50
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
sete %al
jmp 0x33fb7
xorl %r15d, %r15d
movb $0x1, %al
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rsp), %rcx
addl %r15d, 0x58(%rcx)
addl %ebp, %r13d
movq 0xf0(%rbx), %rcx
movq 0x10(%rsp), %rdx
addl %r13d, (%rcx,%rdx,4)
testb %al, %al
jne 0x3400c
movl $0x18, %edi
callq 0x73a0
shll $0x9, %ebp
movl %ebp, (%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq (%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x336f4
testl %r15d, %r15d
leaq 0x20(%rsp), %r14
je 0x34049
movl $0x18, %edi
callq 0x73a0
shll $0x9, %r15d
movl %r15d, (%rax)
movq (%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 0x336f4
cmpq $0x0, 0x30(%rsp)
je 0x34064
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
callq *0x60(%rax)
callq 0x28018
movq %rax, %rdi
callq 0x2841a
cmpq 0x108(%rbx), %rax
jbe 0x340b0
movq (%rsp), %rax
movzwl 0x68(%rax), %eax
leaq (%rax,%rax,2), %rdi
shll $0x4, %edi
addq 0xf8(%rbx), %rdi
callq 0x34754
callq 0x28018
movq %rax, %rdi
callq 0x2841a
addq 0x100(%rbx), %rax
movq %rax, 0x108(%rbx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x340ca
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x340ba
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x340f9
movq %rax, %rbx
movq %r13, %rdi
callq 0x7370
jmp 0x340fc
jmp 0x340f9
jmp 0x340f9
jmp 0x340f9
jmp 0x340f9
jmp 0x340f9
jmp 0x340f9
jmp 0x340f9
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x3411b
movq (%rdi), %r14
callq 0x7370
movq %r14, %rdi
cmpq %r15, %r14
jne 0x3410b
movq %rbx, %rdi
callq 0x7620
nop