// File: /workspace/github_cpp_makefile_star2_O2/narges64[P]SIMBA/ssd.cpp
// Function: add_fetched_request(ssd_info*, request*, unsigned long)
int add_fetched_request(ssd_info * ssd, request * request1, uint64_t nearest_event_time) {
	if (request1 == NULL){	// no request before nearest_event_time
		if (ssd->current_time == MAX_INT64)
			return 0;  
		if (ssd->current_time <= nearest_event_time){
			if ((ssd->request_queue_length >= ssd->parameter->queue_length) &&
			 	(nearest_event_time == MAX_INT64)){
			// 	printf("2. HERE %d \n", ssd->request_queue_length);
			}
			else {
				ssd->current_time = nearest_event_time;
			}
		}
		if (nearest_event_time == MAX_INT64) return 0; 
		return -1;
	}

	if (request1->time == MAX_INT64){ // nothing left to read
		delete request1;
		if (ssd->current_time < nearest_event_time)
			ssd->current_time = nearest_event_time;
		return 0;
	}

	if (ssd->current_time <= request1->time) {
		ssd->current_time=request1->time ;
		if (ssd->current_time == MAX_INT64)
			printf("3. HERE \n");
	}
	add_to_request_queue(ssd, request1);
	if (request1->io_num % 10000 == 0){
		ssd->stats->total_flash_erase_count += ssd->stats->flash_erase_count;
		ssd->stats->flash_erase_count = 0;
		cout << ssd->current_time << " fetching io request number: " << request1->io_num ;
		cout << "\terase: " << ssd->stats->total_flash_erase_count ;
		cout << "\tq length: " << ssd->request_queue_length  << "  " << ssd->parameter->queue_length;
		cout << "\tmove count: " << ssd->stats->gc_moved_page;
		int64_t gc_time = ssd->stats->gc_moved_page * (ssd->parameter->time_characteristics.tR/100000 + ssd->parameter->time_characteristics.tPROG/100000) + ssd->stats->total_flash_erase_count * ssd->parameter->time_characteristics.tBERS/100000; 
		int64_t total_time = ssd->current_time; 
		cout << "\tgc contribution: " << (gc_time / 16.0 )/ (ssd->current_time/100000) << endl;  
	}
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rdi, %rbx
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
testq %rsi, %rsi
je 0x7d84
movq %rsi, %r15
movq 0x8(%rsi), %rcx
cmpq %rax, %rcx
jne 0x7dbb
movq %r15, %rdi
callq 0x8836
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x2170
xorl %ebp, %ebp
cmpq %r14, (%rbx)
jae 0x7f47
movq %r14, (%rbx)
jmp 0x7f47
movq (%rbx), %rcx
xorl %ebp, %ebp
cmpq %rax, %rcx
je 0x7f47
cmpq %r14, %rcx
ja 0x7dab
cmpq %rax, %r14
jne 0x7da8
movq 0x58(%rbx), %rcx
movl 0x78(%rcx), %ecx
cmpl %ecx, 0x1c(%rbx)
jge 0x7dab
movq %r14, (%rbx)
xorl %ebp, %ebp
cmpq %rax, %r14
setne %bpl
negl %ebp
jmp 0x7f47
cmpq %rcx, (%rbx)
jg 0x7dc3
movq %rcx, (%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7f52
movl 0x4(%r15), %eax
movl $0x2710, %ecx           # imm = 0x2710
xorl %edx, %edx
divl %ecx
pushq $0x1
popq %rbp
testl %edx, %edx
jne 0x7f47
movq 0x98(%rbx), %rax
movq 0x98(%rax), %rcx
addq %rcx, 0xa0(%rax)
andq $0x0, 0x98(%rax)
movq (%rbx), %rsi
movq 0x61c3(%rip), %r14      # 0xdfd0
movq %r14, %rdi
callq 0x2120
leaq 0x1d66(%rip), %rsi      # 0x9b82
movq %rax, %rdi
callq 0x2140
movl 0x4(%r15), %esi
movq %rax, %rdi
callq 0x20d0
leaq 0x1d69(%rip), %rsi      # 0x9ba0
movq %r14, %rdi
callq 0x2140
movq 0x98(%rbx), %rcx
movq 0xa0(%rcx), %rsi
movq %rax, %rdi
callq 0x2120
leaq 0x1d4d(%rip), %rsi      # 0x9ba9
movq %r14, %rdi
callq 0x2140
movl 0x1c(%rbx), %esi
movq %rax, %rdi
callq 0x22a0
leaq 0x15cd(%rip), %rsi      # 0x9443
movq %rax, %rdi
callq 0x2140
movq 0x58(%rbx), %rcx
movl 0x78(%rcx), %esi
movq %rax, %rdi
callq 0x22a0
leaq 0x1d21(%rip), %rsi      # 0x9bb5
movq %r14, %rdi
callq 0x2140
movq 0x98(%rbx), %rcx
movl 0xc8(%rcx), %esi
movq %rax, %rdi
callq 0x22a0
movq 0x58(%rbx), %rdi
movq 0x98(%rbx), %r8
movslq 0xc8(%r8), %r9
movl 0x29c(%rdi), %ecx
movl 0x2d8(%rdi), %eax
movl $0x186a0, %r10d         # imm = 0x186A0
cltd
idivl %r10d
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %r10d
addl %esi, %eax
movslq %eax, %rcx
imulq %r9, %rcx
movslq 0x2a4(%rdi), %rax
imulq 0xa0(%r8), %rax
movl $0x186a0, %r12d         # imm = 0x186A0
cqto
idivq %r12
movq %rax, %r15
addq %rcx, %r15
leaq 0x1cb2(%rip), %rsi      # 0x9bc3
movq %r14, %rdi
callq 0x2140
movq %rax, %rdi
cvtsi2sd %r15, %xmm0
mulsd 0x1bb7(%rip), %xmm0     # 0x9ae0
movq (%rbx), %rax
cqto
idivq %r12
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x2290
movq %rax, %rdi
callq 0x20b0
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq