// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
imull $0x7, 0x2cc(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x240(%rdx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x2f0(%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x2d8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
imull $0x7, 0x2cc(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x238(%rcx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x240(%rdx), %ecx
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rdx
imull 0x2cc(%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movss 0x338(%rax), %xmm0
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
cvtsi2ssl 0x29c(%rax), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
imull $0x5, 0x2cc(%rax), %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movslq 0x2a4(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x22c(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
callq 0x2090
movq -0x8(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0xc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
cltd
idivl (%rcx)
movl %edx, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0xc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
cmpl (%rcx), %eax
jae 0x738a
movl -0x60(%rbp), %eax
addl -0x58(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x90(%rcx), %rcx
movl -0xc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
divl (%rcx)
movl %edx, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
callq 0x9660
cmpl $0x0, %eax
je 0x6fbe
jmp 0x737c
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x22c(%rcx), %eax
jae 0x6ff3
movq -0x50(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6fc5
movl $0x0, -0x54(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
movq -0x50(%rbp), %rcx
leaq -0x7c(%rbp), %r8
callq 0x7910
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x7036
jmp 0x737c
movl -0x7c(%rbp), %eax
movl %eax, -0x84(%rbp)
testl %eax, %eax
je 0x70aa
jmp 0x7045
movl -0x84(%rbp), %eax
subl $0x1, %eax
je 0x7066
jmp 0x7052
movl -0x84(%rbp), %eax
subl $0x2, %eax
je 0x70eb
jmp 0x712c
cmpl $0x1, -0x54(%rbp)
jle 0x7089
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xe0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xe0(%rax)
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x7159
cmpl $0x1, -0x54(%rbp)
jle 0x70cd
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xe8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xe8(%rax)
movq -0x30(%rbp), %rax
movslq -0x54(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x7159
cmpl $0x1, -0x54(%rbp)
jle 0x710e
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movq 0xf0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xf0(%rax)
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x7159
movq 0xce9d(%rip), %rdi      # 0x13fd0
leaq 0x8503(%rip), %rsi      # 0xf63d
callq 0x2110
movq %rax, %rdi
movl -0x7c(%rbp), %esi
callq 0x2220
movq %rax, %rdi
movq 0xce6c(%rip), %rsi      # 0x13fc0
callq 0x2150
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x58(%rcx), %rcx
cmpl 0x22c(%rcx), %eax
jae 0x7315
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x718b
jmp 0x7307
movq -0x8(%rbp), %rax
movq (%rax), %rcx
addq -0x70(%rbp), %rcx
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %r9
addq -0x70(%rbp), %r9
movl $0x1, %edx
movl $0x9, %r8d
callq 0x9dd0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl (%rax), %esi
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x4(%rax), %edx
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x60(%rax), %rax
movl 0x8(%rax), %ecx
movq -0x8(%rbp), %rax
movq (%rax), %r9
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq -0x70(%rbp), %rax
movl $0x1, %r8d
xorl %r10d, %r10d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9970
cmpl $0x2, -0x7c(%rbp)
jne 0x72a7
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x90(%rax), %rsi
callq 0xb870
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x50(%rbp), %rcx
movslq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x60(%rcx), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
movslq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x60(%rcx), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, 0x98(%rax)
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x18(%rax)
je 0x72cf
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x76a0
movq -0x50(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x7305
movq -0x90(%rbp), %rdi
callq 0x8ce0
movq -0x90(%rbp), %rdi
movl $0xa0, %esi
callq 0x2140
jmp 0x7307
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x7160
cmpl $0x0, -0x54(%rbp)
je 0x737a
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c(%rbp), %edx
movq -0x8(%rbp), %rax
movq (%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq -0x70(%rbp), %rax
movl $0x1, %ecx
xorl %r9d, %r9d
movq %rax, (%rsp)
callq 0x9780
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %r9
addq -0x78(%rbp), %r9
movl $0x1, %edx
xorl %r8d, %r8d
callq 0x9bc0
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x738a
jmp 0x737c
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x6f69
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x73a7
movq -0x98(%rbp), %rdi
callq 0x21b0
addq $0xb0, %rsp
popq %rbp
retq