// File: /workspace/github_cpp_makefile_star2_O3/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 0x40f7f
movq %rsi, %rbx
movq %rdi, %r15
movq 0x28(%rsi), %r12
leaq 0x40(%rsi), %rax
movq %rax, 0x18(%rsp)
movzwl 0x68(%rsi), %eax
movq 0x10(%rdi), %rcx
xorl %r9d, %r9d
movq %rdi, 0x30(%rsp)
movl %r8d, 0x14(%rsp)
movzwl %ax, %eax
movq %rax, 0x28(%rsp)
movq (%rcx,%rax,8), %rax
movq 0x50(%rax), %rcx
movq 0x58(%rax), %rsi
cmpq %rcx, %r12
jb 0x40de5
cmpq %rsi, %r12
jbe 0x40df6
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(%r15), %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, %r14
movl %r13d, %ecx
shlq %cl, %r14
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movq %rax, 0x20(%rsp)
movq %rdx, %rcx
notq %r14
shlq %cl, %r14
movl %r13d, %ebp
shll $0x9, %ebp
cmpl $0x0, 0x64(%rbx)
je 0x40edf
movl $0xb0, %edi
callq 0x73a0
movq %rax, 0x8(%rsp)
movq %rbx, %r15
movl (%rbx), %ebx
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
movl %ebp, %ecx
movq 0x20(%rsp), %r8
movq %r15, %r9
pushq %rax
pushq %r14
pushq $0x0
pushq %rbx
callq 0x48218
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 %r15, %rbx
incq 0x50(%r15)
movzwl 0x68(%r15), %eax
movq 0x30(%rsp), %r15
movq 0x10(%r15), %rcx
movq (%rcx,%rax,8), %rdx
incl 0x14(%rdx)
jmp 0x40f66
movl $0xa0, %edi
callq 0x73a0
movq %rax, 0x8(%rsp)
movl (%rbx), %eax
movq %rax, 0x38(%rsp)
callq 0x28018
movq %rax, %rdi
callq 0x2841a
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
movl %ebp, %ecx
movq 0x28(%rsp), %r8
movq $-0x1, %r9
pushq %rax
pushq %r14
pushq $0x0
pushq 0x58(%rsp)
pushq %rbx
callq 0x480bc
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(%rbx)
movzwl 0x68(%rbx), %eax
movq 0x10(%r15), %rcx
movq (%rcx,%rax,8), %rdx
incl 0x10(%rdx)
addq %r13, %r12
movq 0x40(%rsp), %r9
addl %r13d, %r9d
movl 0x14(%rsp), %r8d
cmpl %r8d, %r9d
jb 0x40dc7
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40f90
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x7370
movq %rbx, %rdi
callq 0x7620
nop