// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, 0x58(%rsp)
movl %esi, %ebx
movq %rdi, %r14
movq 0x58(%rdi), %rax
movslq 0x2cc(%rax), %rcx
leal (,%rcx,8), %edx
subl %ecx, %edx
movl 0x22c(%rax), %edi
movl 0x240(%rax), %esi
imull 0x238(%rax), %esi
movl 0x2f0(%rax), %r8d
imull %esi, %r8d
addl %edx, %r8d
movq %r8, 0x40(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x38(%rsp)
addl $0x7, %esi
cvtsi2ssl 0x29c(%rax), %xmm0
mulss 0x338(%rax), %xmm0
cvttss2si %xmm0, %rdx
movq %rdx, 0x30(%rsp)
imull %ecx, %esi
movq %rsi, 0x48(%rsp)
leaq (%rcx,%rcx,4), %rcx
movq %rcx, 0x28(%rsp)
movslq 0x2a4(%rax), %rax
movq %rax, 0x20(%rsp)
shlq $0x3, %rdi
callq 0x2040
movq %rax, %r15
callq 0x20a0
movq 0x90(%r14), %rcx
movl %ebx, %esi
movq (%rcx,%rsi,8), %rcx
movl (%rcx), %ecx
cltd
idivl %ecx
xorl %r12d, %r12d
movl %ebx, %ebp
movl %ebx, 0x18(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, %rbx
movq %rdx, 0x50(%rsp)
leal (%r12,%rbx), %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r13d
movq %r14, %rdi
movl %ebp, %esi
callq 0x6ccc
testl %eax, %eax
jne 0x5963
movl %r13d, 0x8(%rsp)
movq 0x58(%r14), %rax
movl 0x22c(%rax), %edx
testq %rdx, %rdx
je 0x56fe
shlq $0x3, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %r14, %rdi
movl %ebp, %esi
movl 0x8(%rsp), %edx
movq %r15, %rcx
leaq 0xc(%rsp), %r8
callq 0x5d66
testl %eax, %eax
je 0x5759
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
movl %ebp, 0x1c(%rsp)
je 0x5786
cmpl $0x2, %ebp
je 0x5763
cmpl $0x1, %ebp
jne 0x57b2
cmpl $0x2, %eax
jl 0x5749
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movslq %eax, %rdx
imulq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rax
jmp 0x57a7
movl $0x4, %eax
jmp 0x595f
cmpl $0x2, %eax
jl 0x5776
movq 0x98(%r14), %rcx
incq 0xf0(%rcx)
movslq %eax, %rdx
imulq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rax
jmp 0x57a7
cmpl $0x2, %eax
jl 0x5799
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movslq %eax, %rdx
imulq 0x48(%rsp), %rdx
movq 0x30(%rsp), %rax
movq %rdx, 0x10(%rsp)
leaq (%rdx,%rax), %r13
jmp 0x580d
movl $0x18, %edx
movq 0x8812(%rip), %rbx      # 0xdfd0
movq %rbx, %rdi
leaq 0x4f36(%rip), %rsi      # 0xa6fe
callq 0x21e0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x22d0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x20f0
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq 0x58(%r14), %rax
cmpl $0x0, 0x22c(%rax)
je 0x5901
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rsi
testq %rsi, %rsi
je 0x58eb
movq (%r14), %rcx
leaq (%rcx,%r13), %r9
movq %r9, 0x58(%rsi)
movq %r14, %rdi
movl $0x1, %edx
movl $0x9, %r8d
callq 0x6e8e
movq (%r15,%rbx,8), %rax
movq 0x60(%rax), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
movq (%r14), %r9
leaq (%r9,%r13), %rax
movq %r14, %rdi
movl $0x1, %r8d
pushq %rax
pushq $0x0
callq 0x6dac
addq $0x10, %rsp
cmpl $0x2, 0x1c(%rsp)
jne 0x58ba
movq (%r15,%rbx,8), %rax
movq 0x90(%rax), %rsi
movq %r14, %rdi
callq 0x7e84
movq 0x90(%r14), %rax
movq (%r15,%rbx,8), %rcx
movq 0x60(%rcx), %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)
movq (%r15,%rbx,8), %rsi
cmpq $0x0, 0x18(%rsi)
je 0x58cd
movq %r14, %rdi
callq 0x5ba9
movq (%r15,%rbx,8), %rbp
testq %rbp, %rbp
je 0x58eb
movq %rbp, %rdi
callq 0x66d0
movl $0xa0, %esi
movq %rbp, %rdi
callq 0x21a0
incq %rbx
movq 0x58(%r14), %rax
movl 0x22c(%rax), %eax
cmpq %rax, %rbx
jb 0x5820
movq (%r14), %r8
addq %r8, %r13
subq $0x8, %rsp
movq %r14, %rdi
movl 0x20(%rsp), %ebx
movl %ebx, %esi
movl 0x10(%rsp), %edx
movl $0x1, %ecx
xorl %r9d, %r9d
pushq %r13
callq 0x6d48
addq $0x10, %rsp
movq (%r14), %rcx
movq 0x10(%rsp), %r9
addq %rcx, %r9
movq %r14, %rdi
movl %ebx, %ebp
movl %ebx, %esi
movl $0x1, %edx
xorl %r8d, %r8d
callq 0x6e18
movq 0x58(%rsp), %rax
movl $0x1, (%rax)
movl $0x2, %eax
movq 0x50(%rsp), %rbx
testb $0x3, %al
jne 0x5981
incl %r12d
movq 0x90(%r14), %rax
movq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %ecx
cmpl %ecx, %r12d
jb 0x56bf
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230