// File: /workspace/github_cpp_makefile_star2_O2/narges64[P]SIMBA/ftl.cpp
// Function: services_2_gc(ssd_info*, unsigned int, unsigned int*)
void services_2_gc(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->gc_time_ratio * ssd->parameter->time_characteristics.tPROG;
	int64_t erase_transfer_time = 5 * ssd->parameter->time_characteristics.tWC;
	int64_t erase_time = ssd->parameter->time_characteristics.tBERS;

	sub_request ** subs = new sub_request * [ssd->parameter->plane_lun];

	int subs_count = 0;
	int random = rand() % ssd->channel_head[channel]->lun_num;
	unsigned int lun = 0;
	for (unsigned int c = 0; c < ssd->channel_head[channel]->lun_num; c++){
		// select the order of luns randomly
		lun = (c + random) % ssd->channel_head[channel]->lun_num;

		if (find_lun_state(ssd , channel, lun) != LUN_MODE_IDLE ) continue;
			// ssd->channel_head[channel]->lun_head[lun]->GCMode != true) continue;
		// collect sub_requests for different planes
		for (int i = 0; i < ssd->parameter->plane_lun; i++) subs[i] = NULL;
		subs_count = 0;

		int64_t lun_busy_time = 0;
		int64_t channel_busy_time = 0;

		int operation = -1;
		subs_count = find_lun_gc_requests(ssd, channel, lun, subs, &operation);
		if (subs_count == 0) continue;


		// find the total latency to service all sub-requests
		switch(operation){
			case READ:
               			if (subs_count > 1) ssd->stats->read_multiplane_count++;
				channel_busy_time =  read_transfer_time * subs_count;
				lun_busy_time = channel_busy_time + read_time;
				break;
			case WRITE:
                		if (subs_count > 1) ssd->stats->write_multiplane_count++;
				channel_busy_time = write_transfer_time * subs_count;
				lun_busy_time = channel_busy_time + write_time;
				break;
			case ERASE:
                		if (subs_count > 1) ssd->stats->erase_multiplane_count++;
				channel_busy_time =  erase_transfer_time * subs_count;
				lun_busy_time =  channel_busy_time + erase_time;
				break;
			default:
				cout << "Error in the operation: " << operation << endl;
		}
	
		// service all sub_requests
		for (int i = 0; i < ssd->parameter->plane_lun; 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 ,
				ssd->current_time + lun_busy_time);

			change_plane_state (ssd, subs[i]->location->channel,
				subs[i]->location->lun, subs[i]->location->plane,
				PLANE_MODE_GC, ssd->current_time, PLANE_MODE_IDLE,
				ssd->current_time + lun_busy_time);

			if (operation == ERASE){
				delete_gc_node(ssd, subs[i]->gc_node);
				ssd->channel_head[subs[i]->location->channel]->lun_head[subs[i]->location->lun]->GCMode = false;
			}

			if (subs[i]->buf_entry != NULL){
				write_cleanup(ssd, subs[i]);
			}

			delete subs[i];
		}

		// make channel, and lun busy
		if (subs_count != 0) {
			change_lun_state (ssd, channel, lun, LUN_MODE_GC, ssd->current_time ,
					LUN_MODE_IDLE, ssd->current_time +   lun_busy_time);
			change_channel_state(ssd, channel, CHANNEL_MODE_GC, 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, 0x50(%rsp)
movl %esi, %ebx
movq %rdi, %r14
movq 0x58(%rdi), %rax
movslq 0x2cc(%rax), %rcx
imull $0x7, %ecx, %edx
movl 0x22c(%rax), %edi
movl 0x240(%rax), %r8d
imull 0x238(%rax), %r8d
movl 0x2f0(%rax), %esi
imull %r8d, %esi
addl %edx, %esi
movq %rsi, 0x10(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x18(%rsp)
addl $0x7, %r8d
cvtsi2ssl 0x29c(%rax), %xmm0
mulss 0x338(%rax), %xmm0
cvttss2si %xmm0, %rdx
movq %rdx, 0x38(%rsp)
imull %ecx, %r8d
movq %r8, 0x48(%rsp)
leaq (%rcx,%rcx,4), %rcx
movq %rcx, 0x30(%rsp)
movslq 0x2a4(%rax), %rax
movq %rax, 0x40(%rsp)
shlq $0x3, %rdi
callq 0x2040
movq %rax, %r15
callq 0x2090
movq 0x90(%r14), %rcx
movl %ebx, 0xc(%rsp)
movl %ebx, %r13d
movq (%rcx,%r13,8), %rcx
movl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, %ebx
xorl %ebp, %ebp
cmpl %ecx, %ebp
jae 0x5243
leal (%rbx,%rbp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r12d
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x61b8
testl %eax, %eax
jne 0x5031
movq 0x58(%r14), %rax
movl 0x22c(%rax), %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x5011
andq $0x0, (%r15,%rcx,8)
incq %rcx
jmp 0x5002
orl $-0x1, 0x24(%rsp)
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movq %r15, %rcx
leaq 0x24(%rsp), %r8
callq 0x5584
testl %eax, %eax
jne 0x5042
incl %ebp
movq 0x90(%r14), %rax
movq (%rax,%r13,8), %rax
movl (%rax), %ecx
jmp 0x4fd4
movl 0x24(%rsp), %ebx
testl %ebx, %ebx
movl %ebx, 0x2c(%rsp)
je 0x50af
cmpl $0x2, %ebx
je 0x5087
cmpl $0x1, %ebx
jne 0x50df
cmpl $0x2, %eax
jl 0x506f
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movslq %eax, %rcx
imulq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rsp)
addq %rcx, 0x18(%rsp)
jmp 0x5116
cmpl $0x2, %eax
jl 0x509a
movq 0x98(%r14), %rcx
incq 0xf0(%rcx)
movslq %eax, %rcx
imulq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x40(%rsp), %rbp
jmp 0x50d5
cmpl $0x2, %eax
jl 0x50c2
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movslq %eax, %rcx
imulq 0x48(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x38(%rsp), %rbp
addq %rcx, %rbp
movq %rbp, 0x18(%rsp)
jmp 0x5116
movq 0x8eea(%rip), %rdi      # 0xdfd0
leaq 0x453e(%rip), %rsi      # 0x962b
callq 0x2140
movq %rax, %rdi
movl %ebx, %esi
callq 0x22a0
movq %rax, %rdi
callq 0x20b0
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movl %r12d, 0x28(%rsp)
pushq $0x1
popq %rbp
movq 0x58(%r14), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %rbx
jae 0x51f3
movq (%r15,%rbx,8), %r13
testq %r13, %r13
je 0x51eb
movq (%r14), %rcx
movq 0x18(%rsp), %r12
leaq (%rcx,%r12), %r9
movq %r9, 0x58(%r13)
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
pushq $0x9
popq %r8
callq 0x637a
movq 0x60(%r13), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movq (%r14), %r9
leaq (%r9,%r12), %rax
movq %r14, %rdi
movl %ebp, %r8d
pushq %rax
pushq $0x0
callq 0x6298
popq %rax
popq %rcx
cmpl $0x2, 0x2c(%rsp)
jne 0x51bf
movq 0x90(%r13), %rsi
movq %r14, %rdi
callq 0x714e
movq 0x90(%r14), %rax
movq 0x60(%r13), %rcx
movslq (%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, 0x98(%rax)
cmpq $0x0, 0x18(%r13)
je 0x51d1
movq %r14, %rdi
movq %r13, %rsi
callq 0x542b
movq %r13, %rdi
callq 0x5d62
movl $0xa0, %esi
movq %r13, %rdi
callq 0x2170
movl 0x28(%rsp), %r12d
incq %rbx
jmp 0x5120
movq (%r14), %r8
movq 0x18(%rsp), %rax
addq %r8, %rax
subq $0x8, %rsp
pushq $0x1
popq %rbx
movq %r14, %rdi
movl 0x14(%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 0x10(%rsp), %r9
addq %rcx, %r9
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
xorl %r8d, %r8d
callq 0x6304
movq 0x50(%rsp), %rax
movl $0x1, (%rax)
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2200