// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rdi, 0x8(%rsp)
movq 0x58(%rdi), %rax
movl 0x22c(%rax), %r12d
orl $-0x1, (%r8)
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
pushq $-0x1
popq %r15
xorl %ebp, %ebp
cmpq %rax, %rcx
je 0x55e4
movq (%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x55df
incl %ebp
movl 0x20(%rdi), %r8d
movl %r8d, (%rbx)
movq 0x60(%rdi), %rdi
movl 0x10(%rdi), %r15d
incq %rax
jmp 0x55c0
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 0x41ee
testb %al, %al
je 0x5628
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x18(%rsp)
xorl %r13d, %r13d
movl %r12d, 0x14(%rsp)
movl %r15d, 0x4(%rsp)
cmpl %r12d, %r13d
jge 0x5617
movslq %r13d, %rbx
cmpq $0x0, (%r14,%rbx,8)
jne 0x5712
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 %r13d, %esi
pushq $-0x1
popq %rdx
movl %r15d, %ecx
callq 0x41be
testq %rax, %rax
je 0x570d
movq %rax, %r15
movq 0x18(%rsp), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x569f
cmpl %eax, 0x20(%r15)
jne 0x570d
movq 0x8(%rsp), %r12
movq 0x90(%r12), %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 %r15, %rsi
callq 0x416c
movq %r12, %rdi
movq %r15, %rsi
callq 0x6364
cmpl $0x9, %eax
jne 0x56e8
decl %r13d
movl 0x14(%rsp), %r12d
jmp 0x570d
movl 0x20(%r15), %eax
leal -0x1(%rax), %ecx
cmpl $0x1, %ecx
movl 0x14(%rsp), %r12d
jbe 0x5700
cmpq $0x0, 0x18(%r15)
je 0x571a
movq %r15, (%r14,%rbx,8)
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
incl %ebp
movl 0x4(%rsp), %r15d
incl %r13d
jmp 0x563a
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rcx
movq 0x20(%rcx), %rdi
cmpl $0x0, 0x8(%rdi)
je 0x5700
movslq 0xc(%r15), %rax
movq 0x10(%rcx), %rdx
movq (%rdx), %rdx
movq %rax, %r8
shlq $0x4, %r8
movq 0x8(%rdx,%r8), %rsi
testq %rsi, %rsi
je 0x575c
cmpb $0x0, (%rsi)
jne 0x5752
movq 0x18(%rcx), %rdi
callq 0x5d32
jmp 0x57ed
movq %r8, 0x30(%rsp)
movl %eax, %esi
callq 0x5cdc
movq %rax, 0x18(%r15)
movb $0x1, (%rax)
cmpb $0x0, 0x8(%rax)
je 0x57aa
movq %r15, 0x10(%rax)
movq %r15, (%r14,%rbx,8)
movl 0x20(%r15), %eax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
incl %ebp
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
cmpl $0x3, 0x334(%rax)
movl 0x4(%rsp), %r15d
je 0x5712
jmp 0x5617
movq 0x8(%rsp), %rdx
movq 0x60(%rdx), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rcx
movq 0x30(%rsp), %rsi
movq %rax, 0x8(%rcx,%rsi)
movq 0x90(%rdx), %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 %r15, %rsi
callq 0x4120
decl %r13d
jmp 0x570d