// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x22c(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x34(%rbp)
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x7452
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x7413
jmp 0x7447
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x20(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x73fa
movq -0x28(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x7466
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x7692
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x22c(%rcx), %eax
jae 0x756f
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x7497
jmp 0x7561
movq -0x28(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x74ad
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x755f
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x18(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x60, %rdi
movl -0x3c(%rbp), %esi
movl -0x30(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5290
movq %rax, %rdx
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x755d
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x18(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x60, %rdi
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
callq 0x5190
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x20(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x755f
jmp 0x7561
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x746d
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x7583
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x7692
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x22c(%rcx), %eax
jae 0x768c
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x75b4
jmp 0x767e
movq -0x28(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x75ca
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x767c
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x18(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x78, %rdi
movl -0x40(%rbp), %esi
movl -0x30(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5290
movq %rax, %rdx
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x767a
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x18(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
addq $0x78, %rdi
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
callq 0x5190
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x20(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x767c
jmp 0x767e
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x758a
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)