// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_SATA.cpp
// Function: SSD_Components::Input_Stream_Manager_SATA::segment_user_request(SSD_Components::User_Request*)
void Input_Stream_Manager_SATA::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_SATA*)input_streams[SATA_STREAM_ID])->Start_logical_sector_address || lsa >((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->End_logical_sector_address) {
				lsa = ((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Start_logical_sector_address
				+ (lsa % (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->End_logical_sector_address - (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Start_logical_sector_address)));
			}
			LHA_type internal_lsa = lsa - ((Input_Stream_SATA*)input_streams[SATA_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, SATA_STREAM_ID,
					transaction_size * SECTOR_SIZE_IN_BYTE, lpa, NO_PPA, user_request, 0, access_status_bitmap, CurrentTimeStamp);
				user_request->Transaction_list.push_back(transaction);
				input_streams[SATA_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, SATA_STREAM_ID,
					transaction_size * SECTOR_SIZE_IN_BYTE, lpa, user_request, 0, access_status_bitmap, CurrentTimeStamp);
				user_request->Transaction_list.push_back(transaction);
				input_streams[SATA_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 $0x38, %rsp
movq 0x28(%rsi), %r12
movl 0x60(%rsi), %r8d
movq %rsi, 0x8(%rsp)
leaq 0x40(%rsi), %rax
movq %rax, 0x20(%rsp)
xorl %r9d, %r9d
movq %rdi, 0x28(%rsp)
movl %r8d, 0x1c(%rsp)
movl %r8d, %ebp
subl %r9d, %ebp
jbe 0x36442
movq 0x10(%rdi), %rax
movq (%rax), %rax
movq 0x48(%rax), %rcx
movq 0x50(%rax), %rsi
cmpq %rcx, %r12
jb 0x36306
cmpq %rsi, %r12
jbe 0x36317
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(%rdi), %rax
movl 0x48(%rax), %edi
movq %r12, %rax
xorl %edx, %edx
divq %rdi
movl %edi, %eax
subl %edx, %eax
movq %r9, 0x30(%rsp)
leal (%rax,%r9), %ecx
cmpl %r8d, %ecx
cmovbl %eax, %ebp
pushq $-0x1
popq %r14
movl %ebp, %ecx
shlq %cl, %r14
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movq %rax, %r15
movq %rdx, %rcx
notq %r14
shlq %cl, %r14
movl %ebp, %ebx
shll $0x9, %ebx
movq 0x8(%rsp), %rax
cmpl $0x0, 0x64(%rax)
je 0x363ce
movl $0xb0, %edi
callq 0x7370
movq %rax, %r13
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
subq $0x8, %rsp
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl %ebx, %ecx
movq %r15, %r8
movq 0x10(%rsp), %r9
pushq %rax
pushq %r14
pushq $0x0
callq 0x3a36c
addq $0x20, %rsp
movq %r13, 0x10(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2ab40
movq 0x28(%rsp), %rdi
movq 0x10(%rdi), %rax
movq (%rax), %rax
incl 0x14(%rax)
jmp 0x3642d
movl $0xa0, %edi
callq 0x7370
movq %rax, %r13
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl %ebx, %ecx
movq %r15, %r8
pushq $-0x1
popq %r9
pushq %rax
pushq %r14
pushq $0x0
pushq 0x20(%rsp)
callq 0x3a21a
addq $0x20, %rsp
movq %r13, 0x10(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2ab40
movq 0x28(%rsp), %rdi
movq 0x10(%rdi), %rax
movq (%rax), %rax
incl 0x10(%rax)
addq %rbp, %r12
movq 0x30(%rsp), %r9
addl %ebp, %r9d
movl 0x1c(%rsp), %r8d
jmp 0x362e1
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x36453
movq %rax, %rbx
movq %r13, %rdi
callq 0x7340
movq %rbx, %rdi
callq 0x75d0