// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq 0x58(%rdi), %rax
movl 0x230(%rax), %r12d
leaq (,%r12,4), %rdi
callq 0x2040
movq %rax, %r15
xorl %eax, %eax
cmpq %rax, %r12
je 0x6b7b
movl %eax, (%r15,%rax,4)
incq %rax
jmp 0x6b6d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5b4b
movl %eax, %ebp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5bf9
movq %r12, %rdx
movl %eax, %r12d
movq %r14, (%rsp)
movq 0x90(%r14), %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 (%r15,%rax,4), %eax
movslq %ebp, %rcx
movl %eax, (%r15,%rcx,4)
addl $-0x2, %edx
movq %rdx, 0x10(%rsp)
movl (%r15,%rdx,4), %eax
movslq %r12d, %rcx
movl %eax, (%r15,%rcx,4)
xorl %ebx, %ebx
cmpq $0xc8, %rbx
je 0x6c47
callq 0x2090
movq 0x10(%rsp), %rcx
movl %ecx, %r14d
subl %ebx, %r14d
xorl %edx, %edx
divl %r14d
cmpl %r12d, %edx
je 0x6c22
cmpl %ebp, %edx
je 0x6c22
movq 0x20(%r13), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x4(%rax)
jle 0x6c29
callq 0x2090
jmp 0x6c03
testl %edx, %edx
je 0x6c42
movl (%r15,%rbx,4), %eax
addl %ebx, %edx
movslq %edx, %rcx
movl (%r15,%rcx,4), %edx
movl %edx, (%r15,%rbx,4)
movl %eax, (%r15,%rcx,4)
incq %rbx
jmp 0x6bea
xorl %eax, %eax
pushq $-0x1
popq %r14
xorl %ecx, %ecx
movq 0x8(%rsp), %rbx
cmpq $0xc8, %rax
je 0x6c91
movslq (%r15,%rax,4), %rdx
cmpl %ebp, %edx
sete %sil
cmpl %r12d, %edx
sete %dil
orb %sil, %dil
jne 0x6c8c
movq 0x20(%r13), %rsi
movq (%rsi,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
jg 0x6c8c
movl 0x8(%rsi), %esi
cmpl %ecx, %esi
cmovgl %edx, %r14d
cmovgl %esi, %ecx
incq %rax
jmp 0x6c54
cmpl $-0x1, %r14d
jne 0x6ca3
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x6a2d
movl %r14d, 0xc(%rbx)
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x2170
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq