// File: /workspace/github_cpp_makefile_star2_O3/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
leal (,%rcx,8), %edx
subl %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, 0x8(%rsp)
movslq 0x2d8(%rax), %rdx
movq %rdx, 0x10(%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, 0x20(%rsp)
callq 0x20a0
movq 0x90(%r14), %rcx
movl %ebx, 0x4(%rsp)
movl %ebx, %r13d
movq (%rcx,%r13,8), %rcx
movl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, %ebx
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
leal (%rbx,%r15), %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r12d
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x6bbc
testl %eax, %eax
jne 0x56a8
movq 0x58(%r14), %rax
movl 0x22c(%rax), %edx
testq %rdx, %rdx
je 0x5685
shlq $0x3, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
movq %rbp, %rcx
leaq 0x1c(%rsp), %r8
callq 0x5ce1
testl %eax, %eax
jne 0x56c2
incl %r15d
movq 0x90(%r14), %rax
movq (%rax,%r13,8), %rax
movl (%rax), %ecx
cmpl %ecx, %r15d
jb 0x564d
jmp 0x5906
movl 0x1c(%rsp), %ebp
testl %ebp, %ebp
movl %ebp, 0x2c(%rsp)
je 0x5734
cmpl $0x2, %ebp
movq 0x20(%rsp), %r15
je 0x570c
cmpl $0x1, %ebp
jne 0x5769
cmpl $0x2, %eax
jl 0x56f4
movq 0x98(%r14), %rcx
incq 0xe0(%rcx)
movslq %eax, %rcx
imulq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rsp)
addq %rcx, 0x10(%rsp)
jmp 0x57ca
cmpl $0x2, %eax
jl 0x571f
movq 0x98(%r14), %rcx
incq 0xf0(%rcx)
movslq %eax, %rcx
imulq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0x40(%rsp), %rax
jmp 0x575f
cmpl $0x2, %eax
movq 0x20(%rsp), %r15
jl 0x574c
movq 0x98(%r14), %rcx
incq 0xe8(%rcx)
movslq %eax, %rcx
imulq 0x48(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0x38(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x10(%rsp)
jmp 0x57ca
movq 0x8860(%rip), %rbx      # 0xdfd0
leaq 0x4f87(%rip), %rsi      # 0xa6fe
movl $0x18, %edx
movq %rbx, %rdi
callq 0x21e0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x22d0
movq %rax, %rbx
movq (%rax), %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)
movq $0x0, 0x8(%rsp)
movq 0x58(%r14), %rax
cmpl $0x0, 0x22c(%rax)
je 0x58b1
xorl %r13d, %r13d
movq 0x10(%rsp), %rbp
movq (%r15,%r13,8), %rbx
testq %rbx, %rbx
je 0x589f
movq (%r14), %rcx
leaq (%rcx,%rbp), %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
leaq (%r9,%rbp), %rax
movq %r14, %rdi
movl $0x1, %r8d
pushq %rax
pushq $0x0
callq 0x6c9c
addq $0x10, %rsp
cmpl $0x2, 0x2c(%rsp)
jne 0x5874
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x7dbb
movq 0x90(%r14), %rax
movq 0x60(%rbx), %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(%rbx)
je 0x5886
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5b24
movq %rbx, %rdi
callq 0x65d0
movl $0xa0, %esi
movq %rbx, %rdi
callq 0x21a0
movq 0x58(%r14), %rax
incq %r13
movl 0x22c(%rax), %ecx
cmpq %rcx, %r13
jb 0x57e3
movq (%r14), %r8
movq 0x10(%rsp), %rax
addq %r8, %rax
subq $0x8, %rsp
movq %r14, %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %esi
movl %r12d, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
pushq %rax
callq 0x6c38
addq $0x10, %rsp
movq (%r14), %rcx
movq 0x8(%rsp), %r9
addq %rcx, %r9
movq %r14, %rdi
movl %ebx, %esi
movl $0x1, %edx
xorl %r8d, %r8d
callq 0x6d08
movq 0x50(%rsp), %rax
movl $0x1, (%rax)
movq 0x20(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230