// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]MQSim/src/ssd/Data_Cache_Manager_Flash_Simple.cpp
// Function: SSD_Components::Data_Cache_Manager_Flash_Simple::write_to_destage_buffer(SSD_Components::User_Request*)
void Data_Cache_Manager_Flash_Simple::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();

		while (it != user_request->Transaction_list.end()
			&& (back_pressure_buffer_depth + 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 (data_cache->Exists(tr->Stream_id, tr->LPA))//If the logical address already exists in the cache
			{
				/*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 = data_cache->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;
				}
				data_cache->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 (!data_cache->Check_free_slot_availability()) {
					Data_Cache_Slot_Type evicted_slot = data_cache->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 ")
					}
				}
				data_cache->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[0].find(tr->LPA) == bloom_filter[0].end()) {
				data_cache->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[0].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 += 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[0].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), %rax
movq %r14, 0x8(%rsp)
movq 0x40(%r14), %r13
movq %rax, 0x38(%rsp)
cmpq %rax, %r13
je 0x35cfe
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r15d, %r15d
leal (%r12,%r14), %eax
addl 0xe0(%rbx), %eax
cmpl 0xdc(%rbx), %eax
jae 0x35cef
movq %r12, 0x18(%rsp)
movq 0x10(%r13), %rbp
movq 0xc0(%rbx), %rdi
movzwl (%rbp), %esi
movq 0x58(%rbp), %rdx
callq 0x31522
testb %al, %al
je 0x35810
movq 0xc0(%rbx), %rsi
movzwl (%rbp), %edx
movq 0x58(%rbp), %rcx
leaq 0x40(%rsp), %rdi
callq 0x3158a
movq 0x58(%rsp), %r8
movq 0xa0(%rbp), %rax
cmpq %r8, %rax
jbe 0x35a5e
movq 0x80(%rbp), %rcx
movq %rax, %r8
jmp 0x35a63
movq 0xc0(%rbx), %rdi
callq 0x31624
testb %al, %al
jne 0x35a33
movq 0xc0(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3172e
cmpl $0x2, 0x60(%rsp)
jne 0x35a33
movl $0xb0, %edi
callq 0x73a0
movq %rax, %r12
movzwl (%rbp), %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 0x22c8f(%rip), %xmm2    # 0x58500
movdqa 0x22c77(%rip), %xmm3    # 0x584f0
movdqa 0x22c8e(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x22c8b(%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 0x35895
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 %r12, %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 %r12, 0x10(%rax)
movq %rax, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x7060
incq 0x10(%r12)
pxor %xmm0, %xmm0
movl $0x40, %eax
pshufd $0x44, 0x40(%rsp), %xmm1 # xmm1 = mem[0,1,0,1]
movdqa 0x22b80(%rip), %xmm2    # 0x58500
movdqa 0x22b68(%rip), %xmm3    # 0x584f0
movdqa 0x22b7f(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x22b7c(%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 0x359a4
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 0x18(%rsp), %eax
movq %rax, 0x18(%rsp)
movq 0xc0(%rbx), %rdi
movzwl (%rbp), %esi
movq 0x58(%rbp), %rdx
movq 0x80(%rbp), %rcx
movq 0xa0(%rbp), %r8
movq 0x98(%rbp), %r9
callq 0x31920
jmp 0x35a83
movq 0x50(%rsp), %rcx
movq 0xc0(%rbx), %rdi
movzwl (%rbp), %esi
movq 0x58(%rbp), %rdx
movq 0x40(%rsp), %r9
orq 0x98(%rbp), %r9
callq 0x31a44
leaq 0x58(%rbp), %r12
movq 0x98(%rbp), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x22a5a(%rip), %xmm1    # 0x58500
movdqa 0x22a42(%rip), %xmm2    # 0x584f0
movdqa 0x22a5a(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x22a57(%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 0x35ac9
movq 0xe8(%rbx), %rax
movq 0x10(%rax), %rcx
movq (%r12), %rdx
testq %rcx, %rcx
je 0x35b78
addq $0x8, %rax
movq %rax, %rsi
xorl %edi, %edi
cmpq %rdx, 0x20(%rcx)
setb %dil
cmovaeq %rcx, %rsi
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0x35b51
cmpq %rax, %rsi
je 0x35b78
cmpq 0x20(%rsi), %rdx
jae 0x35ca4
movdqa %xmm11, 0x80(%rsp)
movq 0xc0(%rbx), %rdi
movzwl (%rbp), %esi
callq 0x317b4
movq 0x98(%rbp), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x2294f(%rip), %xmm1    # 0x58500
movdqa 0x22937(%rip), %xmm2    # 0x584f0
movdqa 0x2294f(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x2294c(%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 0x35bd4
movdqa %xmm11, 0x70(%rsp)
movq 0xe8(%rbx), %rdi
movq %r12, %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, %r12d
addl %r14d, %r12d
movq %rbp, 0x10(%rax)
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7060
incq 0x30(%rsp)
movl %r12d, %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 (%r13), %r12
movq 0x8(%rsp), %rax
decq 0x50(%rax)
movq %r13, %rdi
callq 0x7580
movq %r13, %rdi
callq 0x7370
movq %r12, %r13
cmpq 0x38(%rsp), %r12
movq 0x18(%rsp), %r12
jne 0x3579c
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
sete %al
jmp 0x35d06
movb $0x1, %al
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x8(%rsp), %rcx
addl %r15d, 0x58(%rcx)
addl %r12d, %r14d
addl %r14d, 0xe0(%rbx)
testb %al, %al
leaq 0x20(%rsp), %r14
jne 0x35d5b
movl $0x18, %edi
callq 0x73a0
shll $0x9, %r12d
movl %r12d, (%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 0x35496
testl %r15d, %r15d
je 0x35d94
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 0x35496
cmpq $0x0, 0x30(%rsp)
je 0x35daf
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 0xf8(%rbx), %rax
jbe 0x35dec
movq 0xe8(%rbx), %rdi
callq 0x34754
callq 0x28018
movq %rax, %rdi
callq 0x2841a
addq 0xf0(%rbx), %rax
movq %rax, 0xf8(%rbx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x35e06
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x35df6
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x35e35
movq %rax, %rbx
movq %r12, %rdi
callq 0x7370
jmp 0x35e38
jmp 0x35e35
jmp 0x35e35
jmp 0x35e35
jmp 0x35e35
jmp 0x35e35
jmp 0x35e35
jmp 0x35e35
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x35e57
movq (%rdi), %r14
callq 0x7370
movq %r14, %rdi
cmpq %r15, %r14
jne 0x35e47
movq %rbx, %rdi
callq 0x7620
nop