// File: narges64[P]SIMBA/ftl.cpp
// Function: services_2_io(ssd_info*, unsigned int, unsigned int*)
void services_2_io(ssd_info * ssd, unsigned int channel, unsigned int * channel_busy_flag){
	int64_t read_transfer_time = 7 * ssd->parameter->time_characteristics.tWC +
			(ssd->parameter->subpage_page * ssd->parameter->subpage_capacity) *
			ssd->parameter->time_characteristics.tRC;
	int64_t read_time = ssd->parameter->time_characteristics.tR;
	int64_t write_transfer_time = 7 * ssd->parameter->time_characteristics.tWC +
			(ssd->parameter->subpage_page * ssd->parameter->subpage_capacity) *
			ssd->parameter->time_characteristics.tWC;
	int64_t write_time = ssd->parameter->time_characteristics.tPROG;
	int64_t channel_busy_time = 0;
	int64_t lun_busy_time = 0;
	sub_request ** subs;
	unsigned int subs_count = 0;
	unsigned int max_subs_count = ssd->parameter->plane_lun;
	subs = new sub_request *[max_subs_count];
	unsigned int lun;
	int random = rand();
	for (unsigned int c = 0; c < ssd->channel_head[channel]->lun_num; c++){
		for (int i = 0; i < max_subs_count; i++) subs[i] = NULL;
		subs_count = 0;
		lun = (c + random ) % ssd->channel_head[channel]->lun_num;
		if (find_lun_state(ssd , channel, lun) == LUN_MODE_IDLE &&
			ssd->channel_head[channel]->lun_head[lun]->GCMode == false){
			int operation = -1;
			subs_count = find_lun_io_requests(ssd, channel, lun, subs, &operation);
			if ( subs_count  == 0 ) continue;

			// Process collected sub requests
			switch (operation){
				case READ:
					if (subs_count > 1) ssd->stats->read_multiplane_count++;
					channel_busy_time = subs_count * read_transfer_time;
					lun_busy_time = channel_busy_time + read_time;
					break;
				case WRITE:
					if (subs_count > 1) ssd->stats->write_multiplane_count++;
					channel_busy_time = subs_count * write_transfer_time;
					lun_busy_time = channel_busy_time + write_time;
					break;
				default:
					cout << "Error: wrong operation (cannot be erase) " << endl;
			}

			for (int i = 0; i < max_subs_count; i++){
				if (subs[i] == NULL) continue;
				subs[i]->complete_time = ssd->current_time + lun_busy_time;
				change_subrequest_state (ssd, subs[i],
						SR_MODE_ST_S, ssd->current_time,
						SR_MODE_COMPLETE , subs[i]->complete_time);
				change_plane_state(ssd, subs[i]->location->channel,  
						subs[i]->location->lun, subs[i]->location->plane,
						PLANE_MODE_IO, ssd->current_time,
						PLANE_MODE_IDLE, subs[i]->complete_time);

				if (subs[i]->trigger_gc) {
					ssd->channel_head[channel]->lun_head[lun]->GCMode = true;
				}
				if (subs[i]->buf_entry != NULL){
					write_cleanup(ssd, subs[i]);

					delete subs[i];
				}
			}

			if (subs_count > 0){
				change_lun_state (ssd, channel, lun, LUN_MODE_IO,
						ssd->current_time, LUN_MODE_IDLE,
						ssd->current_time + lun_busy_time);
				change_channel_state(ssd, channel, CHANNEL_MODE_IO,
						ssd->current_time, CHANNEL_MODE_IDLE,
						ssd->current_time + channel_busy_time);
				*channel_busy_flag = 1;
				break;
			}
		}
	}
	delete [] subs;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
imull $0x7, 0x2cc(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x240(%rdx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x2f0(%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x2d8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
imull $0x7, 0x2cc(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x240(%rdx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x2cc(%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x29c(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x22c(%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, -0x50(%rbp)
callq 0x2090
movl %eax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0xc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
cmpl (%rcx), %eax
jae 0x6dd2
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x6a88
movq -0x50(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x6a65
movl $0x0, -0x54(%rbp)
movl -0x64(%rbp), %eax
addl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0xc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
divl (%rcx)
movl %edx, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
callq 0x9660
cmpl $0x0, %eax
jne 0x6dc2
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movl -0xc(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x5c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movb 0x98(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x6dc2
movl $0xffffffff, -0x6c(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
movq -0x50(%rbp), %rcx
leaq -0x6c(%rbp), %r8
callq 0x73b0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x6b23
jmp 0x6dc4
movl -0x6c(%rbp), %eax
movl %eax, -0x74(%rbp)
testl %eax, %eax
je 0x6b76
jmp 0x6b2f
movl -0x74(%rbp), %eax
subl $0x1, %eax
jne 0x6bb3
jmp 0x6b39
cmpl $0x1, -0x54(%rbp)
jbe 0x6b5c
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xe0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xe0(%rax)
movl -0x54(%rbp), %eax
imulq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x6bd5
cmpl $0x1, -0x54(%rbp)
jbe 0x6b99
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xe8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xe8(%rax)
movl -0x54(%rbp), %eax
imulq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x6bd5
movq 0xd416(%rip), %rdi      # 0x13fd0
leaq 0x8a1e(%rip), %rsi      # 0xf5df
callq 0x2110
movq %rax, %rdi
movq 0xd3f0(%rip), %rsi      # 0x13fc0
callq 0x2150
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x6d5b
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x6bfc
jmp 0x6d4d
movq -0x8(%rbp), %rax
movq (%rax), %rcx
addq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq 0x58(%rax), %r9
movl $0x1, %edx
movl $0x9, %r8d
callq 0x9dd0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl (%rax), %esi
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x4(%rax), %edx
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x8(%rax), %ecx
movq -0x8(%rbp), %rax
movq (%rax), %r9
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %r8
movq (%rax,%r8,8), %rax
movq 0x58(%rax), %rax
movl $0x2, %r8d
xorl %r10d, %r10d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9970
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
testb $0x1, 0x98(%rax)
je 0x6cf4
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movl -0xc(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl -0x5c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movb $0x1, 0x98(%rax)
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x18(%rax)
je 0x6d4b
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x76a0
movq -0x50(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x6d49
movq -0x80(%rbp), %rdi
callq 0x8ce0
movq -0x80(%rbp), %rdi
movl $0xa0, %esi
callq 0x2140
jmp 0x6d4b
jmp 0x6d4d
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x6bdc
cmpl $0x0, -0x54(%rbp)
jbe 0x6dc0
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
movq -0x8(%rbp), %rax
movq (%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq -0x48(%rbp), %rax
movl $0x2, %ecx
xorl %r9d, %r9d
movq %rax, (%rsp)
callq 0x9780
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %r9
addq -0x40(%rbp), %r9
movl $0x2, %edx
xorl %r8d, %r8d
callq 0x9bc0
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x6dd2
jmp 0x6dc2
jmp 0x6dc4
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6a41
movq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x6def
movq -0x88(%rbp), %rdi
callq 0x21b0
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)