// File: /workspace/github_cpp_makefile_star2_O3/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 0x9358
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movl 0x22c(%rax), %r14d
imull 0x88(%rax), %r14d
imull 0x230(%rax), %r14d
imull 0x234(%rax), %r14d
movss 0xebd(%rip), %xmm0      # 0xa130
subss 0x25c(%rax), %xmm0
movss %xmm0, 0x14(%rsp)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x38(%rsp), %r12
movq %rax, (%r12)
movl $0x5, %edi
callq 0x2040
movq %rax, %rbp
xorl %eax, %eax
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x30(%rsp), %r13
movq %rax, (%r13)
movq 0x48(%rbx), %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movq 0x48(%rbx), %rdx
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movl $0xc8, %esi
callq 0x22b0
leaq 0x1a22(%rip), %rsi      # 0xad01
movq %r15, %rdi
movq %r12, %rdx
movq %rbp, 0x28(%rsp)
movq %rbp, %rcx
leaq 0x18(%rsp), %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x2160
movq (%r12), %r15
testq %r15, %r15
jns 0x9314
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq %r15, 0x38(%rsp)
movq 0x18(%rsp), %r12
testq %r12, %r12
sets %al
movq 0x30(%rsp), %r13
testq %r13, %r13
setle %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x9360
movq 0x4ea0(%rip), %rax      # 0xe1d8
movq %rax, 0x38(%rbx)
movl 0x30(%rbx), %eax
movq 0x58(%rbx), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x9358
incl %eax
movl %eax, 0x30(%rbx)
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
jmp 0x9373
xorl %r14d, %r14d
jmp 0x942f
cmpq 0x8(%rsp), %r15
jle 0x937f
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
movq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x21c0
jmp 0x942f
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
movss 0x14(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, 0x8(%rsp)
leaq 0x196f(%rip), %rsi      # 0xad11
movq 0x28(%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, 0x1c(%rax)
movups %xmm0, 0x2c(%rax)
movups %xmm0, 0x38(%rax)
movq $0x0, (%rax)
addq 0x38(%rbx), %r15
movq %r15, 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 %r13d, 0x14(%r14)
movl 0x4dd0(%rip), %eax      # 0xe1e0
leal 0x1(%rax), %ecx
movl %ecx, 0x4dc7(%rip)      # 0xe1e0
movl %eax, 0x4(%r14)
movl %ebp, 0x18(%r14)
movq (%rbx), %rax
movq %rax, 0x20(%r14)
movq %r15, 0x4da9(%rip)      # 0xe1d8
movq %r14, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq