// File: /workspace/github_cpp_makefile_star2_O3/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 0x8b9f
movq %rsi, %r15
movq 0x8(%rsi), %rcx
cmpq %rax, %rcx
jne 0x8bd6
movq %r15, %rdi
callq 0x9810
movl $0x48, %esi
movq %r15, %rdi
callq 0x21a0
xorl %ebp, %ebp
cmpq %r14, (%rbx)
jae 0x8dfb
movq %r14, (%rbx)
jmp 0x8dfb
movq (%rbx), %rcx
xorl %ebp, %ebp
cmpq %rax, %rcx
je 0x8dfb
cmpq %r14, %rcx
ja 0x8bc6
cmpq %rax, %r14
jne 0x8bc3
movq 0x58(%rbx), %rcx
movl 0x78(%rcx), %ecx
cmpl %ecx, 0x1c(%rbx)
jge 0x8bc6
movq %r14, (%rbx)
xorl %ebp, %ebp
cmpq %rax, %r14
setne %bpl
negl %ebp
jmp 0x8dfb
movq (%rbx), %rsi
cmpq %rcx, %rsi
jg 0x8be4
movq %rcx, (%rbx)
movq %rcx, %rsi
movq %rsi, 0x20(%r15)
cmpq $0x0, 0x68(%rbx)
je 0x8bf9
movq 0x70(%rbx), %rax
movq %r15, 0x38(%rax)
jmp 0x8bfd
movq %r15, 0x68(%rbx)
movq %r15, 0x70(%rbx)
incl 0x1c(%rbx)
imull $0x3afb7e91, 0x4(%r15), %eax # imm = 0x3AFB7E91
rorl $0x4, %eax
movl $0x1, %ebp
cmpl $0x68db8, %eax          # imm = 0x68DB8
ja 0x8dfb
movq 0x98(%rbx), %rax
movq 0x98(%rax), %rcx
addq %rcx, 0xa0(%rax)
movq $0x0, 0x98(%rax)
movq 0x538a(%rip), %r14      # 0xdfd0
movq %r14, %rdi
callq 0x2140
movq %rax, %r12
leaq 0x2042(%rip), %rsi      # 0xac9a
movl $0x1d, %edx
movq %rax, %rdi
callq 0x21e0
movl 0x4(%r15), %esi
movq %r12, %rdi
callq 0x20e0
leaq 0x2040(%rip), %rsi      # 0xacb8
movl $0x8, %edx
movq %r14, %rdi
callq 0x21e0
movq 0x98(%rbx), %rax
movq 0xa0(%rax), %rsi
movq %r14, %rdi
callq 0x2140
leaq 0x201f(%rip), %rsi      # 0xacc1
movl $0xb, %edx
movq %r14, %rdi
callq 0x21e0
movl 0x1c(%rbx), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r15
leaq 0x186f(%rip), %rsi      # 0xa533
movl $0x2, %edx
movq %rax, %rdi
callq 0x21e0
movq 0x58(%rbx), %rax
movl 0x78(%rax), %esi
movq %r15, %rdi
callq 0x22d0
leaq 0x1fe6(%rip), %rsi      # 0xaccd
movl $0xd, %edx
movq %r14, %rdi
callq 0x21e0
movq 0x98(%rbx), %rax
movl 0xc8(%rax), %esi
movq %r14, %rdi
callq 0x22d0
movq 0x58(%rbx), %rax
movq 0x98(%rbx), %rcx
movslq 0xc8(%rcx), %rdx
movslq 0x2d8(%rax), %rsi
imulq $0x14f8b589, %rsi, %rsi # imm = 0x14F8B589
movq %rsi, %rdi
shrq $0x3f, %rdi
sarq $0x2d, %rsi
addl %edi, %esi
movslq 0x29c(%rax), %rdi
imulq $0x14f8b589, %rdi, %rdi # imm = 0x14F8B589
movq %rdi, %r8
shrq $0x3f, %r8
sarq $0x2d, %rdi
addl %r8d, %edi
addl %esi, %edi
movslq %edi, %rsi
imulq %rdx, %rsi
movslq 0x2a4(%rax), %rax
imulq 0xa0(%rcx), %rax
movabsq $0x29f16b11c6d1e109, %r15 # imm = 0x29F16B11C6D1E109
imulq %r15
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xe, %r12
addq %rax, %r12
addq %rsi, %r12
leaq 0x1f49(%rip), %rsi      # 0xacdb
movl $0x12, %edx
movq %r14, %rdi
callq 0x21e0
cvtsi2sd %r12, %xmm0
mulsd 0x1e74(%rip), %xmm0     # 0xac20
movq %r15, %rax
imulq (%rbx)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xe, %rdx
addq %rax, %rdx
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x20f0
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq