// File: narges64[P]SIMBA/garbage_collection.cpp
// Function: create_gc_sub_request(ssd_info*, local const*, int, gc_operation*)
sub_request * create_gc_sub_request( ssd_info * ssd,const local * location, int operation, gc_operation * gc_node){

	if (gc_node != NULL)	{
		if (location->channel != gc_node->location->channel || location->lun != gc_node->location->lun){
			cout << "Error in location and gc_node incompatible! " << endl;
			return NULL;
		}
	}

	sub_request * sub = new sub_request(ssd->current_time, -1, ssd->subrequest_sequence_number++,operation);

	sub->gc_node = gc_node;
	sub->begin_time=ssd->current_time;

	if (operation != ERASE) {
		sub->lpn = ssd->channel_head[location->channel]->lun_head[location->lun]->plane_head[location->plane]->blk_head[location->block]->page_head[location->page]->lpn;
		if (ssd->dram->map->map_entry[sub->lpn].state){
			sub->state = 1;
			if (sub->lpn == -1) 
				cout << "Error in state of physical page " << endl; 
		}else{
			sub->state = 0;
			if (sub->lpn != -1) 
				cout << "Error in state of invalid physical page " << endl; 
		}
	}

	if (operation == READ)
	{
		if (sub->location != NULL) delete sub->location;
		sub->location = new local(location->channel, location->lun, location->plane,location->block, location->page);
		sub->ppn = ssd->dram->map->map_entry[sub->lpn].pn;
	}
	else if(operation == WRITE)
	{
		if (sub->location != NULL) delete sub->location;
	}
	else if (operation == ERASE)
	{
		if (sub->location != NULL) delete sub->location;
		sub->location = new local(location->channel, location->lun, location->plane, location->block, 0);
		sub->ppn = -1;
	}else {
		delete sub;
		printf("\nERROR ! Unexpected command.\n");
		return NULL;
	}

	return sub;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa4e6
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl (%rcx), %eax
jne 0xa4b5
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x4(%rcx), %eax
je 0xa4e4
movq 0x9b14(%rip), %rdi      # 0x13fd0
leaq 0x55bf(%rip), %rsi      # 0xfa82
callq 0x2110
movq %rax, %rdi
movq 0x9aee(%rip), %rsi      # 0x13fc0
callq 0x2150
movq $0x0, -0x8(%rbp)
jmp 0xa854
jmp 0xa4e6
movl $0xa0, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl 0xc(%rax), %ecx
movl %ecx, %edx
incl %edx
movl %edx, 0xc(%rax)
movl -0x1c(%rbp), %r8d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8fa0
jmp 0xa51b
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x48(%rax)
cmpl $0x2, -0x1c(%rbp)
je 0xa662
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0xc(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x10(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
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
testb $0x1, 0x4(%rax)
je 0xa627
movq -0x30(%rbp), %rax
movl $0x1, 0x40(%rax)
movq -0x30(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
jne 0xa625
movq 0x99e7(%rip), %rdi      # 0x13fd0
leaq 0x54bf(%rip), %rsi      # 0xfaaf
callq 0x2110
movq %rax, %rdi
movq 0x99c1(%rip), %rsi      # 0x13fc0
callq 0x2150
jmp 0xa625
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0xa0, %esi
callq 0x2140
jmp 0xa861
jmp 0xa660
movq -0x30(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x30(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
je 0xa65e
movq 0x998d(%rip), %rdi      # 0x13fd0
leaq 0x5486(%rip), %rsi      # 0xfad0
callq 0x2110
movq %rax, %rdi
movq 0x9967(%rip), %rsi      # 0x13fc0
callq 0x2150
jmp 0xa660
jmp 0xa662
cmpl $0x1, -0x1c(%rbp)
jne 0xa727
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xa6a2
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0xa6a0
movq -0x50(%rbp), %rdi
callq 0x8b50
movq -0x50(%rbp), %rdi
movl $0x18, %esi
callq 0x2140
jmp 0xa6a2
movl $0x18, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
movl 0x10(%rax), %r9d
callq 0x8ae0
jmp 0xa6d1
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
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
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0xa84c
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x18, %esi
callq 0x2140
jmp 0xa861
cmpl $0x0, -0x1c(%rbp)
jne 0xa768
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xa763
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0xa761
movq -0x60(%rbp), %rdi
callq 0x8b50
movq -0x60(%rbp), %rdi
movl $0x18, %esi
callq 0x2140
jmp 0xa763
jmp 0xa84a
cmpl $0x2, -0x1c(%rbp)
jne 0xa80b
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xa7a8
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0xa7a6
movq -0x68(%rbp), %rdi
callq 0x8b50
movq -0x68(%rbp), %rdi
movl $0x18, %esi
callq 0x2140
jmp 0xa7a8
movl $0x18, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
xorl %r9d, %r9d
callq 0x8ae0
jmp 0xa7d6
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x30(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
jmp 0xa848
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x18, %esi
callq 0x2140
jmp 0xa861
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0xa830
movq -0x78(%rbp), %rdi
callq 0x8ce0
movq -0x78(%rbp), %rdi
movl $0xa0, %esi
callq 0x2140
leaq 0x52c2(%rip), %rdi      # 0xfaf9
movb $0x0, %al
callq 0x2050
movq $0x0, -0x8(%rbp)
jmp 0xa854
jmp 0xa84a
jmp 0xa84c
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2230
nopw (%rax,%rax)