// File: narges64[P]SIMBA/ssd.cpp
// Function: generate_next_request(ssd_info*, long)
request * generate_next_request(ssd_info * ssd, int64_t nearest_event_time){
	if (ssd->request_queue_length >= ssd->parameter->queue_length){
		return NULL;
	}

	static uint64_t previous_time = 0;
	double rd_ratio = ssd->parameter->syn_rd_ratio;
	int lun_number = ssd->parameter->lun_num;
	uint64_t min_sector_address = 0;
	uint64_t max_sector_address =(long int)(((long int)ssd->parameter->subpage_page*ssd->parameter->page_block*ssd->parameter->block_plane*ssd->parameter->plane_lun*ssd->parameter->lun_num)*(1-ssd->parameter->overprovide));

	uint64_t size = ssd->parameter->syn_req_size;
	uint64_t avg_time = ssd->parameter->syn_interarrival_mean; 
	int event_count = ssd->parameter->syn_req_count;

	// Time
 	uint64_t new_time = 0;
 	uint64_t time_interval = expo_dist(avg_time);//, 1);
 	new_time = previous_time  + time_interval;
	if (new_time < ssd->current_time) new_time = ssd->current_time;

	if (new_time > nearest_event_time)
		return NULL;

 	previous_time = new_time;
	request * request1 = new request();
	request1->app_id = 1;
	request1->io_num = ssd->request_sequence_number;
	request1->time = new_time;

 	// Operation
 	int r = rand() % 100;
 	if (r < (rd_ratio*100)) {
		request1->operation = 1; // Read
 	}else {
		request1->operation = 0; // Write
 	}

 	// Address
	int align = ssd->parameter->subpage_page; 
 	uint64_t address = (rand() % (max_sector_address - min_sector_address)) + min_sector_address ;
	request1->lsn = address;

 	// Size
 	int req_size = size;
	request1->size = size;

	if (request1->io_num > event_count) {
		request1->time = MAX_INT64;
	}else {
		ssd->request_sequence_number++;
	}
	return request1;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x78(%rcx), %eax
jl 0xd384
movq $0x0, -0x8(%rbp)
jmp 0xd62b
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movss 0x324(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x88(%rax), %eax
movl %eax, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x238(%rax), %ecx
movl 0x234(%rax), %edx
imulq %rdx, %rcx
movl 0x230(%rax), %edx
imulq %rdx, %rcx
movl 0x88(%rax), %edx
movl 0x22c(%rax), %esi
imulq %rsi, %rcx
imulq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
movss 0x25c(%rax), %xmm2
movss 0x1c0f(%rip), %xmm1     # 0xf010
subss %xmm2, %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x32c(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x330(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x328(%rax), %eax
movl %eax, -0x4c(%rbp)
movq $0x0, -0x58(%rbp)
movaps 0x2738(%rip), %xmm1     # 0xfb90
movq -0x48(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x2737(%rip), %xmm1     # 0xfba0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
callq 0xd2e0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x26ec(%rip), %xmm1     # 0xfb80
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x60(%rbp)
movq 0x6cda(%rip), %rax      # 0x14188
addq -0x60(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xd4ce
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0xd4e5
movq $0x0, -0x8(%rbp)
jmp 0xd62b
movq -0x58(%rbp), %rax
movq %rax, 0x6c98(%rip)      # 0x14188
movl $0x48, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
callq 0xe2e0
jmp 0xd50e
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
callq 0x2090
movl $0x64, %ecx
cltd
idivl %ecx
movl %edx, -0x78(%rbp)
cvtsi2sdl -0x78(%rbp), %xmm1
movsd 0x1aae(%rip), %xmm0     # 0xf008
mulsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd594
movq -0x68(%rbp), %rax
movl $0x1, 0x18(%rax)
jmp 0xd59f
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x48, %esi
callq 0x2140
jmp 0xd638
movq -0x68(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x238(%rax), %eax
movl %eax, -0x7c(%rbp)
callq 0x2090
cltq
movq -0x38(%rbp), %rcx
subq -0x30(%rbp), %rcx
xorl %edx, %edx
divq %rcx
addq -0x30(%rbp), %rdx
movq %rdx, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl %eax, %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x40(%rbp), %rax
movl %eax, -0x8c(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x4c(%rbp), %eax
jbe 0xd616
movq -0x68(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
movq %rcx, 0x8(%rax)
jmp 0xd623
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2230
nopw %cs:(%rax,%rax)