// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rdx, 0x58(%rsp)
movl %esi, %r13d
movq %rdi, %r14
movq 0x58(%rdi), %rax
movl 0x2cc(%rax), %ebx
movl 0x22c(%rax), %ecx
movl 0x240(%rax), %edx
imull 0x238(%rax), %edx
movl 0x2f0(%rax), %esi
movq %rdx, %r15
imull %edx, %esi
movq %rsi, 0x20(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movslq 0x29c(%rax), %rax
movq %rax, 0x48(%rsp)
movq %rcx, 0x30(%rsp)
leaq (,%rcx,8), %rdi
movq %rdi, 0x68(%rsp)
callq 0x2040
movq %rax, %r12
callq 0x20a0
movq %rax, 0x70(%rsp)
movl %r13d, %ecx
movq 0x90(%r14), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %rcx
cmpl $0x0, (%rcx)
je 0x55d8
leal (,%rbx,8), %ecx
subl %ebx, %ecx
movq 0x20(%rsp), %rdx
addl %ecx, %edx
movq %rdx, 0x20(%rsp)
addl $0x7, %r15d
imull %ebx, %r15d
movq %r15, 0x40(%rsp)
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
xorl %ebp, %ebp
movq $0x0, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movl %r13d, 0x3c(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x5366
movq %r12, %rdi
xorl %esi, %esi
movq 0x68(%rsp), %rdx
callq 0x20d0
movq 0x70(%rsp), %rax
addl %ebp, %eax
movq (%rbx), %rcx
xorl %edx, %edx
divl (%rcx)
movl %edx, %ebx
movq %r14, %rdi
movl %r13d, %esi
callq 0x6ccc
testl %eax, %eax
jne 0x55bb
movq 0x90(%r14), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movl %ebx, %r15d
movq (%rax,%r15,8), %rax
cmpb $0x0, 0x98(%rax)
jne 0x55bb
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
movq %r12, %rcx
leaq 0xc(%rsp), %r8
callq 0x5997
testl %eax, %eax
je 0x5406
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
movq %r15, 0x60(%rsp)
je 0x5411
cmpl $0x1, %ecx
jne 0x5440
cmpl $0x1, %eax
je 0x53f7
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movl %eax, %edx
imulq 0x20(%rsp), %rdx
movq 0x50(%rsp), %rax
jmp 0x5431
movl $0x4, %r15d
jmp 0x55b5
cmpl $0x1, %eax
je 0x5424
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movl %eax, %edx
imulq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rax
movq %rdx, 0x28(%rsp)
addq %rdx, %rax
movq %rax, 0x10(%rsp)
jmp 0x5482
movl $0x29, %edx
movq 0x8b84(%rip), %r15      # 0xdfd0
movq %r15, %rdi
leaq 0x524a(%rip), %rsi      # 0xa6a0
callq 0x21e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x20f0
cmpq $0x0, 0x30(%rsp)
je 0x5557
xorl %r13d, %r13d
movq (%r12,%r13,8), %rsi
testq %rsi, %rsi
je 0x5549
movq (%r14), %rcx
movq 0x10(%rsp), %rax
leaq (%rcx,%rax), %r9
movq %r9, 0x58(%rsi)
movq %r14, %rdi
movl $0x1, %edx
movl $0x9, %r8d
callq 0x6e8e
movq (%r12,%r13,8), %rax
movq 0x60(%rax), %rcx
movl (%rcx), %esi
movl 0x4(%rcx), %edx
movl 0x8(%rcx), %ecx
movq (%r14), %r9
movq %r14, %rdi
movl $0x2, %r8d
pushq 0x58(%rax)
pushq $0x0
callq 0x6dac
addq $0x10, %rsp
movq (%r12,%r13,8), %rsi
cmpb $0x1, 0x98(%rsi)
jne 0x551c
movq 0x90(%r14), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x1, 0x98(%rax)
cmpq $0x0, 0x18(%rsi)
je 0x5549
movq %r14, %rdi
callq 0x5ba9
movq (%r12,%r13,8), %r15
testq %r15, %r15
je 0x5549
movq %r15, %rdi
callq 0x66d0
movl $0xa0, %esi
movq %r15, %rdi
callq 0x21a0
incq %r13
cmpq %r13, 0x30(%rsp)
jne 0x5491
movq (%r14), %r8
movq 0x10(%rsp), %rax
addq %r8, %rax
subq $0x8, %rsp
movl $0x2, %r15d
movq %r14, %rdi
movl 0x44(%rsp), %r13d
movl %r13d, %esi
movl %ebx, %edx
movl $0x2, %ecx
xorl %r9d, %r9d
pushq %rax
callq 0x6d48
addq $0x10, %rsp
movq (%r14), %rcx
movq 0x28(%rsp), %rax
leaq (%rcx,%rax), %r9
movq %r14, %rdi
movl %r13d, %esi
movl $0x2, %edx
xorl %r8d, %r8d
callq 0x6e18
movq 0x58(%rsp), %rax
movl $0x1, (%rax)
testb $0x3, %r15b
jne 0x55d8
incl %ebp
movq 0x90(%r14), %rax
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
movq (%rbx), %rax
cmpl (%rax), %ebp
jb 0x534f
movq %r12, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230