// 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::process_new_user_request(SSD_Components::User_Request*)
void Data_Cache_Manager_Flash_Advanced::process_new_user_request(User_Request* user_request)
	{
		//This condition shouldn't happen, but we check it
		if (user_request->Transaction_list.size() == 0) {
			return;
		}

		if (user_request->Type == UserRequestType::READ) {
			switch (caching_mode_per_input_stream[user_request->Stream_id]) {
				case Caching_Mode::TURNED_OFF:
					static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					return;
				case Caching_Mode::WRITE_CACHE:
				case Caching_Mode::READ_CACHE:
				case Caching_Mode::WRITE_READ_CACHE:
				{
					std::list<NVM_Transaction*>::iterator it = user_request->Transaction_list.begin();
					while (it != user_request->Transaction_list.end()) {
						NVM_Transaction_Flash_RD* tr = (NVM_Transaction_Flash_RD*)(*it);
						if (per_stream_cache[tr->Stream_id]->Exists(tr->Stream_id, tr->LPA)) {
							page_status_type available_sectors_bitmap = per_stream_cache[tr->Stream_id]->Get_slot(tr->Stream_id, tr->LPA).State_bitmap_of_existing_sectors & tr->read_sectors_bitmap;
							if (available_sectors_bitmap == tr->read_sectors_bitmap) {
								user_request->Sectors_serviced_from_cache += count_sector_no_from_status_bitmap(tr->read_sectors_bitmap);
								user_request->Transaction_list.erase(it++);//the ++ operation should happen here, otherwise the iterator will be part of the list after erasing it from the list
							} else if (available_sectors_bitmap != 0) {
								user_request->Sectors_serviced_from_cache += count_sector_no_from_status_bitmap(available_sectors_bitmap);
								tr->read_sectors_bitmap = (tr->read_sectors_bitmap & ~available_sectors_bitmap);
								tr->Data_and_metadata_size_in_byte -= count_sector_no_from_status_bitmap(available_sectors_bitmap) * SECTOR_SIZE_IN_BYTE;
								it++;
							} else {
								it++;
							}
						} else {
							it++;
						}
					}

					if (user_request->Sectors_serviced_from_cache > 0) {
						Memory_Transfer_Info* transfer_info = new Memory_Transfer_Info;
						transfer_info->Size_in_bytes = user_request->Sectors_serviced_from_cache * SECTOR_SIZE_IN_BYTE;
						transfer_info->Related_request = user_request;
						transfer_info->next_event_type = Data_Cache_Simulation_Event_Type::MEMORY_READ_FOR_USERIO_FINISHED;
						transfer_info->Stream_id = user_request->Stream_id;
						service_dram_access_request(transfer_info);
					}
					if (user_request->Transaction_list.size() > 0) {
						static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					}

					return;
				}
			}
		}
		else//This is a write request
		{
			switch (caching_mode_per_input_stream[user_request->Stream_id])
			{
				case Caching_Mode::TURNED_OFF:
				case Caching_Mode::READ_CACHE:
					static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					return;
				case Caching_Mode::WRITE_CACHE://The data cache manger unit performs like a destage buffer
				case Caching_Mode::WRITE_READ_CACHE:
				{
					write_to_destage_buffer(user_request);

					int queue_id = user_request->Stream_id;
					if (shared_dram_request_queue) {
						queue_id = 0;
					}
					if (user_request->Transaction_list.size() > 0) {
						waiting_user_requests_queue_for_dram_free_slot[queue_id].push_back(user_request);
					}
					return;
				}
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpq $0x0, 0x50(%rsi)
je 0x3411b
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x40(%rsi), %r15
cmpl $0x0, 0x64(%rsi)
leaq 0x3b8e6(%rip), %rax     # 0x6f628
movq (%rax), %rax
movzwl 0x68(%rsi), %ecx
movl (%rax,%rcx,4), %eax
je 0x33dc2
cmpl $0x3, %eax
ja 0x3411b
movl %eax, %eax
leaq 0x25448(%rip), %rcx     # 0x591a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
movq %rbx, %rsi
callq 0x343c0
cmpq $0x0, 0x50(%rbx)
je 0x3411b
movzwl 0x68(%rbx), %eax
movq 0xd8(%r14), %rcx
xorl %edx, %edx
cmpb $0x0, 0xe0(%r14)
cmoveq %rax, %rdx
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %r14
movl $0x18, %edi
callq 0x73a0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
incq 0x10(%r14)
jmp 0x3411b
cmpl $0x3, %eax
jae 0x340f7
movq (%r15), %r12
cmpq %r15, %r12
je 0x340a9
leaq 0x8(%rsp), %r13
movq 0x10(%r12), %rbp
movq 0xc0(%r14), %rax
movzwl (%rbp), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%rbp), %rdx
callq 0x31d3e
testb %al, %al
je 0x3409c
movq 0xc0(%r14), %rax
movzwl (%rbp), %edx
movq (%rax,%rdx,8), %rsi
movq 0x58(%rbp), %rcx
movq %r13, %rdi
callq 0x31dfc
movq 0x90(%rbp), %rax
movq 0x8(%rsp), %rcx
andq %rax, %rcx
cmpq %rax, %rcx
jne 0x33f1c
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm1     # xmm1 = xmm0[0,1,0,1]
pxor %xmm0, %xmm0
movl $0x40, %eax
movdqa 0x246b1(%rip), %xmm2    # 0x58500
movdqa 0x24699(%rip), %xmm3    # 0x584f0
movdqa 0x246b0(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x246ad(%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 0x33e73
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 %eax, 0x58(%rbx)
movq (%r12), %rbp
decq 0x50(%rbx)
movq %r12, %rdi
callq 0x7580
movq %r12, %rdi
callq 0x7370
movq %rbp, %r12
jmp 0x340a0
testq %rcx, %rcx
movdqa 0x245e8(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x245e5(%rip), %xmm11   # 0x58520
je 0x3409c
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm1, %xmm1
movl $0x40, %edx
movdqa 0x245a4(%rip), %xmm2    # 0x58500
movdqa 0x2458c(%rip), %xmm3    # 0x584f0
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 %xmm0, %xmm7
andpd %xmm0, %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, %xmm1
psubd %xmm10, %xmm1
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rdx
jne 0x33f64
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %edx
addl %edx, 0x58(%rbx)
xorq %rax, %rcx
movq %rcx, 0x90(%rbp)
pxor %xmm1, %xmm1
movl $0x40, %eax
movdqa 0x244f8(%rip), %xmm2    # 0x58500
movdqa 0x244e0(%rip), %xmm3    # 0x584f0
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 %xmm0, %xmm7
andpd %xmm0, %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, %xmm1
psubd %xmm10, %xmm1
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rax
jne 0x34010
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, %eax
shll $0x9, %eax
subl %eax, 0x50(%rbp)
movq (%r12), %r12
cmpq %r15, %r12
jne 0x33ddc
movl 0x58(%rbx), %ebp
testl %ebp, %ebp
je 0x340dd
movl $0x18, %edi
callq 0x73a0
shll $0x9, %ebp
movl %ebp, (%rax)
movq %rbx, 0x8(%rax)
movl $0x2, 0x10(%rax)
movzwl 0x68(%rbx), %ecx
movw %cx, 0x14(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x3412a
cmpq $0x0, 0x50(%rbx)
je 0x3411b
movq 0x38(%r14), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x60(%rax)
jmp 0x3411b
jne 0x3411b
movq 0x38(%r14), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq 0x60(%rax), %rax
movq %r15, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq