// File: narges64[P]SIMBA/ftl.cpp
// Function: distribute(ssd_info*)
ssd_info * distribute(ssd_info *ssd){

	if (ssd->dram != NULL)
		ssd->dram->current_time=ssd->current_time;

	request * req = ssd->request_tail; // The request we want to distribute
	if (req->size == 0) {
		cout << "size zero " << endl; 
		return ssd; 
	}
	unsigned lsn  = req->lsn;
	unsigned last_lpn  = (req->lsn+req->size-1)/ssd->parameter->subpage_page;
	unsigned first_lpn = req->lsn/ssd->parameter->subpage_page;
	unsigned lpn  = first_lpn;

	if (req->operation == READ)
	{
		while(lpn<=last_lpn)
		{
			sub_request * sub     = create_sub_request(ssd,lpn, req,req->operation, req->io_num);
			if (service_in_buffer(ssd, sub) != SUCCESS)
				printf("Error in servicing a request in buffer \n");

			lpn++;
		}
	}
	else if(req->operation==WRITE)
	{
		while (lpn<=last_lpn)
		{
			sub_request * sub=create_sub_request(ssd,lpn, req,req->operation, req->io_num);
			if (service_in_buffer(ssd, sub) != SUCCESS)
				printf("Error in servicing write requset in buffer! \n");
			lpn++;
		}
	} else {
		// FIXME for TRIM
	}
	return ssd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x5e7a
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x5ebf
movq 0xe139(%rip), %rdi      # 0x13fd0
leaq 0x96cd(%rip), %rsi      # 0xf56b
callq 0x2110
movq %rax, %rdi
movq 0xe113(%rip), %rsi      # 0x13fc0
callq 0x2150
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x5fe1
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x18(%rbp), %rcx
addl 0x14(%rcx), %eax
subl $0x1, %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
xorl %edx, %edx
divl 0x238(%rcx)
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
xorl %edx, %edx
divl 0x238(%rcx)
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x5f71
jmp 0x5f19
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
ja 0x5f6f
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
movl 0x4(%rax), %r8d
callq 0x5ff0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x6220
cmpl $0x1, %eax
je 0x5f64
leaq 0x9619(%rip), %rdi      # 0xf576
movb $0x0, %al
callq 0x2050
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5f19
jmp 0x5fd9
movq -0x18(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x5fd5
jmp 0x5f7d
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
ja 0x5fd3
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
movl 0x4(%rax), %r8d
callq 0x5ff0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x6220
cmpl $0x1, %eax
je 0x5fc8
leaq 0x95de(%rip), %rdi      # 0xf59f
movb $0x0, %al
callq 0x2050
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5f7d
jmp 0x5fd7
jmp 0x5fd7
jmp 0x5fd9
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)