// File: CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_SATA.cpp
// Function: SSD_Components::Input_Stream_Manager_SATA::Handle_serviced_request(SSD_Components::User_Request*)
inline void Input_Stream_Manager_SATA::Handle_serviced_request(User_Request* request)
	{
		((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Waiting_user_requests.remove(request);
		((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->On_the_fly_requests--;

		DEBUG("** Host Interface: Request #" << request->ID << " is finished")

		//If this is a read request, then the read data should be written to host memory
		if (request->Type == UserRequestType::READ) {
			((Host_Interface_SATA*)host_interface)->request_fetch_unit->Send_read_data(request);
		}

		if (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Submission_head != ((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Submission_tail) {
			((Host_Interface_SATA*)host_interface)->request_fetch_unit->Fetch_next_request(SATA_STREAM_ID);
			((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->On_the_fly_requests++;
			((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Submission_head++;//Update submission queue head after starting fetch request
			if (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Submission_head == ncq_depth) {//Circular queue implementation
				((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Submission_head = 0;
			}
		}

		//Check if completion queue is full
		if (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_head > ((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_tail) {
			//completion queue is full
			if (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_tail + 1 == ((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_head) {
				((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completed_user_requests.push_back(request);//Wait while the completion queue is full
				return;
			}
		} else if (((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_tail - ((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completion_head
			== ncq_depth - 1) {
			((Input_Stream_SATA*)input_streams[SATA_STREAM_ID])->Completed_user_requests.push_back(request);//Wait while the completion queue is full
			return;
		}

		inform_host_request_completed(request);//Completion queue is not full, so the device can DMA the completion queue entry to the host
		DELETE_REQUEST_NVME(request);
	}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movq %rdi, 0x30(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rdi
addq $0x78, %rdi
leaq 0x38(%rsp), %rsi
callq 0x709e0
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rax
movw 0xc0(%rax), %cx
addw $-0x1, %cx
movw %cx, 0xc0(%rax)
movq 0x38(%rsp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x8b750
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x58(%rax), %rdi
movq 0x38(%rsp), %rsi
movq (%rdi), %rax
callq *0x20(%rax)
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movzwl 0x68(%rax), %eax
movl %eax, 0x2c(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movq (%rcx), %rcx
movzwl 0x6c(%rcx), %ecx
cmpl %ecx, %eax
je 0x8b835
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x58(%rax), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x10(%rax)
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movw 0xc0(%rax), %cx
addw $0x1, %cx
movw %cx, 0xc0(%rax)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movw 0x68(%rax), %cx
addw $0x1, %cx
movw %cx, 0x68(%rax)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rcx
movq (%rax), %rax
movzwl 0x68(%rax), %eax
movzwl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jne 0x8b833
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rax
movw $0x0, 0x68(%rax)
jmp 0x8b835
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movzwl 0x6e(%rax), %eax
movl %eax, 0x28(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq %rax, %rcx
movl 0x28(%rsp), %eax
movq (%rcx), %rcx
movzwl 0x70(%rcx), %ecx
cmpl %ecx, %eax
jle 0x8b8e7
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movzwl 0x70(%rax), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq %rax, %rcx
movl 0x24(%rsp), %eax
movq (%rcx), %rcx
movzwl 0x6e(%rcx), %ecx
cmpl %ecx, %eax
jne 0x8b8e5
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rdi
addq $0x90, %rdi
leaq 0x38(%rsp), %rsi
callq 0x6c210
jmp 0x8ba54
jmp 0x8b963
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movzwl 0x70(%rax), %eax
movl %eax, 0x20(%rsp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq 0x30(%rsp), %rcx
movq %rax, %rdx
movl 0x20(%rsp), %eax
movq (%rdx), %rdx
movzwl 0x6e(%rdx), %edx
subl %edx, %eax
movzwl 0x28(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8b961
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x85300
movq (%rax), %rdi
addq $0x90, %rdi
leaq 0x38(%rsp), %rsi
callq 0x6c210
jmp 0x8ba54
jmp 0x8b963
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x8bae0
movq 0x38(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x8b990
movq 0x18(%rsp), %rdi
callq 0x8410
callq 0x46500
movq %rax, %rdi
callq 0x46d20
testb $0x1, %al
jne 0x8b9a3
jmp 0x8b9d1
movq 0x38(%rsp), %rax
cmpq $0x0, 0x78(%rax)
je 0x8b9cf
movq 0x38(%rsp), %rax
movq 0x78(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x8b9cd
movq 0x10(%rsp), %rdi
callq 0x8570
jmp 0x8b9cf
jmp 0x8b9d1
movq 0x38(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
je 0x8ba30
movq 0x4e604(%rip), %rdi     # 0xd9ff0
leaq 0x1eef5(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x4e5f1(%rip), %rdi     # 0xd9ff0
leaq 0x22a10(%rip), %rsi     # 0xae416
callq 0x8440
movq %rax, %rdi
movq 0x4e543(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x4e5af(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x8ba54
movq 0x8(%rsp), %rdi
callq 0x6c070
movq 0x8(%rsp), %rdi
callq 0x8410
addq $0x48, %rsp
retq
nopl (%rax)