// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x148(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x140(%rsp)
movq 0x148(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x138(%rsp)
movq 0x148(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x134(%rsp)
movq $0x0, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movl $0x0, 0x120(%rsp)
movl 0x124(%rsp), %eax
cmpl 0x134(%rsp), %eax
jae 0x89cc4
movq 0xd0(%rsp), %rdi
movq 0x140(%rsp), %rax
movq %rax, 0xc8(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq %rax, %rcx
movq 0xc8(%rsp), %rax
movq (%rcx), %rcx
cmpq 0x48(%rcx), %rax
jb 0x8986d
movq 0xd0(%rsp), %rdi
movq 0x140(%rsp), %rax
movq %rax, 0xc0(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq %rax, %rcx
movq 0xc0(%rsp), %rax
movq (%rcx), %rcx
cmpq 0x50(%rcx), %rax
jbe 0x8990c
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0xd0(%rsp), %rdi
movq (%rax), %rax
movq 0x48(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0xb0(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0xd0(%rsp), %rdi
movq (%rax), %rax
movq 0x50(%rax), %rax
movq %rax, 0xa8(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0xa8(%rsp), %rcx
movq %rax, %rdx
movq 0xb0(%rsp), %rax
movq (%rdx), %rdx
subq 0x48(%rdx), %rcx
xorl %edx, %edx
divq %rcx
movq 0xb8(%rsp), %rax
addq %rdx, %rax
movq %rax, 0x140(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x140(%rsp), %rax
movq %rax, 0x98(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0xd0(%rsp), %rcx
movq %rax, %rdx
movq 0x98(%rsp), %rax
movq (%rdx), %rdx
subq 0x48(%rdx), %rax
movq %rax, 0x118(%rsp)
movq 0x8(%rcx), %rax
movl 0x48(%rax), %eax
movl %eax, 0xa4(%rsp)
movq 0x140(%rsp), %rax
movq 0x8(%rcx), %rcx
movl 0x48(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movl 0xa4(%rsp), %eax
movl %edx, %ecx
subl %ecx, %eax
movl %eax, 0x120(%rsp)
movl 0x124(%rsp), %eax
addl 0x120(%rsp), %eax
cmpl 0x134(%rsp), %eax
jb 0x899b3
movl 0x134(%rsp), %eax
subl 0x124(%rsp), %eax
movl %eax, 0x120(%rsp)
movq 0xd0(%rsp), %rcx
movq 0x118(%rsp), %rax
movq 0x8(%rcx), %rcx
movl 0x48(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, 0x110(%rsp)
movl 0x120(%rsp), %eax
movl %eax, %ecx
movq $-0x1, %rax
shlq %cl, %rax
movq 0xd0(%rsp), %rcx
xorq $-0x1, %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x118(%rsp), %rax
movq 0x8(%rcx), %rcx
movl 0x48(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq 0x90(%rsp), %rax
movl %edx, %ecx
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, 0x128(%rsp)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x89b7c
movl $0xa0, %edi
callq 0x8450
movq %rax, 0x60(%rsp)
movl 0x120(%rsp), %eax
shll $0x9, %eax
movl %eax, 0x6c(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x80(%rsp)
callq 0x46500
movq %rax, 0x88(%rsp)
jmp 0x89aa1
movq 0x88(%rsp), %rdi
callq 0x46c50
movq %rax, 0x58(%rsp)
jmp 0x89ab5
movq 0x70(%rsp), %r8
movl 0x6c(%rsp), %ecx
movq 0x60(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rsi
movq 0x58(%rsp), %r9
movq %rsp, %rax
movq %r9, 0x18(%rax)
movq %rsi, 0x10(%rax)
movq %rdx, (%rax)
movq $0x0, 0x8(%rax)
xorl %edx, %edx
movq $-0x1, %r9
movl %edx, %esi
callq 0x96180
jmp 0x89afd
movq 0x60(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x148(%rsp), %rdi
addq $0x40, %rdi
movq 0x100(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xe8(%rsp), %rsi
callq 0x6c260
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x89c90
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
callq 0x8410
jmp 0x89ccc
movl $0xb0, %edi
callq 0x8450
movq %rax, 0x28(%rsp)
movl 0x120(%rsp), %eax
shll $0x9, %eax
movl %eax, 0x34(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x48(%rsp)
callq 0x46500
movq %rax, 0x50(%rsp)
jmp 0x89bcc
movq 0x50(%rsp), %rdi
callq 0x46c50
movq %rax, 0x20(%rsp)
jmp 0x89bdd
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r8
movl 0x34(%rsp), %ecx
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq %rsp, %rax
movq %rsi, 0x10(%rax)
movq %rdx, 0x8(%rax)
movq $0x0, (%rax)
xorl %edx, %edx
movl %edx, %esi
callq 0x964b0
jmp 0x89c17
movq 0x28(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x148(%rsp), %rdi
addq $0x40, %rdi
movq 0xe0(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xd8(%rsp), %rsi
callq 0x6c260
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
jmp 0x89c90
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
callq 0x8410
jmp 0x89ccc
movq 0x140(%rsp), %rax
movl 0x120(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x140(%rsp)
movl 0x120(%rsp), %eax
addl 0x124(%rsp), %eax
movl %eax, 0x124(%rsp)
jmp 0x897e3
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xf8(%rsp), %rdi
callq 0x8770
nopl (%rax)