// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]MQSim/src/host/IO_Flow_Base.cpp
// Function: Host_Components::IO_Flow_Base::NVMe_read_sqe(unsigned long)
Submission_Queue_Entry* IO_Flow_Base::NVMe_read_sqe(uint64_t address)
	{
		Submission_Queue_Entry* sqe = new Submission_Queue_Entry;
		Host_IO_Request* request = request_queue_in_memory[(uint16_t)((address - nvme_queue_pair.Submission_queue_memory_base_address) / sizeof(Submission_Queue_Entry))];
		
		if (request == NULL) {
			throw std::invalid_argument(this->ID() + ": Request to access a submission queue entry that does not exist.");
		}

		sqe->Command_Identifier = request->IO_queue_info;
		if (request->Type == Host_IO_Request_Type::READ) {
			sqe->Opcode = NVME_READ_OPCODE;
			sqe->Command_specific[0] = (uint32_t) request->Start_LBA;
			sqe->Command_specific[1] = (uint32_t)(request->Start_LBA >> 32);
			sqe->Command_specific[2] = ((uint32_t)((uint16_t)request->LBA_count)) & (uint32_t)(0x0000ffff);
			sqe->PRP_entry_1 = (DATA_MEMORY_REGION);//Dummy addresses, just to emulate data read/write access
			sqe->PRP_entry_2 = (DATA_MEMORY_REGION + 0x1000);//Dummy addresses
		} else {
			sqe->Opcode = NVME_WRITE_OPCODE;
			sqe->Command_specific[0] = (uint32_t)request->Start_LBA;
			sqe->Command_specific[1] = (uint32_t)(request->Start_LBA >> 32);
			sqe->Command_specific[2] = ((uint32_t)((uint16_t)request->LBA_count)) & (uint32_t)(0x0000ffff);
			sqe->PRP_entry_1 = (DATA_MEMORY_REGION);//Dummy addresses, just to emulate data read/write access
			sqe->PRP_entry_2 = (DATA_MEMORY_REGION + 0x1000);//Dummy addresses
		}

		return sqe;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl $0x48, %edi
callq 0x73a0
movq %rax, %rcx
subq 0x90(%r14), %rbx
movabsq $-0x1c71c71c71c71c71, %rdx # imm = 0xE38E38E38E38E38F
movq %rbx, %rax
mulq %rdx
movq 0xe8(%r14), %rax
shrl $0x3, %edx
andl $0x7fff8, %edx          # imm = 0x7FFF8
movq (%rax,%rdx), %rax
testq %rax, %rax
je 0x1f685
movzwl 0x20(%rax), %edx
movw %dx, 0x2(%rcx)
xorl %edx, %edx
cmpl $0x1, 0x1c(%rax)
adcb $0x1, %dl
movzwl 0x18(%rax), %esi
movq 0x10(%rax), %rax
movb %dl, (%rcx)
movl %eax, 0x30(%rcx)
shrq $0x20, %rax
movl %eax, 0x34(%rcx)
movl %esi, 0x38(%rcx)
movabsq $0xff0000000000, %rax   # imm = 0xFF0000000000
movq %rax, 0x20(%rcx)
orq $0x1000, %rax           # imm = 0x1000
movq %rax, 0x28(%rcx)
movq %rcx, %rax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0xaf32
leaq 0x383a3(%rip), %rsi     # 0x57a58
movq %rsp, %rdi
callq 0x7690
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1f6e0
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x1f6ed
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x8(%rax), %rdx
leaq 0x20(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x7600
xorl %ebp, %ebp
movq 0x4f8ab(%rip), %rsi     # 0x6efc8
movq 0x4f884(%rip), %rdx     # 0x6efa8
movq %rbx, %rdi
callq 0x75e0
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x1f73e
callq 0x7370
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x1f74c
callq 0x7370
testb %bpl, %bpl
jne 0x1f769
jmp 0x1f771
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x1f769
callq 0x7370
jmp 0x1f769
movq %rax, %r14
movq %rbx, %rdi
callq 0x72c0
movq %r14, %rdi
callq 0x7620
nop