// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_NVMe.cpp
// Function: SSD_Components::Input_Stream_Manager_NVMe::segment_user_request(SSD_Components::User_Request*)
void Input_Stream_Manager_NVMe::segment_user_request(User_Request *user_request)
{
	LHA_type lsa = user_request->Start_LBA;
	LHA_type lsa2 = user_request->Start_LBA;
	unsigned int req_size = user_request->SizeInSectors;

	page_status_type access_status_bitmap = 0;
	unsigned int handled_sectors_count = 0;
	unsigned int transaction_size = 0;
	while (handled_sectors_count < req_size)
	{
		//Check if LSA is in the correct range allocted to the stream
		if (lsa < ((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->Start_logical_sector_address || lsa > ((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->End_logical_sector_address)
		{
			lsa = ((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->Start_logical_sector_address + (lsa % (((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->End_logical_sector_address - (((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->Start_logical_sector_address)));
		}
		LHA_type internal_lsa = lsa - ((Input_Stream_NVMe *)input_streams[user_request->Stream_id])->Start_logical_sector_address; //For each flow, all lsa's should be translated into a range starting from zero

		transaction_size = host_interface->sectors_per_page - (unsigned int)(lsa % host_interface->sectors_per_page);
		if (handled_sectors_count + transaction_size >= req_size)
		{
			transaction_size = req_size - handled_sectors_count;
		}
		LPA_type lpa = internal_lsa / host_interface->sectors_per_page;

		page_status_type temp = ~(0xffffffffffffffff << (int)transaction_size);
		access_status_bitmap = temp << (int)(internal_lsa % host_interface->sectors_per_page);

		if (user_request->Type == UserRequestType::READ)
		{
			NVM_Transaction_Flash_RD *transaction = new NVM_Transaction_Flash_RD(Transaction_Source_Type::USERIO, user_request->Stream_id,
																				 transaction_size * SECTOR_SIZE_IN_BYTE, lpa, NO_PPA, user_request, user_request->Priority_class, 0, access_status_bitmap, CurrentTimeStamp);
			user_request->Transaction_list.push_back(transaction);
			input_streams[user_request->Stream_id]->STAT_number_of_read_transactions++;
		}
		else
		{ //user_request->Type == UserRequestType::WRITE
			NVM_Transaction_Flash_WR *transaction = new NVM_Transaction_Flash_WR(Transaction_Source_Type::USERIO, user_request->Stream_id,
																				 transaction_size * SECTOR_SIZE_IN_BYTE, lpa, user_request, user_request->Priority_class, 0, access_status_bitmap, CurrentTimeStamp);
			user_request->Transaction_list.push_back(transaction);
			input_streams[user_request->Stream_id]->STAT_number_of_write_transactions++;
		}

		lsa = lsa + transaction_size;
		handled_sectors_count += transaction_size;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x60(%rsi), %r8d
testl %r8d, %r8d
je 0x419c8
movq %rsi, %r15
movq %rdi, %rbp
movq 0x28(%rsi), %r12
leaq 0x40(%rsi), %rax
movq %rax, 0x18(%rsp)
xorl %r9d, %r9d
movq %rdi, 0x30(%rsp)
movl %r8d, 0x14(%rsp)
movzwl 0x68(%r15), %ecx
movq 0x10(%rbp), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rcx
cmpq %rcx, %r12
jb 0x41824
cmpq 0x58(%rax), %r12
jbe 0x41839
movq 0x58(%rax), %rsi
subq %rcx, %rsi
movq %r12, %rax
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
addq %rcx, %r12
movq %r12, %rsi
subq %rcx, %rsi
movq 0x8(%rbp), %rax
movl 0x48(%rax), %edi
movq %r12, %rax
xorl %edx, %edx
divq %rdi
movl %edi, %eax
subl %edx, %eax
leal (%rax,%r9), %ecx
movl %r8d, %r13d
movq %r9, 0x40(%rsp)
subl %r9d, %r13d
cmpl %r8d, %ecx
cmovbl %eax, %r13d
movq $-0x1, %rbx
movl %r13d, %ecx
shlq %cl, %rbx
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movq %rax, 0x20(%rsp)
movq %rdx, %rcx
notq %rbx
shlq %cl, %rbx
movl %r13d, %r14d
shll $0x9, %r14d
cmpl $0x0, 0x64(%r15)
je 0x41925
movl $0xb0, %edi
callq 0x73a0
movq %rax, 0x8(%rsp)
movq %r15, %rbp
movl (%r15), %r15d
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movzwl 0x28(%rsp), %edx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl %r14d, %ecx
movq 0x20(%rsp), %r8
movq %rbp, %r9
pushq %rax
pushq %rbx
pushq $0x0
pushq %r15
callq 0x48c74
addq $0x20, %rsp
movl $0x18, %edi
callq 0x73a0
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x7060
movq %rbp, %r15
incq 0x50(%rbp)
movzwl 0x68(%rbp), %eax
movq 0x30(%rsp), %rbp
movq 0x10(%rbp), %rcx
movq (%rcx,%rax,8), %rax
incl 0x14(%rax)
jmp 0x419af
movl $0xa0, %edi
callq 0x73a0
movq %rax, 0x8(%rsp)
movl (%r15), %eax
movq %rax, 0x38(%rsp)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
subq $0x8, %rsp
movzwl 0x30(%rsp), %edx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movl %r14d, %ecx
movq 0x28(%rsp), %r8
movq $-0x1, %r9
pushq %rax
pushq %rbx
pushq $0x0
pushq 0x58(%rsp)
pushq %r15
callq 0x48b18
addq $0x30, %rsp
movl $0x18, %edi
callq 0x73a0
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x7060
incq 0x50(%r15)
movzwl 0x68(%r15), %eax
movq 0x10(%rbp), %rcx
movq (%rcx,%rax,8), %rax
incl 0x10(%rax)
addq %r13, %r12
movq 0x40(%rsp), %r9
addl %r13d, %r9d
movl 0x14(%rsp), %r8d
cmpl %r8d, %r9d
jb 0x41803
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x419d9
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x7370
movq %rbx, %rdi
callq 0x7620