// File: /workspace/github_cpp_makefile_star2_O1/narges64[P]SIMBA/ftl.cpp
// Function: find_lun_io_requests(ssd_info*, unsigned int, unsigned int, sub_request**, int*)
int find_lun_io_requests(ssd_info * ssd, unsigned int channel,
				unsigned int lun, sub_request ** subs, int * operation){
	int max_subs_count = ssd->parameter->plane_lun;
	unsigned int page_offset = -1;
	int subs_count = 0;
	(*operation) = -1;
	for (int i = 0; i < max_subs_count; i++){
		if (subs[i] == NULL) continue;
		subs_count++;
		(*operation) = subs[i]->operation;
		page_offset = subs[i]->location->page;
	}
	if ((*operation) == ERASE ) return subs_count;

	for (unsigned plane = 0; plane < ssd->parameter->plane_lun; plane++){
		if (subs[plane] != NULL ) continue;
		if ((*operation) == -1 || (*operation) == READ ) {
			subs[plane] = ssd->channel_head[channel]->lun_head[lun]->
						rsubs_queue.target_request(plane, -1, page_offset);
			if (subs[plane] != NULL) {
				ssd->channel_head[channel]->lun_head[lun]->rsubs_queue.remove_node(subs[plane]);
				page_offset = subs[plane]->location->page;
				subs_count++;
				(*operation) = subs[plane]->operation;
			}
		}
	}

	if ((*operation) == READ) return subs_count;


	for (unsigned plane = 0; plane < ssd->parameter->plane_lun; plane++){
		if (subs[plane] != NULL ) continue;

		if ((*operation) == -1 || (*operation) == WRITE ){
			subs[plane] = ssd->channel_head[channel]->lun_head[lun]->
						wsubs_queue.target_request(plane, -1, page_offset);
			if (subs[plane] != NULL) {
				ssd->channel_head[channel]->lun_head[lun]->wsubs_queue.remove_node(subs[plane]);
				page_offset = subs[plane]->location->page;
				subs_count++;
				(*operation) = subs[plane]->operation;
			}
		}
	}

	return subs_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdi, %r10
movq 0x58(%rdi), %rax
movslq 0x22c(%rax), %rax
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
testq %rax, %rax
jle 0x59ee
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %ebp, %ebp
movq (%r14,%r9,8), %rdi
testq %rdi, %rdi
je 0x59e4
incl %ebp
movl 0x20(%rdi), %ecx
movl %ecx, (%r8)
movq 0x60(%rdi), %rdi
movl 0x10(%rdi), %ecx
incq %r9
cmpq %r9, %rax
jne 0x59cc
jmp 0x59f5
xorl %ebp, %ebp
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x2, (%r8)
je 0x5b98
movq 0x58(%r10), %rax
cmpl $0x0, 0x22c(%rax)
movq %r8, 0x8(%rsp)
movl %edx, 0x14(%rsp)
movl %esi, 0x10(%rsp)
je 0x5acd
movl %esi, %r12d
movl %edx, %r15d
xorl %r13d, %r13d
cmpq $0x0, (%r14,%r13,8)
jne 0x5ab7
movl (%r8), %eax
cmpl $0x1, %eax
je 0x5a3e
cmpl $-0x1, %eax
jne 0x5ab7
movq 0x90(%r10), %rax
movq (%rax,%r12,8), %rax
movq 0x50(%rax), %rax
movq (%rax,%r15,8), %rdi
addq $0x60, %rdi
movl %r13d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r10, %rbx
movl %ecx, 0x4(%rsp)
callq 0x457a
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x5aab
movq 0x90(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r15,8), %rdi
addq $0x60, %rdi
movq %rax, %rsi
callq 0x4524
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl %ebp
movl 0x20(%rax), %eax
movq 0x8(%rsp), %r8
movl %eax, (%r8)
jmp 0x5ab4
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r8
movq %rbx, %r10
incq %r13
movq 0x58(%r10), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %r13
jb 0x5a26
cmpl $0x1, (%r8)
je 0x5b98
movq 0x58(%r10), %rax
cmpl $0x0, 0x22c(%rax)
je 0x5b98
movl 0x10(%rsp), %ebx
movl 0x14(%rsp), %r12d
xorl %r13d, %r13d
cmpq $0x0, (%r14,%r13,8)
jne 0x5b82
movl (%r8), %eax
incl %eax
cmpl $0x1, %eax
ja 0x5b82
movq 0x90(%r10), %rax
movq (%rax,%rbx,8), %rax
movq 0x50(%rax), %rax
movq (%rax,%r12,8), %rdi
addq $0x78, %rdi
movl %r13d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r10, %r15
movl %ecx, 0x4(%rsp)
callq 0x457a
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x5b76
movq 0x90(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r12,8), %rdi
addq $0x78, %rdi
movq %rax, %rsi
callq 0x4524
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl %ebp
movl 0x20(%rax), %eax
movq 0x8(%rsp), %r8
movl %eax, (%r8)
jmp 0x5b7f
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r8
movq %r15, %r10
incq %r13
movq 0x58(%r10), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %r13
jb 0x5af4
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq