// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xce16
movq -0x10(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, (%rax)
jne 0xcdae
movl $0x0, -0x4(%rbp)
jmp 0xd0e2
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
ja 0xcdee
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x78(%rcx), %eax
jl 0xcde1
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x20(%rbp)
jne 0xcde1
jmp 0xcdec
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0xcdee
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x20(%rbp)
jne 0xce0a
movl $0x0, -0x4(%rbp)
jmp 0xd0e2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd0e2
movq -0x18(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, 0x8(%rax)
jne 0xce73
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0xce4f
movq -0x38(%rbp), %rdi
callq 0xe260
movq -0x38(%rbp), %rdi
movl $0x48, %esi
callq 0x2140
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0xce67
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0xd0e2
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jg 0xceb6
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, (%rax)
jne 0xceb4
leaq 0x2dc1(%rip), %rdi      # 0xfc6e
movb $0x0, %al
callq 0x2050
jmp 0xceb6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xd0f0
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x2710, %ecx           # imm = 0x2710
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0xd0db
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq 0x98(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
addq 0xa0(%rax), %rcx
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x70a5(%rip), %rdi      # 0x13fd0
callq 0x2240
movq %rax, %rdi
leaq 0x2d3e(%rip), %rsi      # 0xfc78
callq 0x2110
movq %rax, %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x21c0
movq 0x707b(%rip), %rdi      # 0x13fd0
leaq 0x2d3a(%rip), %rsi      # 0xfc96
callq 0x2110
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xa0(%rax), %rsi
callq 0x2240
movq 0x704e(%rip), %rdi      # 0x13fd0
leaq 0x2d16(%rip), %rsi      # 0xfc9f
callq 0x2110
movq %rax, %rdi
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %esi
callq 0x2220
movq %rax, %rdi
leaq 0x24af(%rip), %rsi      # 0xf456
callq 0x2110
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x78(%rax), %esi
callq 0x2220
movq 0x700a(%rip), %rdi      # 0x13fd0
leaq 0x2cde(%rip), %rsi      # 0xfcab
callq 0x2110
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movl 0xc8(%rax), %esi
callq 0x2220
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movl 0xc8(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x2d8(%rax), %eax
movl $0x186a0, %ecx          # imm = 0x186A0
cltd
idivl %ecx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x29c(%rax), %eax
movl $0x186a0, %esi          # imm = 0x186A0
cltd
idivl %esi
movl %eax, %edx
movl -0x44(%rbp), %eax
addl %edx, %ecx
imull %ecx, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movslq 0x2a4(%rcx), %rcx
imulq %rcx, %rax
movl $0x186a0, %ecx          # imm = 0x186A0
cqto
idivq %rcx
movq %rax, %rcx
movq -0x40(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq 0x6f44(%rip), %rdi      # 0x13fd0
leaq 0x2c26(%rip), %rsi      # 0xfcb9
callq 0x2110
movq %rax, %rdi
cvtsi2sdq -0x28(%rbp), %xmm0
movsd 0x2a9f(%rip), %xmm1     # 0xfb48
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl $0x186a0, %ecx          # imm = 0x186A0
cqto
idivq %rcx
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x2260
movq %rax, %rdi
movq 0x6eea(%rip), %rsi      # 0x13fc0
callq 0x2150
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)