// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%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 %r14, 0x10(%rsp)
movq 0x40(%r14), %r12
movl $0x0, %r14d
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movq %rcx, 0x38(%rsp)
cmpq %rcx, %r12
je 0x36780
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
addl %r14d, %eax
addl 0xe0(%rbx), %eax
cmpl 0xdc(%rbx), %eax
jae 0x36780
movq 0x10(%r12), %r13
movq 0xc0(%rbx), %rdi
movzwl (%r13), %esi
movq 0x58(%r13), %rdx
callq 0x31d3e
testb %al, %al
je 0x3628a
movq 0xc0(%rbx), %rsi
movzwl (%r13), %edx
movq 0x58(%r13), %rcx
leaq 0x40(%rsp), %rdi
callq 0x31dfc
movq 0x58(%rsp), %r8
movq 0xa0(%r13), %rax
cmpq %r8, %rax
jbe 0x364d9
movq 0x80(%r13), %rcx
movq %rax, %r8
jmp 0x364de
movq 0xc0(%rbx), %rdi
callq 0x31ee0
testb %al, %al
jne 0x364ad
movq 0xc0(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x31fd0
cmpl $0x2, 0x60(%rsp)
jne 0x364ad
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 0x22214(%rip), %xmm2    # 0x58500
movdqa 0x221fc(%rip), %xmm3    # 0x584f0
movdqa 0x22213(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x22210(%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 0x36310
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 0x18(%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 0x22106(%rip), %xmm2    # 0x58500
movdqa 0x220ee(%rip), %xmm3    # 0x584f0
movdqa 0x22105(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x22102(%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 0x3641e
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 0x8(%rsp), %eax
movq %rax, 0x8(%rsp)
movq 0xc0(%rbx), %rdi
movzwl (%r13), %esi
movq 0x58(%r13), %rdx
movq 0x80(%r13), %rcx
movq 0xa0(%r13), %r8
movq 0x98(%r13), %r9
callq 0x32284
jmp 0x364ff
movq 0x50(%rsp), %rcx
movq 0xc0(%rbx), %rdi
movzwl (%r13), %esi
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 0x21fdd(%rip), %xmm1    # 0x58500
movdqa 0x21fc5(%rip), %xmm2    # 0x584f0
movdqa 0x21fdd(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x21fda(%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 0x36546
movq 0xe8(%rbx), %rax
movq 0x10(%rax), %rdx
addq $0x8, %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x365e9
movq (%rbp), %rsi
movq %rax, %rcx
xorl %edi, %edi
cmpq %rsi, 0x20(%rdx)
setb %dil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rdi,8), %rdx
testq %rdx, %rdx
jne 0x365d1
movq %rax, %rdx
cmpq %rax, %rcx
je 0x36600
movq (%rbp), %rdx
cmpq 0x20(%rcx), %rdx
cmovbq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
jne 0x3673a
movdqa %xmm11, 0x80(%rsp)
movq 0xc0(%rbx), %rdi
movzwl (%r13), %esi
movq 0x58(%r13), %rdx
callq 0x3205a
movq 0x98(%r13), %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm11
movl $0x40, %eax
movdqa 0x21eb8(%rip), %xmm1    # 0x58500
movdqa 0x21ea0(%rip), %xmm2    # 0x584f0
movdqa 0x21eb8(%rip), %xmm7    # 0x58510
pxor %xmm8, %xmm8
pcmpeqd %xmm9, %xmm9
movdqa 0x21eb5(%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 0x3666b
movdqa %xmm11, 0x70(%rsp)
movq 0xe8(%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 0x10(%rsp), %rax
decq 0x50(%rax)
movq %r12, %rdi
callq 0x7580
movq %r12, %rdi
callq 0x7370
movq %r13, %r12
cmpq 0x38(%rsp), %r13
jne 0x36214
movq 0x10(%rsp), %rax
addl %r15d, 0x58(%rax)
addl 0x8(%rsp), %r14d
addl %r14d, 0xe0(%rbx)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x367dd
movl $0x18, %edi
callq 0x73a0
movq 0x8(%rsp), %rcx
shll $0x9, %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x35efa
testl %r15d, %r15d
leaq 0x20(%rsp), %r14
je 0x3681b
movl $0x18, %edi
callq 0x73a0
shll $0x9, %r15d
movl %r15d, (%rax)
movq 0x10(%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 0x35efa
cmpq $0x0, 0x30(%rsp)
je 0x36836
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 0xf8(%rbx), %rax
jbe 0x36873
movq 0xe8(%rbx), %rdi
callq 0x351b2
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
addq 0xf0(%rbx), %rax
movq %rax, 0xf8(%rbx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x3688d
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x3687d
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 0x368bf
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
jmp 0x368bc
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x368de
movq (%rdi), %r14
callq 0x7370
movq %r14, %rdi
cmpq %r15, %r14
jne 0x368ce
movq %rbx, %rdi
callq 0x7620