// File: /workspace/github_cpp_makefile_star2_O1/narges64[P]SIMBA/ftl.cpp
// Function: find_lun_gc_requests(ssd_info*, unsigned int, unsigned int, sub_request**, int*)
int find_lun_gc_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;
	static int k = 0; 
	for (int i = 0; i < max_subs_count; i++){
		if (subs[i] != NULL) {
			subs_count++;
			(*operation) = subs[i]->operation;
			page_offset = subs[i]->location->page;
		}
	}

	if (ssd->channel_head[channel]->lun_head[lun]->GCSubs.is_empty()) return subs_count;
	for (int i = 0; i < max_subs_count; i++){
		if (subs[i] != NULL) continue;
		sub_request * temp = ssd->channel_head[channel]->lun_head[lun]->
					GCSubs.target_request(i, -1, page_offset);
		if (temp == NULL ||
			((*operation) != -1 && temp->operation != (*operation)))
			continue;  // can be improved
		ssd->channel_head[channel]->lun_head[lun]->GCSubs.remove_node(temp);
		if (find_subrequest_state(ssd, temp) == SR_MODE_COMPLETE) {
			i--;
			continue;
		}
		if (temp->operation == ERASE || temp->operation == READ) {
			subs[i] = temp;
			(*operation) = subs[i]->operation;
			subs_count++;
			continue;
		}
		// If it's write

		if ((temp->buf_entry != NULL) || (ssd->dram->gc_buffer->buffer_capacity== 0)){
			subs[i] = temp;
			(*operation) = subs[i]->operation;
			subs_count++;
			continue;
		}

		int lpn = temp->lpn;
		buffer_entry * buf_ent = ssd->dram->map->map_entry[lpn].buf_ent;
		if (buf_ent != NULL) {
			if (buf_ent->gc) 
				ssd->dram->gc_buffer->hit_write(buf_ent); 
			else 
				ssd->dram->buffer->hit_write(buf_ent);  
			i--;
			continue;
		}else {
			buf_ent = ssd->dram->gc_buffer->add_head(lpn);
			temp->buf_entry = buf_ent;
			buf_ent->gc = true;

			if (!buf_ent->outlier) {
				ssd->dram->map->map_entry[lpn].buf_ent= buf_ent;
				ssd->channel_head[channel]->lun_head[lun]->GCSubs.push_tail(temp);
				i--;
				continue;
			}else {
				buf_ent->sub = temp;
				subs[i] = temp;
				(*operation) = subs[i]->operation;
				subs_count++;
			}

		}

		if (ssd->parameter->plane_level_tech != GCGC)
			return subs_count; // NOT FOR GCGC
	}

	return subs_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r12
movq %rcx, %r14
movq %rdi, 0x8(%rsp)
movq 0x58(%rdi), %rax
movslq 0x22c(%rax), %r15
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
testq %r15, %r15
jle 0x5dc4
xorl %eax, %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq (%r14,%rax,8), %rcx
testq %rcx, %rcx
je 0x5dba
incl %ebx
movl 0x20(%rcx), %edi
movl %edi, (%r12)
movq 0x60(%rcx), %rcx
movl 0x10(%rcx), %r13d
incq %rax
cmpq %rax, %r15
jne 0x5da0
jmp 0x5dcc
xorl %ebx, %ebx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rax
movl %esi, %ecx
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl %edx, %ecx
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
callq 0x45a6
testl %r15d, %r15d
jle 0x604f
testb %al, %al
jne 0x604f
movq %r12, 0x18(%rsp)
xorl %r12d, %r12d
movq %r14, 0x38(%rsp)
movl %r13d, 0x4(%rsp)
movslq %r12d, %rbp
cmpq $0x0, (%r14,%rbp,8)
jne 0x5ff4
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
movl %r12d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %r13d, %ecx
callq 0x457a
movl $0x7, %ecx
testq %rax, %rax
je 0x5fe2
movq %rax, %r13
movq 0x18(%rsp), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x5e89
cmpl %eax, 0x20(%r13)
jne 0x5fe2
movq 0x8(%rsp), %r14
movq 0x90(%r14), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
movq %r13, %rsi
callq 0x4524
movq %r14, %rdi
movq %r13, %rsi
callq 0x6e78
cmpl $0x9, %eax
jne 0x5ed1
decl %r12d
movq 0x38(%rsp), %r14
jmp 0x5ef6
movl 0x20(%r13), %ecx
leal -0x1(%rcx), %eax
cmpl $0x1, %eax
movq 0x38(%rsp), %r14
jbe 0x5ee9
cmpq $0x0, 0x18(%r13)
je 0x5f05
movq %r13, (%r14,%rbp,8)
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
incl %ebx
movl 0x4(%rsp), %r13d
movl $0x7, %ecx
jmp 0x5fe7
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rax
movq 0x20(%rax), %rdi
cmpl $0x0, 0x8(%rdi)
je 0x5ee9
movl 0xc(%r13), %r8d
movq 0x10(%rax), %rcx
movq (%rcx), %rcx
movslq %r8d, %rdx
shlq $0x4, %rdx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x5f47
cmpb $0x0, (%rsi)
jne 0x5f3d
movq 0x18(%rax), %rdi
callq 0x6678
jmp 0x6047
movq %rdx, 0x40(%rsp)
movq %rdi, 0x50(%rsp)
movl $0x28, %edi
movl %r8d, 0x34(%rsp)
callq 0x2190
movq %rax, %rsi
movq %rax, 0x48(%rsp)
xorl %eax, %eax
movb %al, 0x2(%rsi)
movb %al, 0x8(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movq %rax, 0x20(%rsi)
movl 0x34(%rsp), %eax
movl %eax, 0x4(%rsi)
movw $0x0, (%rsi)
movq 0x50(%rsp), %rdi
callq 0x696e
movq 0x48(%rsp), %rsi
testb %al, %al
movl $0x0, %eax
cmoveq %rax, %rsi
movq %rsi, 0x18(%r13)
movb $0x1, (%rsi)
cmpb %al, 0x8(%rsi)
je 0x6004
movq %r13, 0x10(%rsi)
movq %r13, (%r14,%rbp,8)
movl 0x20(%r13), %eax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
incl %ebx
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
xorl %ecx, %ecx
cmpl $0x3, 0x334(%rax)
setne %cl
movl 0x14(%rsp), %eax
cmovnel %ebx, %eax
movl %eax, 0x14(%rsp)
movl 0x4(%rsp), %r13d
cmpl $0x7, %ecx
je 0x5ff4
movl 0x14(%rsp), %eax
testl %ecx, %ecx
jne 0x6051
incl %r12d
movl %ebx, %eax
cmpl %r15d, %r12d
jl 0x5e1e
jmp 0x6051
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rdx
movq %rsi, 0x8(%rax,%rdx)
movq 0x90(%rcx), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
movq %r13, %rsi
callq 0x44d6
decl %r12d
jmp 0x5ef6
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq