// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x38(%rsp)
movl %esi, %r12d
movq %rdi, %r14
movq 0x58(%rdi), %rax
movl 0x2cc(%rax), %ecx
imull $0x7, %ecx, %edx
movl 0x22c(%rax), %ebp
movl 0x240(%rax), %edi
imull 0x238(%rax), %edi
movl 0x2f0(%rax), %esi
imull %edi, %esi
addl %edx, %esi
movq %rsi, 0x8(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x10(%rsp)
addl $0x7, %edi
imull %ecx, %edi
movq %rdi, 0x30(%rsp)
movslq 0x29c(%rax), %rax
movq %rax, 0x28(%rsp)
leaq (,%rbp,8), %rdi
callq 0x2040
movq %rax, %r15
callq 0x2090
movl %eax, %ebx
movl %r12d, 0x4(%rsp)
movl %r12d, %eax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x90(%r14), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %ecx
cmpl %ecx, %r13d
jae 0x4efe
xorl %eax, %eax
cmpq %rax, %rbp
je 0x4cf8
andq $0x0, (%r15,%rax,8)
incq %rax
jmp 0x4ce9
leal (%rbx,%r13), %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r12d
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x61b8
testl %eax, %eax
jne 0x4d5c
movq 0x90(%r14), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl %r12d, %ecx
movq (%rax,%rcx,8), %rax
cmpb $0x0, 0x98(%rax)
jne 0x4d5c
movq %rcx, 0x40(%rsp)
orl $-0x1, 0x1c(%rsp)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
movq %r15, %rcx
leaq 0x1c(%rsp), %r8
callq 0x5259
testl %eax, %eax
jne 0x4d64
incl %r13d
jmp 0x4ccc
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
je 0x4d98
cmpl $0x1, %ecx
jne 0x4dc7
cmpl $0x1, %eax
je 0x4d84
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movl %eax, %eax
imulq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
addq %rax, 0x10(%rsp)
jmp 0x4df4
cmpl $0x1, %eax
je 0x4dab
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movl %eax, %eax
imulq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
jmp 0x4df4
movq 0x9202(%rip), %rdi      # 0xdfd0
leaq 0x47f8(%rip), %rsi      # 0x95cd
callq 0x2140
movq %rax, %rdi
callq 0x20b0
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x4eae
movq (%r15,%rbx,8), %r13
testq %r13, %r13
je 0x4ea6
movq (%r14), %rcx
movq 0x10(%rsp), %rax
leaq (%rcx,%rax), %r9
movq %r9, 0x58(%r13)
movq %r14, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
pushq $0x9
popq %r8
callq 0x637a
movq 0x60(%r13), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movq (%r14), %r9
movq %r14, %rdi
pushq $0x2
popq %r8
pushq 0x58(%r13)
pushq $0x0
callq 0x6298
popq %rax
popq %rcx
cmpb $0x1, 0x98(%r13)
jne 0x4e7f
movq 0x90(%r14), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq 0x40(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x1, 0x98(%rax)
cmpq $0x0, 0x18(%r13)
je 0x4ea6
movq %r14, %rdi
movq %r13, %rsi
callq 0x542b
movq %r13, %rdi
callq 0x5d62
movl $0xa0, %esi
movq %r13, %rdi
callq 0x2170
incq %rbx
jmp 0x4df6
movq (%r14), %r8
movq 0x10(%rsp), %rax
addq %r8, %rax
subq $0x8, %rsp
pushq $0x2
popq %rbx
movq %r14, %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
movl %r12d, %edx
movl %ebx, %ecx
xorl %r9d, %r9d
pushq %rax
callq 0x6234
popq %rax
popq %rcx
movq (%r14), %rcx
movq 0x8(%rsp), %r9
addq %rcx, %r9
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
xorl %r8d, %r8d
callq 0x6304
movq 0x38(%rsp), %rax
movl $0x1, (%rax)
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2200