// File: narges64[P]SIMBA/ftl.cpp
// Function: create_sub_request(ssd_info*, int, request*, unsigned int, int)
sub_request * create_sub_request( ssd_info * ssd, int lpn,
				 request * req,unsigned int operation, int io_num = -1){

	sub_request * sub = new sub_request(ssd->current_time, lpn,
				ssd->subrequest_sequence_number++, operation);

	if(req!=NULL)
	{
		sub->next_subs = req->subs;
		req->subs = sub;

		sub->app_id = req->app_id;
		sub->io_num = req->io_num;

		sub->state_time[SR_MODE_WAIT] = ssd->current_time - req->time;
		sub->state_current_time = ssd->current_time;
	}
	else
	{
		sub->io_num = io_num;
		sub->state_time[SR_MODE_WAIT] = 0;
		sub->state_current_time = ssd->current_time;
	}

	if (operation == READ)
	{
		sub->ppn = ssd->dram->map->map_entry[lpn].pn;
		if (sub->ppn >= 0) {
			find_location(ssd,sub->ppn, sub->location);
			if (ssd->dram->map->map_entry[lpn].state)
				sub->state = 1;
			else
				sub->state = 0;
		}else {
			sub->state = 0;
		}
	}
	else if(operation == WRITE)
	{
		sub->ppn = -1; // ssd->dram->map->map_entry[lpn].pn;
		sub->state=0;
	}
	else
	{
		delete sub;
		printf("\n FIXME TRIM command \n");
		return NULL;
	}

	return sub;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl $0xa0, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
movl 0xc(%rax), %ecx
movl %ecx, %r8d
incl %r8d
movl %r8d, 0xc(%rax)
movl -0x24(%rbp), %r8d
callq 0x8fa0
jmp 0x6040
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x60d9
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x20(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x88(%rax)
jmp 0x6107
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0xa0, %esi
callq 0x2140
jmp 0x620c
movl -0x28(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x88(%rax)
cmpl $0x1, -0x24(%rbp)
jne 0x619d
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jl 0x6190
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
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
testb $0x1, 0x4(%rax)
je 0x6183
movq -0x30(%rbp), %rax
movl $0x1, 0x40(%rax)
jmp 0x618e
movq -0x30(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0x619b
movq -0x30(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0x61fa
cmpl $0x0, -0x24(%rbp)
jne 0x61bb
movq -0x30(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0x61f8
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x61e0
movq -0x50(%rbp), %rdi
callq 0x8ce0
movq -0x50(%rbp), %rdi
movl $0xa0, %esi
callq 0x2140
leaq 0x946f(%rip), %rdi      # 0xf656
movb $0x0, %al
callq 0x2050
movq $0x0, -0x8(%rbp)
jmp 0x6202
jmp 0x61fa
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2230
nopw %cs:(%rax,%rax)