// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %r8, %r15
movq %rcx, %r14
movq %rdi, 0x10(%rsp)
movq 0x58(%rdi), %rax
movslq 0x22c(%rax), %rbx
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
testq %rbx, %rbx
jle 0x5d3e
xorl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq (%r14,%rax,8), %rcx
testq %rcx, %rcx
je 0x5d34
incl %ebp
movl 0x20(%rcx), %edi
movl %edi, (%r15)
movq 0x60(%rcx), %rcx
movl 0x10(%rcx), %r12d
incq %rax
cmpq %rax, %rbx
jne 0x5d1b
jmp 0x5d46
xorl %ebp, %ebp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x10(%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 0x458a
testl %ebx, %ebx
movl %ebp, 0xc(%rsp)
jle 0x5f82
testb %al, %al
jne 0x5f82
movl %r12d, 0x34(%rsp)
movq %r15, 0x18(%rsp)
xorl %r13d, %r13d
movslq %r13d, %r12
cmpq $0x0, (%r14,%r12,8)
je 0x5dad
incl %r13d
cmpl %ebx, %r13d
jl 0x5d96
jmp 0x5f82
movq 0x10(%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
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl 0x34(%rsp), %ecx
callq 0x455c
testq %rax, %rax
je 0x5da0
movq %rax, %r15
movq 0x18(%rsp), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x5dfe
cmpl %eax, 0x20(%r15)
jne 0x5da0
movq 0x10(%rsp), %rbp
movq 0x90(%rbp), %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 0x4506
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6d68
cmpl $0x9, %eax
jne 0x5e44
decl %r13d
jmp 0x5da0
movl 0x20(%r15), %ecx
leal -0x1(%rcx), %eax
cmpl $0x1, %eax
jbe 0x5e57
cmpq $0x0, 0x18(%r15)
je 0x5e6b
movq %r15, (%r14,%r12,8)
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
incl 0xc(%rsp)
jmp 0x5da0
movq 0x10(%rsp), %rax
movq 0x60(%rax), %rax
movq 0x20(%rax), %rdi
cmpl $0x0, 0x8(%rdi)
je 0x5e57
movl 0xc(%r15), %r8d
movq 0x10(%rax), %rcx
movq (%rcx), %rcx
movslq %r8d, %rdx
shlq $0x4, %rdx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x5eaa
cmpb $0x0, (%rsi)
jne 0x5ea3
movq 0x18(%rax), %rdi
callq 0x6578
jmp 0x5e3c
movq %rdx, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
movl $0x28, %edi
movl %r8d, 0x30(%rsp)
callq 0x2190
movq %rax, %rbp
xorl %eax, %eax
movb %al, 0x2(%rbp)
movb %al, 0x8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbp)
movq %rax, 0x20(%rbp)
movl 0x30(%rsp), %eax
movl %eax, 0x4(%rbp)
movw $0x0, (%rbp)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x6878
testb %al, %al
movl $0x0, %eax
cmoveq %rax, %rbp
movq %rbp, 0x18(%r15)
movb $0x1, (%rbp)
cmpb %al, 0x8(%rbp)
je 0x5f3a
movq %r15, 0x10(%rbp)
movq %r15, (%r14,%r12,8)
movl 0x20(%r15), %eax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
incl 0xc(%rsp)
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rax
cmpl $0x3, 0x334(%rax)
je 0x5da0
jmp 0x5f82
movq 0x10(%rsp), %rcx
movq 0x60(%rcx), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x38(%rsp), %rdx
movq %rbp, 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 %r15, %rsi
callq 0x44b8
jmp 0x5e3c
movl 0xc(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq