// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/host/IO_Flow_Base.cpp
// Function: Host_Components::IO_Flow_Base::NVMe_consume_io_request(Completion_Queue_Entry*)
void IO_Flow_Base::NVMe_consume_io_request(Completion_Queue_Entry* cqe)
	{
		//Find the request and update statistics
		Host_IO_Request* request = nvme_software_request_queue[cqe->Command_Identifier];
		nvme_software_request_queue.erase(cqe->Command_Identifier);
		available_command_ids.insert(cqe->Command_Identifier);
		sim_time_type device_response_time = Simulator->Time() - request->Enqueue_time;
		sim_time_type request_delay = Simulator->Time() - request->Arrival_time;
		STAT_serviced_request_count++;
		STAT_serviced_request_count_short_term++;

		STAT_sum_device_response_time += device_response_time;
		STAT_sum_device_response_time_short_term += device_response_time;
		STAT_sum_request_delay += request_delay;
		STAT_sum_request_delay_short_term += request_delay;
		if (device_response_time > STAT_max_device_response_time) {
			STAT_max_device_response_time = device_response_time;
		}
		if (device_response_time < STAT_min_device_response_time) {
			STAT_min_device_response_time = device_response_time;
		}
		if (request_delay > STAT_max_request_delay) {
			STAT_max_request_delay = request_delay;
		}
		if (request_delay < STAT_min_request_delay) {
			STAT_min_request_delay = request_delay;
		}
		STAT_transferred_bytes_total += request->LBA_count * SECTOR_SIZE_IN_BYTE;
		
		if (request->Type == Host_IO_Request_Type::READ) {
			STAT_serviced_read_request_count++;
			STAT_sum_device_response_time_read += device_response_time;
			STAT_sum_request_delay_read += request_delay;
			if (device_response_time > STAT_max_device_response_time_read) {
				STAT_max_device_response_time_read = device_response_time;
			}
			if (device_response_time < STAT_min_device_response_time_read) {
				STAT_min_device_response_time_read = device_response_time;
			}
			if (request_delay > STAT_max_request_delay_read) {
				STAT_max_request_delay_read = request_delay;
			}
			if (request_delay < STAT_min_request_delay_read) {
				STAT_min_request_delay_read = request_delay;
			}
			STAT_transferred_bytes_read += request->LBA_count * SECTOR_SIZE_IN_BYTE;
		} else {
			STAT_serviced_write_request_count++;
			STAT_sum_device_response_time_write += device_response_time;
			STAT_sum_request_delay_write += request_delay;
			if (device_response_time > STAT_max_device_response_time_write) {
				STAT_max_device_response_time_write = device_response_time;
			}
			if (device_response_time < STAT_min_device_response_time_write) {
				STAT_min_device_response_time_write = device_response_time;
			}
			if (request_delay > STAT_max_request_delay_write) {
				STAT_max_request_delay_write = request_delay;
			}
			if (request_delay < STAT_min_request_delay_write) {
				STAT_min_request_delay_write = request_delay;
			}
			STAT_transferred_bytes_write += request->LBA_count * SECTOR_SIZE_IN_BYTE;
		}

		delete request;

		nvme_queue_pair.Submission_queue_head = cqe->SQ_Head;
		
		//MQSim always assumes that the request is processed correctly, so no need to check cqe->SF_P

		//If the submission queue is not full anymore, then enqueue waiting requests
		while(waiting_requests.size() > 0) {
			if (!NVME_SQ_FULL(nvme_queue_pair) && available_command_ids.size() > 0) {
				Host_IO_Request* new_req = waiting_requests.front();
				waiting_requests.pop_front();
				if (nvme_software_request_queue[*available_command_ids.begin()] != NULL) {
					PRINT_ERROR("Unexpteced situation in IO_Flow_Base! Overwriting a waiting I/O request in the queue!")
				} else {
					new_req->IO_queue_info = *available_command_ids.begin();
					nvme_software_request_queue[*available_command_ids.begin()] = new_req;
					available_command_ids.erase(available_command_ids.begin());
					request_queue_in_memory[nvme_queue_pair.Submission_queue_tail] = new_req;
					NVME_UPDATE_SQ_TAIL(nvme_queue_pair);
				}
				new_req->Enqueue_time = Simulator->Time();
				pcie_root_complex->Write_to_device(nvme_queue_pair.Submission_tail_register_address_on_device, nvme_queue_pair.Submission_queue_tail);//Based on NVMe protocol definition, the updated tail pointer should be informed to the device
			} else {
				break;
			}
		}

		delete cqe;

		//Announce simulation progress
		if (stop_time > 0) {
			progress = int(Simulator->Time() / (double)stop_time * 100);
		} else {
			progress = int(STAT_serviced_request_count / (double)total_requests_to_be_generated * 100);
		}

		if (progress >= next_progress_step) {
			std::string progress_bar;
			int barWidth = 100;
			progress_bar += "[";
			int pos = progress;
			for (int i = 0; i < barWidth; i += 5) {
				if (i < pos) {
					progress_bar += "=";
				} else if (i == pos) {
					progress_bar += ">";
				} else {
					progress_bar += " ";
				}
			}
			progress_bar += "] ";
			PRINT_MESSAGE(progress_bar << " " << progress << "% progress in " << ID() << std::endl)
			next_progress_step += 5;
		}

		if (Simulator->Time() > next_logging_milestone) {
			log_file << Simulator->Time() / SIM_TIME_TO_MICROSECONDS_COEFF << "\t" << Get_device_response_time_short_term() << "\t" << Get_end_to_end_request_delay_short_term() << std::endl;
			STAT_sum_device_response_time_short_term = 0;
			STAT_sum_request_delay_short_term = 0;
			STAT_serviced_request_count_short_term = 0;
			next_logging_milestone = Simulator->Time() + logging_period;
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x118(%rdi), %r15
leaq 0xc(%rsi), %rbp
movzwl 0xc(%rsi), %eax
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x19d20
movq (%rax), %r13
movq %r14, 0x8(%rsp)
movzwl 0xc(%r14), %eax
movq %rax, (%r12)
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1a094
leaq 0xb8(%rbx), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x19a84
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %rax, %rbp
subq 0x8(%r13), %rbp
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
subq (%r13), %rax
incl 0x160(%rbx)
incl 0x468(%rbx)
addq %rbp, 0x170(%rbx)
addq %rbp, 0x458(%rbx)
addq %rax, 0x1b8(%rbx)
addq %rax, 0x460(%rbx)
cmpq 0x1a0(%rbx), %rbp
jbe 0x17f98
movq %rbp, 0x1a0(%rbx)
cmpq 0x188(%rbx), %rbp
jae 0x17fa8
movq %rbp, 0x188(%rbx)
cmpq 0x1e8(%rbx), %rax
jbe 0x17fb8
movq %rax, 0x1e8(%rbx)
cmpq 0x1d0(%rbx), %rax
jae 0x17fc8
movq %rax, 0x1d0(%rbx)
movl 0x18(%r13), %ecx
shll $0x9, %ecx
addq %rcx, 0x200(%rbx)
cmpl $0x0, 0x1c(%r13)
je 0x18038
incl 0x168(%rbx)
addq %rbp, 0x180(%rbx)
addq %rax, 0x1c8(%rbx)
cmpq 0x1b0(%rbx), %rbp
jbe 0x18001
movq %rbp, 0x1b0(%rbx)
cmpq 0x198(%rbx), %rbp
jae 0x18011
movq %rbp, 0x198(%rbx)
cmpq 0x1f8(%rbx), %rax
jbe 0x18021
movq %rax, 0x1f8(%rbx)
movl $0x210, %edx            # imm = 0x210
cmpq 0x1e0(%rbx), %rax
jae 0x18094
leaq 0x1e0(%rbx), %rsi
jmp 0x18091
incl 0x164(%rbx)
addq %rbp, 0x178(%rbx)
addq %rax, 0x1c0(%rbx)
cmpq 0x1a8(%rbx), %rbp
jbe 0x1805c
movq %rbp, 0x1a8(%rbx)
cmpq 0x190(%rbx), %rbp
jae 0x1806c
movq %rbp, 0x190(%rbx)
cmpq 0x1f0(%rbx), %rax
jbe 0x1807c
movq %rax, 0x1f0(%rbx)
movl $0x208, %edx            # imm = 0x208
cmpq 0x1d8(%rbx), %rax
jae 0x18094
leaq 0x1d8(%rbx), %rsi
movq %rax, (%rsi)
leaq 0x458(%rbx), %rax
movq %rax, 0x30(%rsp)
addq %rcx, (%rbx,%rdx)
movq %r13, %rdi
callq 0x7340
movq 0x8(%rsp), %rax
movzwl 0x8(%rax), %eax
movw %ax, 0x80(%rbx)
leaq 0x100(%rbx), %r13
leaq 0x10(%rsp), %rbp
cmpq $0x0, 0x110(%rbx)
je 0x181d9
movzwl 0x82(%rbx), %eax
movzwl 0x84(%rbx), %ecx
decl %ecx
cmpl %eax, %ecx
jle 0x180fc
incl %eax
movzwl 0x80(%rbx), %ecx
cmpl %ecx, %eax
jne 0x1810a
jmp 0x181d9
cmpw $0x0, 0x80(%rbx)
je 0x181d9
cmpq $0x0, 0xe0(%rbx)
je 0x181d9
movq 0x100(%rbx), %rax
movq 0x10(%rax), %r14
movq %r13, %rdi
callq 0x19970
movq 0xd0(%rbx), %rax
movzwl 0x20(%rax), %eax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x19d20
cmpq $0x0, (%rax)
jne 0x18475
movq 0xd0(%rbx), %rax
movzwl 0x20(%rax), %eax
movw %ax, 0x20(%r14)
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x19d20
movq %r14, (%rax)
movq 0xd0(%rbx), %rsi
movq %r12, %rdi
callq 0x1a1ae
movzwl 0x82(%rbx), %eax
movq 0xe8(%rbx), %rcx
movq %r14, (%rcx,%rax,8)
incl %eax
cmpw 0x84(%rbx), %ax
movl $0x0, %ecx
cmovel %ecx, %eax
movw %ax, 0x82(%rbx)
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %rax, 0x8(%r14)
movq 0x58(%rbx), %rdi
movq 0x88(%rbx), %rsi
movzwl 0x82(%rbx), %edx
callq 0x1ce34
jmp 0x180c8
movq 0x8(%rsp), %rdi
callq 0x7340
cmpq $0x0, 0x48(%rbx)
je 0x1823f
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %rax, %xmm1
movq 0x2d5cc(%rip), %xmm2    # 0x457d0
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x2d5d0(%rip), %xmm3    # 0x457e0
subpd %xmm3, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x48(%rbx), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm3, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
jmp 0x18256
movl 0x160(%rbx), %eax
cvtsi2sd %rax, %xmm0
movl 0x50(%rbx), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x2cbd2(%rip), %xmm0    # 0x44e30
cvttsd2si %xmm0, %eax
movl %eax, 0x218(%rbx)
cmpl %eax, 0x21c(%rbx)
jg 0x18372
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x2d672(%rip), %rsi     # 0x458fe
leaq 0x10(%rsp), %rdi
callq 0x7630
movl 0x218(%rbx), %ebp
xorl %r15d, %r15d
leaq 0x2da58(%rip), %r13     # 0x45cfe
leaq 0x2b38c(%rip), %r12     # 0x43639
leaq 0x10(%rsp), %r14
cmpl $0x64, %r15d
jae 0x182db
cmpl %r15d, %ebp
movq %r13, %rsi
leaq 0x2b305(%rip), %rax     # 0x435ca
cmoveq %rax, %rsi
cmovgq %r12, %rsi
movq %r14, %rdi
callq 0x7630
addl $0x5, %r15d
jmp 0x182b2
leaq 0x2d61e(%rip), %rsi     # 0x45900
leaq 0x10(%rsp), %rdi
callq 0x7630
movq 0x49c85(%rip), %rdi     # 0x61f78
leaq 0x10(%rsp), %rsi
callq 0x7310
leaq 0x2d9fa(%rip), %rsi     # 0x45cfe
movq %rax, %rdi
callq 0x7360
movl 0x218(%rbx), %esi
movq %rax, %rdi
callq 0x75a0
leaq 0x2d5e2(%rip), %rsi     # 0x45903
movq %rax, %rdi
callq 0x7360
movq %rax, %r14
leaq 0x8(%rbx), %rsi
leaq 0x38(%rsp), %rdi
callq 0x7480
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x7310
movq %rax, %rdi
callq 0x71a0
movq %rax, %rdi
callq 0x71a0
leaq 0x38(%rsp), %rdi
callq 0x7678
addl $0x5, 0x21c(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x7678
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
cmpq 0x230(%rbx), %rax
jbe 0x18466
leaq 0x258(%rbx), %r15
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
xorl %r14d, %r14d
movl $0x3e8, %r12d           # imm = 0x3E8
xorl %edx, %edx
divq %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x7270
leaq 0x2d52a(%rip), %rsi     # 0x458ea
movq %rax, %rdi
callq 0x7360
movq %rax, %rdi
movl 0x468(%rbx), %ecx
testq %rcx, %rcx
movq 0x30(%rsp), %r15
je 0x183eb
movq (%r15), %rax
xorl %edx, %edx
divq %rcx
xorl %edx, %edx
divq %r12
movl %eax, %r14d
movq %r14, %rsi
callq 0x7270
leaq 0x2d4f0(%rip), %rsi     # 0x458ea
movq %rax, %rdi
callq 0x7360
movq %rax, %rdi
cmpl $0x0, 0x160(%rbx)
je 0x1842e
movq 0x460(%rbx), %rax
movl 0x468(%rbx), %ecx
xorl %edx, %edx
divq %rcx
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movl %eax, %esi
jmp 0x18430
xorl %esi, %esi
callq 0x7270
movq %rax, %rdi
callq 0x71a0
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
andl $0x0, 0x10(%r15)
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
addq 0x228(%rbx), %rax
movq %rax, 0x230(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x49b74(%rip), %rbx     # 0x61ff0
leaq 0x2c105(%rip), %rsi     # 0x44588
movq %rbx, %rdi
callq 0x7360
leaq 0x2d480(%rip), %rsi     # 0x45912
movq %rbx, %rdi
callq 0x7360
movq %rax, %rdi
callq 0x71a0
movq 0x49b27(%rip), %rdi     # 0x61fd0
callq 0x73d0
pushq $0x1
popq %rdi
callq 0x73e0
jmp 0x184cb
jmp 0x184cb
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7678
jmp 0x184ce
jmp 0x184cb
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7678
movq %rbx, %rdi
callq 0x75d0