// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq 0x58(%rdi), %rax
movl 0x22c(%rax), %ebx
orl $-0x1, (%r8)
xorl %edi, %edi
testl %ebx, %ebx
cmovlel %edi, %ebx
pushq $-0x1
popq %r10
movl %r10d, %ecx
xorl %ebp, %ebp
cmpq %rdi, %rbx
je 0x52af
movq (%r14,%rdi,8), %r9
testq %r9, %r9
je 0x52aa
incl %ebp
movl 0x20(%r9), %r10d
movl %r10d, (%r8)
movq 0x60(%r9), %r9
movl 0x10(%r9), %ecx
incq %rdi
jmp 0x528b
cmpl $0x2, %r10d
je 0x5374
movl %esi, %ebx
movl %edx, %r15d
xorl %r13d, %r13d
movq %r8, 0x10(%rsp)
movl 0x22c(%rax), %eax
cmpq %rax, %r13
jae 0x536e
cmpq $0x0, (%r14,%r13,8)
je 0x52e6
incq %r13
movq 0x58(%r12), %rax
jmp 0x52c6
movl (%r8), %eax
cmpl $0x1, %eax
je 0x52f3
cmpl $-0x1, %eax
jne 0x52dc
movq 0x90(%r12), %rax
movq (%rax,%rbx,8), %rax
movq 0x50(%rax), %rax
movq (%rax,%r15,8), %rdi
addq $0x60, %rdi
movl %r13d, %esi
pushq $-0x1
popq %rdx
movl %ecx, 0xc(%rsp)
callq 0x41be
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x5360
movq 0x90(%r12), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r15,8), %rdi
addq $0x60, %rdi
movq %rax, %rsi
callq 0x416c
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl %ebp
movl 0x20(%rax), %eax
movq 0x10(%rsp), %r8
movl %eax, (%r8)
jmp 0x52dc
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %ecx
jmp 0x52dc
cmpl $0x1, (%r8)
jne 0x5385
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
movl %eax, %eax
cmpq %rax, %r13
jae 0x5374
cmpq $0x0, (%r14,%r13,8)
je 0x53a6
incq %r13
movq 0x58(%r12), %rax
movl 0x22c(%rax), %eax
jmp 0x5388
movl (%r8), %eax
incl %eax
cmpl $0x1, %eax
ja 0x5396
movq 0x90(%r12), %rax
movq (%rax,%rbx,8), %rax
movq 0x50(%rax), %rax
movq (%rax,%r15,8), %rdi
addq $0x78, %rdi
movl %r13d, %esi
pushq $-0x1
popq %rdx
movl %ecx, 0xc(%rsp)
callq 0x41be
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x541d
movq 0x90(%r12), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r15,8), %rdi
addq $0x78, %rdi
movq %rax, %rsi
callq 0x416c
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl %ebp
movl 0x20(%rax), %eax
movq 0x10(%rsp), %r8
movl %eax, (%r8)
jmp 0x5396
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %ecx
jmp 0x5396