// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, 0x48(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movq 0x58(%rdi), %rax
movl 0x2cc(%rax), %r15d
movl 0x22c(%rax), %ecx
movl 0x240(%rax), %esi
imull 0x238(%rax), %esi
movl 0x2f0(%rax), %edx
movq %rsi, 0x28(%rsp)
imull %esi, %edx
movq %rdx, 0x30(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movslq 0x29c(%rax), %rax
movq %rax, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
leaq (,%rcx,8), %r13
movq %r13, %rdi
callq 0x2040
movq %rax, 0x20(%rsp)
callq 0x20a0
movl %eax, %ebx
movl %ebp, 0xc(%rsp)
movl %ebp, %ebp
movq 0x90(%r14), %rax
movq (%rax,%rbp,8), %rax
movl (%rax), %r12d
testl %r12d, %r12d
je 0x5399
leal (,%r15,8), %eax
subl %r15d, %eax
movq 0x30(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x30(%rsp)
movq 0x28(%rsp), %rax
addl $0x7, %eax
imull %r15d, %eax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x38(%rsp)
je 0x531a
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20d0
leal (%rbx,%r15), %eax
xorl %edx, %edx
divl %r12d
movl %edx, %r12d
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x6bbc
movq 0x90(%r14), %rcx
testl %eax, %eax
jne 0x5386
movq (%rcx,%rbp,8), %rax
movq 0x50(%rax), %rax
movl %r12d, %edx
movq (%rax,%rdx,8), %rax
cmpb $0x0, 0x98(%rax)
jne 0x5386
movq %rdx, 0x50(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movq 0x20(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x591e
testl %eax, %eax
jne 0x53b1
movq 0x90(%r14), %rcx
incl %r15d
movq (%rcx,%rbp,8), %rax
movl (%rax), %r12d
cmpl %r12d, %r15d
jb 0x5304
movq 0x20(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
je 0x53e3
cmpl $0x1, %ecx
jne 0x5410
cmpl $0x1, %eax
je 0x53d1
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movl %eax, %eax
imulq 0x30(%rsp), %rax
movq %rax, %r15
addq %rax, 0x10(%rsp)
jmp 0x545e
cmpl $0x1, %eax
je 0x53f6
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movl %eax, %ecx
imulq 0x28(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, %r15
addq %rcx, %rax
movq %rax, 0x10(%rsp)
jmp 0x545e
movq 0x8bb9(%rip), %rbx      # 0xdfd0
leaq 0x5282(%rip), %rsi      # 0xa6a0
movl $0x29, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %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
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x38(%rsp)
je 0x5527
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
movq (%rax,%r13,8), %rbx
testq %rbx, %rbx
je 0x5519
movq (%r14), %rcx
movq 0x10(%rsp), %rax
leaq (%rcx,%rax), %r9
movq %r9, 0x58(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
movl $0x9, %r8d
callq 0x6d7e
movq 0x60(%rbx), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movq (%r14), %r9
movq %r14, %rdi
movl $0x2, %r8d
pushq 0x58(%rbx)
pushq $0x0
callq 0x6c9c
addq $0x10, %rsp
cmpb $0x1, 0x98(%rbx)
jne 0x54f2
movq 0x90(%r14), %rax
movq (%rax,%rbp,8), %rax
movq 0x50(%rax), %rax
movq 0x50(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x1, 0x98(%rax)
cmpq $0x0, 0x18(%rbx)
je 0x5519
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5b24
movq %rbx, %rdi
callq 0x65d0
movl $0xa0, %esi
movq %rbx, %rdi
callq 0x21a0
incq %r13
cmpq %r13, 0x38(%rsp)
jne 0x546c
movq (%r14), %r8
movq 0x10(%rsp), %rax
addq %r8, %rax
subq $0x8, %rsp
movq %r14, %rdi
movl 0x14(%rsp), %ebx
movl %ebx, %esi
movl %r12d, %edx
movl $0x2, %ecx
xorl %r9d, %r9d
pushq %rax
callq 0x6c38
addq $0x10, %rsp
movq (%r14), %rcx
addq %rcx, %r15
movq %r14, %rdi
movl %ebx, %esi
movl $0x2, %edx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x6d08
movq 0x48(%rsp), %rax
movl $0x1, (%rax)
jmp 0x5399