// File: /workspace/github_cpp_makefile_star2_O1/narges64[P]SIMBA/garbage_collection.cpp
// Function: RGA_algorithm(ssd_info*, local*)
STATE RGA_algorithm(ssd_info * ssd, local * location){
	unsigned int window_size = 200; // FIXME
	unsigned int total_size = ssd->parameter->block_plane;

	int * blocks = new int[total_size];
	for (int i = 0; i < total_size; i++){
		blocks[i] = i;
	}

	int active_block = get_active_block(ssd, location);
	int cold_active_block = get_cold_active_block(ssd, location); 
	plane_info * p = ssd->channel_head[location->channel]->lun_head[location->lun]->plane_head[location->plane];

	blocks[active_block] = blocks[total_size - 1];
	total_size--;
	blocks[cold_active_block] = blocks[total_size -1]; 
	total_size--; 

	for (int i = 0; i < window_size; i++){
		int j = rand() % (total_size - i);
		while ((p->blk_head[j]->free_page_num >0) || (j ==active_block) || (j == cold_active_block)) {
			j = rand() % (total_size - i); 
		}
		if (j != 0){
			int temp = blocks[i];
			blocks[i] = blocks[i+j];
			blocks[i + j] = temp;
		}
	}

	int block = -1;
	int max_invalid = 0; 
	for (int i = 0; i < window_size; i++){
		int temp = blocks[i]; 
		if (temp == active_block || temp == cold_active_block) continue; 
		if (p->blk_head[temp]->free_page_num > 0) continue; 
		if (p->blk_head[temp]->invalid_page_num > max_invalid) {
			block = temp; 
			max_invalid = p->blk_head[temp]->invalid_page_num; 
		}
		
	}
	if (block == -1) 
			greedy_algorithm(ssd, location); 
	
	location->block = block;
	delete blocks;

	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x58(%rdi), %rax
movl 0x230(%rax), %r12d
leaq (,%r12,4), %rdi
callq 0x2040
movq %rax, %r14
testq %r12, %r12
je 0x77e7
xorl %eax, %eax
movl %eax, (%r14,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x77db
movq %r15, %rdi
movq %rbx, %rsi
callq 0x644e
movl %eax, %ebp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x652d
movq %r12, %rdx
movl %eax, %r12d
movq %r15, (%rsp)
movq 0x90(%r15), %rax
movslq (%rbx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rbx), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rbx, 0x8(%rsp)
movslq 0x8(%rbx), %rcx
movq (%rax,%rcx,8), %r13
leal -0x1(%rdx), %eax
movl (%r14,%rax,4), %eax
movslq %ebp, %rcx
movl %eax, (%r14,%rcx,4)
addl $-0x2, %edx
movq %rdx, 0x10(%rsp)
movl (%r14,%rdx,4), %eax
movslq %r12d, %rcx
movl %eax, (%r14,%rcx,4)
xorl %ebx, %ebx
callq 0x20a0
movq 0x10(%rsp), %rcx
movl %ecx, %r15d
subl %ebx, %r15d
xorl %edx, %edx
divl %r15d
cmpl %r12d, %edx
je 0x7885
cmpl %ebp, %edx
je 0x7885
movq 0x20(%r13), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x4(%rax)
jle 0x788c
callq 0x20a0
jmp 0x7866
testl %edx, %edx
je 0x78a5
movl (%r14,%rbx,4), %eax
addl %ebx, %edx
movslq %edx, %rcx
movl (%r14,%rcx,4), %edx
movl %edx, (%r14,%rbx,4)
movl %eax, (%r14,%rcx,4)
incq %rbx
cmpq $0xc8, %rbx
jne 0x7856
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r14,%rax,4), %rdx
cmpl %ebp, %edx
sete %sil
cmpl %r12d, %edx
sete %dil
orb %sil, %dil
jne 0x78e9
movq 0x20(%r13), %rsi
movq (%rsi,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
jg 0x78e9
movl 0x8(%rsi), %esi
cmpl %ecx, %esi
cmovgl %edx, %ebx
cmovgl %esi, %ecx
incq %rax
cmpq $0xc8, %rax
jne 0x78ba
cmpl $-0x1, %ebx
movq 0x8(%rsp), %r15
jne 0x790a
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x7684
movl %ebx, 0xc(%r15)
movl $0x4, %esi
movq %r14, %rdi
callq 0x21a0
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq