// File: narges64[P]SIMBA/garbage_collection.cpp
// Function: move_page(ssd_info*, local const*, gc_operation*)
STATE move_page(ssd_info * ssd,  const local * location, gc_operation * gc_node){

	sub_request * rsub = create_gc_sub_request(ssd, location, READ, gc_node);
	ssd->channel_head[rsub->location->channel]->lun_head[rsub->location->lun]->GCSubs.push_tail(rsub);


	sub_request * wsub = create_gc_sub_request(ssd, location, WRITE, gc_node);
	wsub->location = new local(location->channel, location->lun, location->plane);
	invalid_old_page(ssd, wsub);
	wsub->ppn = get_new_ppn(ssd, wsub->lpn, wsub->location, false); // false---> cold block 
	if( wsub->ppn == -1 || write_page(ssd, wsub->lpn, wsub->ppn) == FAIL) {
		cout << "error in move page " << endl; 
		return FAIL; 
	}
		
	find_location(ssd, wsub->ppn, wsub->location);	

	buffer_entry * buf_ent = ssd->dram->map->map_entry[wsub->lpn].buf_ent; 
	if (buf_ent == NULL){
		ssd->channel_head[wsub->location->channel]->lun_head[wsub->location->lun]->GCSubs.push_tail(wsub);
	} else {
		if (!buf_ent->gc) 
			ssd->dram->buffer->hit_write(buf_ent); 
		else 
			ssd->dram->gc_buffer->hit_write(buf_ent); 	
		delete wsub; 
	}	

	return SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0xa470
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x60(%rsi), %rcx
movslq (%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
callq 0x50c0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rcx
xorl %edx, %edx
callq 0xa470
movq %rax, -0x30(%rbp)
movl $0x18, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
callq 0x8b20
jmp 0xa2cb
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x6760
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0xc(%rax), %esi
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdx
xorl %ecx, %ecx
callq 0x5960
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x10(%rax)
je 0xa32d
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0xc(%rax), %esi
movq -0x30(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x5ac0
cmpl $0x0, %eax
jne 0xa37a
movq 0x9c9c(%rip), %rdi      # 0x13fd0
leaq 0x57dc(%rip), %rsi      # 0xfb17
callq 0x2110
movq %rax, %rdi
movq 0x9c76(%rip), %rsi      # 0x13fc0
callq 0x2150
movl $0x0, -0x4(%rbp)
jmp 0xa45e
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x18, %esi
callq 0x2140
jmp 0xa467
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0x10(%rax), %esi
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdx
callq 0x5c70
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xa3fb
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x60(%rcx), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x60(%rcx), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x48, %rdi
movq -0x30(%rbp), %rsi
callq 0x50c0
jmp 0xa457
movq -0x48(%rbp), %rax
testb $0x1, (%rax)
jne 0xa41b
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x8c70
jmp 0xa430
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x8c70
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xa455
movq -0x58(%rbp), %rdi
callq 0x8ce0
movq -0x58(%rbp), %rdi
movl $0xa0, %esi
callq 0x2140
jmp 0xa457
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2230