// File: narges64[P]SIMBA/ssd.cpp
// Function: read_request_from_file(ssd_info*, long)
request * read_request_from_file(ssd_info * ssd, int64_t nearest_event_time){

	static int64_t last_request_time = 0; 

	unsigned int total_size = ssd->parameter->lun_num * ssd->parameter->plane_lun *
						ssd->parameter->block_plane * ssd->parameter->page_block;
	total_size = total_size * (1-ssd->parameter->overprovide);

	if (ssd->request_queue_length >= ssd->parameter->queue_length){
		return NULL;
	}	

	static int io_num = 0;
	int64_t time_tt = MAX_INT64;
	int unused;
	char * type = new char[5];
	int64_t lsn=0;
	long int size = 0; 
	int app_id = 0;
	
	long filepoint;
	filepoint= ftell(ssd->tracefile);
	
	char buffer[200];
	fgets(buffer, 200, ssd->tracefile);
	// sscanf(buffer,"%d %lld %d %d %s %d %ld %d",&io_num,&time_tt,&unused,&unused,type,&lsn,&size,&app_id);
	sscanf(buffer,"%lld %s %d %ld ",&time_tt,type,&lsn,&size);
	
	if (time_tt != MAX_INT64){
		if (time_tt > MAX_INT64 || time_tt < 0)
			time_tt = MAX_INT64;
	}
	

	if ((lsn < 0) || (size <= 0))
	{
		ssd->last_times = last_request_time; 
		if (ssd->repeat_times < ssd->parameter->repeat_trace){	 
			ssd->repeat_times++; 
			fseek(ssd->tracefile,0, SEEK_SET);
		}
		
		return NULL;
	}


	// if next request is not before the next event, we discard it 	
	if (time_tt > nearest_event_time ){
		fseek(ssd->tracefile,filepoint,0);
		return NULL;
	}

	int ope; 
	if (strcasecmp(type, "Read") == 0)
		ope = 1;
	else
		ope = 0;

	request * request1 = new request();

	request1->app_id = app_id;
	request1->time = ssd->last_times + time_tt;
	request1->lsn = (int64_t)((lsn % total_size) / ssd->parameter->subpage_page ) * ssd->parameter->subpage_page;
	request1->size = size;
	request1->io_num = io_num++;
	request1->operation = ope;
	request1->begin_time = ssd->current_time; 

	last_request_time = request1->time; 

	return request1;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movl 0x88(%rcx), %eax
movl 0x22c(%rcx), %edx
imull %edx, %eax
movl 0x230(%rcx), %edx
imull %edx, %eax
movl 0x234(%rcx), %ecx
imull %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cvtsi2ss %rax, %xmm0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movss 0x25c(%rax), %xmm2
movss 0x1791(%rip), %xmm1     # 0xf010
subss %xmm2, %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x78(%rcx), %eax
jl 0xd8b0
movq $0x0, -0x8(%rbp)
jmp 0xdb3c
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, -0x28(%rbp)
movl $0x5, %edi
callq 0x2040
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2030
movq %rax, -0x58(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdx
movl $0xc8, %esi
callq 0x2210
leaq -0x120(%rbp), %rdi
movq -0x38(%rbp), %rcx
leaq 0x23c0(%rip), %rsi      # 0xfcdf
leaq -0x28(%rbp), %rdx
leaq -0x40(%rbp), %r8
leaq -0x48(%rbp), %r9
movb $0x0, %al
callq 0x2100
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x28(%rbp)
je 0xd969
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x28(%rbp)
jg 0xd959
cmpq $0x0, -0x28(%rbp)
jge 0xd967
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, -0x28(%rbp)
jmp 0xd969
cmpq $0x0, -0x40(%rbp)
jl 0xd977
cmpq $0x0, -0x48(%rbp)
jg 0xd9c7
movq 0x6812(%rip), %rcx      # 0x14190
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x6c(%rcx), %eax
jge 0xd9ba
movq -0x10(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2170
movq $0x0, -0x8(%rbp)
jmp 0xdb3c
movq -0x28(%rbp), %rax
cmpq -0x18(%rbp), %rax
jle 0xd9f1
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x2170
movq $0x0, -0x8(%rbp)
jmp 0xdb3c
movq -0x38(%rbp), %rdi
leaq 0x22f3(%rip), %rsi      # 0xfcef
callq 0x2160
cmpl $0x0, %eax
jne 0xda12
movl $0x1, -0x124(%rbp)
jmp 0xda1c
movl $0x0, -0x124(%rbp)
movl $0x48, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x148(%rbp)
callq 0xe2e0
jmp 0xda3a
movq -0x148(%rbp), %rax
movq %rax, -0x130(%rbp)
movl -0x4c(%rbp), %ecx
movq -0x130(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
addq -0x28(%rbp), %rcx
movq -0x130(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movl -0x1c(%rbp), %ecx
cqto
idivq %rcx
movq %rdx, %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
cqto
idivq %rcx
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
imulq %rcx, %rax
movl %eax, %ecx
movq -0x130(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x130(%rbp), %rax
movl %ecx, 0x14(%rax)
movl 0x66d7(%rip), %ecx      # 0x14198
movl %ecx, %eax
addl $0x1, %eax
movl %eax, 0x66cc(%rip)      # 0x14198
movq -0x130(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x124(%rbp), %ecx
movq -0x130(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x130(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x130(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x6686(%rip)      # 0x14190
movq -0x130(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xdb3c
movq -0x148(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
movl $0x48, %esi
callq 0x2140
jmp 0xdb49
movq -0x8(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x138(%rbp), %rdi
callq 0x2230
nopw %cs:(%rax,%rax)