// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movq 0x58(%rdi), %rax
movl 0x1c(%rdi), %ecx
cmpl 0x78(%rax), %ecx
jge 0x843d
movq %rdi, %rbx
movq %rsi, (%rsp)
movl 0x22c(%rax), %r15d
imull 0x88(%rax), %r15d
imull 0x230(%rax), %r15d
imull 0x234(%rax), %r15d
movss 0xce4(%rip), %xmm0      # 0x9040
subss 0x25c(%rax), %xmm0
movss %xmm0, 0xc(%rsp)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x28(%rsp), %r12
movq %rax, (%r12)
pushq $0x5
popq %rdi
callq 0x2040
movq %rax, %rbp
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
leaq 0x20(%rsp), %r13
andq $0x0, (%r13)
movq 0x48(%rbx), %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq 0x48(%rbx), %rdx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movl $0xc8, %esi
callq 0x2280
leaq 0x1823(%rip), %rsi      # 0x9be9
movq %r14, %rdi
movq %rbp, %r14
movq %r12, %rdx
movq %rbp, %rcx
leaq 0x10(%rsp), %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x2130
movq (%r12), %r13
testq %r13, %r13
jns 0x83f9
movabsq $0x7fffffffffffffff, %r13 # imm = 0x7FFFFFFFFFFFFFFF
movq %r13, 0x28(%rsp)
movq 0x10(%rsp), %r12
testq %r12, %r12
sets %al
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
setle %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x8445
movq 0x5da3(%rip), %rax      # 0xe1c0
movq %rax, 0x38(%rbx)
movl 0x30(%rbx), %eax
movq 0x58(%rbx), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x843d
incl %eax
movl %eax, 0x30(%rbx)
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
jmp 0x8457
xorl %r14d, %r14d
jmp 0x8508
cmpq (%rsp), %r13
jle 0x8463
movq 0x48(%rbx), %rdi
xorl %r14d, %r14d
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x21a0
jmp 0x8508
cvtsi2ss %r15, %xmm0
movss 0xc(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, (%rsp)
leaq 0x1777(%rip), %rsi      # 0x9bf9
movq %r14, %rdi
callq 0x2190
xorl %r15d, %r15d
testl %eax, %eax
sete %r15b
pushq $0x48
popq %rdi
callq 0x2160
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rax)
movups %xmm0, 0x2c(%rax)
movups %xmm0, 0x38(%rax)
andq $0x0, (%rax)
addq 0x38(%rbx), %r13
movq %r13, 0x8(%rax)
movl (%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 %ebp, 0x14(%r14)
movl 0x5cdf(%rip), %eax      # 0xe1c8
leal 0x1(%rax), %ecx
movl %ecx, 0x5cd6(%rip)      # 0xe1c8
movl %eax, 0x4(%r14)
movl %r15d, 0x18(%r14)
movq (%rbx), %rax
movq %rax, 0x20(%r14)
movq %r13, 0x5cb8(%rip)      # 0xe1c0
movq %r14, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq