// File: narges64[P]SIMBA/garbage_collection.cpp
// Function: pre_process_gc(ssd_info*, local const*)
void pre_process_gc(ssd_info * ssd, const local * location){

	int pre_process_move = 0;
	local * gc_location = new local(location->channel, location->lun, location->plane);
	if (find_victim_block(ssd, gc_location) != SUCCESS) {
		printf("Error: invalid block selected for gc \n");
		delete gc_location;
		return;
	}
	//location->print();
	for(unsigned int i=0;i<ssd->parameter->page_block;i++)
	{
		if(ssd->channel_head[gc_location->channel]->lun_head[gc_location->lun]->plane_head[gc_location->plane]->blk_head[gc_location->block]->page_head[i]->valid_state == true)
		{
			gc_location->page=i;

			int lpn = ssd->channel_head[gc_location->channel]->lun_head[gc_location->lun]->plane_head[gc_location->plane]->blk_head[gc_location->block]->page_head[gc_location->page]->lpn;
			local * location = new local(0,0,0,0,0); 
			invalid_old_page(ssd, lpn, location);
			int ppn = get_new_ppn(ssd, lpn, location, false); // false indicate we want cold active block 
			if (ppn ==-1 || write_page(ssd, lpn, ppn) == FAIL) {
				cout << "error in pre-process " << endl; 
				return; 
			}
				
			pre_process_move++;
		}
	}
	erase_block(ssd, gc_location);
	delete gc_location;
	ssd->stats->gc_moved_page += pre_process_move;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x18, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
callq 0x8b20
jmp 0xb5fe
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xa000
cmpl $0x1, %eax
je 0xb66f
leaq 0x436d(%rip), %rdi      # 0xf98c
movb $0x0, %al
callq 0x2050
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xb64b
movq -0x58(%rbp), %rdi
callq 0x8b50
movq -0x58(%rbp), %rdi
movl $0x18, %esi
callq 0x2140
jmp 0xb85b
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x18, %esi
callq 0x2140
jmp 0xb861
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x234(%rcx), %eax
jae 0xb80f
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0xc(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movl -0x30(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movb (%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xb7ff
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq (%rax), %rax
movslq 0x8(%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x20(%rax), %rax
movslq 0xc(%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x20(%rax), %rax
movslq 0x10(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x18, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
xorl %r9d, %r9d
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x8ae0
jmp 0xb76c
movq -0x60(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x40(%rbp), %rdx
callq 0x5840
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x5960
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
je 0xb7b3
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x44(%rbp), %edx
callq 0x5ac0
cmpl $0x0, %eax
jne 0xb7f6
movq 0x8816(%rip), %rdi      # 0x13fd0
leaq 0x436a(%rip), %rsi      # 0xfb2b
callq 0x2110
movq %rax, %rdi
movq 0x87f0(%rip), %rsi      # 0x13fc0
callq 0x2150
jmp 0xb85b
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x18, %esi
callq 0x2140
jmp 0xb861
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xb801
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xb676
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xa870
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0xb841
movq -0x68(%rbp), %rdi
callq 0x8b50
movq -0x68(%rbp), %rdi
movl $0x18, %esi
callq 0x2140
movl -0x14(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
addl 0xc8(%rax), %ecx
movl %ecx, 0xc8(%rax)
addq $0x70, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x2230
nopw (%rax,%rax)