// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq 0x58(%rdi), %rax
movl 0x1c(%rdi), %ecx
cmpl 0x78(%rax), %ecx
jge 0x9478
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movl 0x22c(%rax), %r14d
imull 0x88(%rax), %r14d
imull 0x230(%rax), %r14d
imull 0x234(%rax), %r14d
movss 0xd9b(%rip), %xmm0      # 0xa130
subss 0x25c(%rax), %xmm0
movss %xmm0, 0x14(%rsp)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x28(%rsp), %r12
movq %rax, (%r12)
movl $0x5, %edi
callq 0x2040
movq %rax, %r13
xorl %ebp, %ebp
leaq 0x20(%rsp), %rax
movq %rbp, (%rax)
leaq 0x18(%rsp), %rax
movq %rbp, (%rax)
movq 0x48(%rbx), %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
movq 0x48(%rbx), %rdx
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movl $0xc8, %esi
callq 0x22b0
leaq 0x18e9(%rip), %rsi      # 0xace9
movq %r15, %rdi
movq %r12, %rdx
movq %r13, 0x38(%rsp)
movq %r13, %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0x2160
cmpq %rbp, (%r12)
jns 0x9434
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %r12
testq %r12, %r12
sets %al
movq 0x18(%rsp), %r15
testq %r15, %r15
setle %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x9480
movq 0x4d80(%rip), %rax      # 0xe1d8
movq %rax, 0x38(%rbx)
movl 0x30(%rbx), %eax
movq 0x58(%rbx), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x9478
incl %eax
movl %eax, 0x30(%rbx)
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
jmp 0x9498
xorl %r14d, %r14d
jmp 0x9557
movq 0x28(%rsp), %r13
cmpq 0x8(%rsp), %r13
jle 0x94a4
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
movq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x21c0
jmp 0x9557
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
movss 0x14(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, 0x8(%rsp)
leaq 0x1832(%rip), %rsi      # 0xacf9
movq 0x38(%rsp), %rdi
callq 0x21b0
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movl $0x48, %edi
callq 0x2190
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x14(%rax)
movups %xmm0, 0x24(%rax)
movups %xmm0, 0x34(%rax)
movl $0x0, 0x44(%rax)
movups %xmm0, (%rax)
addq 0x38(%rbx), %r13
movq %r13, 0x8(%rax)
movl 0x8(%rsp), %ecx
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rcx
movq 0x58(%rbx), %rax
movl 0x238(%rax), %esi
movq %rdx, %rax
xorl %edx, %edx
divq %rsi
subl %edx, %ecx
movl %ecx, 0x10(%r14)
movl %r15d, 0x14(%r14)
movl 0x4ca8(%rip), %eax      # 0xe1e0
leal 0x1(%rax), %ecx
movl %ecx, 0x4c9f(%rip)      # 0xe1e0
movl %eax, 0x4(%r14)
movl %ebp, 0x18(%r14)
movq (%rbx), %rax
movq %rax, 0x20(%r14)
movq %r13, 0x4c81(%rip)      # 0xe1d8
movq %r14, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq