// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq 0x58(%rdi), %rax
movslq 0x22c(%rax), %r11
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
testq %r11, %r11
jle 0x5989
xorl %edi, %edi
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq (%r14,%rdi,8), %r9
testq %r9, %r9
je 0x5972
incl %ebx
movl 0x20(%r9), %r10d
movl %r10d, (%r8)
movq 0x60(%r9), %r9
movl 0x10(%r9), %ecx
incq %rdi
cmpq %rdi, %r11
jne 0x5958
movl %ebx, 0xc(%rsp)
cmpl $0x2, %r10d
jne 0x5996
jmp 0x5b11
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x0, 0xc(%rsp)
cmpl $0x0, 0x22c(%rax)
je 0x5b11
movl %esi, %ebx
movl %edx, %r12d
xorl %r13d, %r13d
movq %r8, 0x10(%rsp)
cmpq $0x0, (%r14,%r13,8)
je 0x59ce
incq %r13
movq 0x58(%r15), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %r13
jb 0x59b0
jmp 0x5a54
movl (%r8), %eax
cmpl $0x1, %eax
je 0x59db
cmpl $-0x1, %eax
jne 0x59b7
movq 0x90(%r15), %rax
movq (%rax,%rbx,8), %rax
movq 0x50(%rax), %rax
movq (%rax,%r12,8), %rdi
addq $0x60, %rdi
movl %r13d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %ecx, %ebp
callq 0x455c
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x5a48
movq 0x90(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r12,8), %rdi
addq $0x60, %rdi
movq %rax, %rsi
callq 0x4506
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl 0xc(%rsp)
movl 0x20(%rax), %eax
movq 0x10(%rsp), %r8
movl %eax, (%r8)
jmp 0x59b7
movq 0x10(%rsp), %r8
movl %ebp, %ecx
jmp 0x59b7
testl %eax, %eax
sete %al
cmpl $0x1, (%r8)
sete %dil
orb %al, %dil
jne 0x5b11
xorl %r13d, %r13d
cmpq $0x0, (%r14,%r13,8)
jne 0x5afb
movl (%r8), %eax
incl %eax
cmpl $0x1, %eax
ja 0x5afb
movq 0x90(%r15), %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 %r15, %rbp
movl %ecx, %r15d
callq 0x455c
movq %rax, (%r14,%r13,8)
testq %rax, %rax
je 0x5af0
movq 0x90(%rbp), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx,%r12,8), %rdi
addq $0x78, %rdi
movq %rax, %rsi
callq 0x4506
movq (%r14,%r13,8), %rax
movq 0x60(%rax), %rcx
movl 0x10(%rcx), %ecx
incl 0xc(%rsp)
movl 0x20(%rax), %eax
movq 0x10(%rsp), %r8
movl %eax, (%r8)
jmp 0x5af8
movq 0x10(%rsp), %r8
movl %r15d, %ecx
movq %rbp, %r15
incq %r13
movq 0x58(%r15), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %r13
jb 0x5a6d
movl 0xc(%rsp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq