// File: /workspace/github_cpp_makefile_star2_O3/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
movq %r12, %r13
testq %r12, %r12
je 0x774d
leaq 0x1(%r13), %rax
andq $-0x2, %rax
leaq -0x1(%r13), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x3325(%rip), %xmm1     # 0xaa00
xorl %ecx, %ecx
movdqa 0x332b(%rip), %xmm2     # 0xaa10
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x332b(%rip), %xmm4     # 0xaa20
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x772a
movl %ecx, (%r14,%rcx,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x7740
leal 0x1(%rcx), %edx
movl %edx, 0x4(%r14,%rcx,4)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x76f5
movq %r15, %rdi
movq %rbx, %rsi
callq 0x636f
movl %eax, %ebp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x643d
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), %r15
leal -0x1(%r13), %eax
movl (%r14,%rax,4), %eax
movslq %ebp, %rcx
movl %eax, (%r14,%rcx,4)
addl $-0x2, %r13d
movq %r13, 0x10(%rsp)
movl (%r14,%r13,4), %eax
movslq %r12d, %rcx
movl %eax, (%r14,%rcx,4)
xorl %ebx, %ebx
callq 0x20a0
movq 0x10(%rsp), %rcx
movl %ecx, %r13d
subl %ebx, %r13d
xorl %edx, %edx
divl %r13d
cmpl %r12d, %edx
je 0x77ea
cmpl %ebp, %edx
je 0x77ea
movq 0x20(%r15), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rcx
cmpl $0x0, 0x4(%rcx)
jle 0x77f1
callq 0x20a0
jmp 0x77cb
testl %edx, %edx
je 0x780a
movl (%r14,%rbx,4), %ecx
addl %ebx, %edx
movslq %edx, %rdx
movl (%r14,%rdx,4), %esi
movl %esi, (%r14,%rbx,4)
movl %ecx, (%r14,%rdx,4)
incq %rbx
cmpq $0xc8, %rbx
jne 0x77bb
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %r15
movslq (%r14,%rcx,4), %rsi
cmpl %ebp, %esi
sete %dil
cmpl %r12d, %esi
sete %r8b
orb %dil, %r8b
jne 0x784f
movq (%rax,%rsi,8), %rdi
cmpl $0x0, 0x4(%rdi)
jg 0x784f
movl 0x8(%rdi), %edi
cmpl %edx, %edi
cmovgl %esi, %ebx
cmovgl %edi, %edx
incq %rcx
cmpq $0xc8, %rcx
jne 0x7824
cmpl $-0x1, %ebx
jne 0x786c
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x7561
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