// File: chhylp123[P]hifiasm/inter.cpp
// Function: gl_exact_chain(kv_ul_ov_t*, kv_ul_ov_t*, ul_idx_t const*, long, double, long, unsigned long*, unsigned long*, unsigned long*, void*)
int64_t gl_exact_chain(kv_ul_ov_t *res, kv_ul_ov_t *ex, const ul_idx_t *uref, int64_t bw, double diff_ec_ul, 
int64_t qlen, uint64_t *srt, uint64_t *idx, uint64_t *track, void *km)
{
	// fprintf(stderr, "*****************\n");
	uint32_t li_v, lj_v;
	int64_t mm_ovlp, x, i, j, k, sc, csc, mm_sc, mm_idx;
	ul_ov_t *li = NULL, *lj = NULL;
	const asg_t *g = uref->ug->g;
	radix_sort_ul_ov_srt_qe(res->a, res->a + res->n);
	for (i = 0; i < (int64_t)res->n; ++i) {
		li = &(res->a[i]); li_v = (li->tn<<1)|li->rev;
		mm_ovlp = max_ovlp(g, li_v^1);
		x = (li->qs + mm_ovlp)*diff_ec_ul; 
		if(x < bw) x = bw; 
		x += li->qs + mm_ovlp;
		if (x > qlen+1) x = qlen+1;
		x = find_ul_ov_max(i, res->a, x); 
		csc = retrieve_u_cov_region(uref, li->tn, 0, li->ts, li->te, NULL);
		mm_sc = csc; mm_idx = -1;
		// fprintf(stderr, "---i:%ld, csc:%ld, li->tn:%u, li->ts:%u, li->te:%u\n", i, csc, li->tn, li->ts, li->te);
        for (j = x; j >= 0; --j) { // collect potential destination vertices
			lj = &(res->a[j]); lj_v = (lj->tn<<1)|lj->rev;
			// if(lj->qs >= li->qs) continue; // lj is contained in li on the query coordinate
			if(li_v != lj_v && get_ecov(uref, li, lj, qlen, bw, diff_ec_ul)) {
				sc = csc + (track[j]>>32);
				if(sc > mm_sc) mm_sc = sc, mm_idx = j;
			}
		}
		// 4294967295L
		track[i] = mm_sc; track[i] <<= 32;
		track[i] |= (mm_idx>=0?mm_idx:((uint64_t)0x7FFFFFFF));
		srt[i] = mm_sc; srt[i] <<= 32; srt[i] |= i;
		// fprintf(stderr, "+++i:%ld, mm_idx:%ld, mm_sc:%ld\n", i, mm_idx, mm_sc);
		// fprintf(stderr, "[M::utg%.6d%c] qs->%u; qe->%u\n\n", li->tn+1, "lc"[uref->ug->u.a[li->tn].circ], li->qs, li->qe);
	}
	
	int64_t n_v, n_u, n_v0; 
	radix_sort_gfa64(srt, srt+res->n); ex->n = res->n;
	for (k = (int64_t)res->n-1, n_v = n_u = 0; k >= 0; --k) {
		// fprintf(stderr, "\nk:%ld\n", k);
		n_v0 = n_v;
		for (i = (uint32_t)srt[k]; i >= 0 && (track[i]&((uint64_t)0x80000000)) == 0;) {
			ex->a[n_v++] = res->a[i]; track[i] |= ((uint64_t)0x80000000);
			// fprintf(stderr, "+i:%ld, ", i);
			// fprintf(stderr, "[M::utg%.6d%c] qs->%u; qe->%u\n", res->a[i].tn+1, "lc"[uref->ug->u.a[res->a[i].tn].circ], res->a[i].qs, res->a[i].qe);
			if((track[i]&((uint64_t)0x7FFFFFFF)) == ((uint64_t)0x7FFFFFFF)) i = -1;
			else i = track[i]&((uint64_t)0x7FFFFFFF);
			// if(i>=(int64_t)res->n) fprintf(stderr, "ERROR->i:%ld, res->n:%d, n_v:%ld, qlen:%ld\n", i, (int32_t)res->n, n_v, qlen);
			// fprintf(stderr, "next_i:%ld\n", i);
			// i = (olist->list[i].y_id == (uint32_t)-1?-1:olist->list[i].y_id);
			// fprintf(stderr, "-i:%ld\n", i);
		}
		if(n_v0 == n_v) continue;
		///keep the whole score; do not cut score like minigraph
		// sc = (i<0?(srt[k]>>32):((srt[k]>>32)-olist->list[i].x_id));
		sc = srt[k]>>32;
		idx[n_u++] = ((uint64_t)sc<<32)|(n_v-n_v0);
	}
	// if(n_v != (int64_t)res->n) {
	// 	fprintf(stderr, "\nERROR->n_v:%ld, res->n:%d, qlen:%ld\n", n_v, (int32_t)res->n, qlen);
	// 	for (k = 0; k < (int64_t)res->n; k++) {
	// 		fprintf(stderr, "(%ld)srt-sc:%lu, srt-i:%u\n", k, srt[k]>>32, (uint32_t)srt[k]);
	// 	}

	// 	for (k = 0; k < (int64_t)res->n; k++) {
	// 		fprintf(stderr, "(%ld)track-sc:%lu, track-pi:%lu\n", k, track[k]>>32, track[k]&((uint64_t)0x7FFFFFFF));
	// 	}
	// }
	// if(n_v && ex->a[0].qn == 6) {
	// 	for (k = 0, n_v = n_v0 = 0; k < n_u; k++) {
	// 		n_v0 = n_v; n_v += (uint32_t)idx[k];
	// 		fprintf(stderr, "\n");
	// 		for (i = n_v0; i < n_v; i++) {
	// 			fprintf(stderr, "[%u, %u]\n", ex->a[i].qs, ex->a->qe);
	// 		}
	// 	}
	// }

	for (k = 0, n_v = n_v0 = 0; k < n_u; k++) {
		n_v0 = n_v; n_v += (uint32_t)idx[k];
		res->a[k].qn = idx[k]>>32;
		res->a[k].ts = n_v0; res->a[k].te = n_v;
		res->a[k].qs = ex->a[n_v-1].qs;
		res->a[k].qe = ex->a[n_v0].qe;
	}
	res->n = n_u;
	return res->n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq -0x8(%rbp), %rax
imulq $0x1c, 0x8(%rax), %rax
addq %rax, %rsi
callq 0x27b350
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x28d551
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x90(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0xa0(%rbp), %rdi
movl -0x3c(%rbp), %esi
xorl $0x1, %esi
callq 0x287d20
movq %rax, -0x48(%rbp)
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
mulsd -0x28(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x28d342
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x48(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jle 0x28d375
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
movl %eax, %edi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq -0x50(%rbp), %rax
movl %eax, %edx
callq 0x28cf00
cltq
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %esi
movq -0x90(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0x90(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %r8d
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r9d
callq 0x110c0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $-0x1, -0x88(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jl 0x28d4a5
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x98(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
je 0x28d492
movq -0x18(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %r8
movsd -0x28(%rbp), %xmm0
callq 0x28d000
cmpq $0x0, %rax
je 0x28d492
movq -0x78(%rbp), %rax
movq 0x18(%rbp), %rcx
movq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x80(%rbp), %rax
jle 0x28d490
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x28d492
jmp 0x28d494
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x28d3e7
movq -0x80(%rbp), %rdx
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x88(%rbp)
jl 0x28d4e3
movq -0x88(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x28d4f1
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0xc0(%rbp)
jmp 0x28d4f1
movq -0xc0(%rbp), %rdx
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x58(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x28d2b3
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x27a9b0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa8(%rbp)
cmpq $0x0, -0x68(%rbp)
jl 0x28d723
movq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x58(%rbp)
movb %al, -0xc1(%rbp)
jl 0x28d600
movq 0x18(%rbp), %rcx
movq -0x58(%rbp), %rdx
movabsq $0x80000000, %rax       # imm = 0x80000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
sete %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x28d60f
jmp 0x28d6b6
movq -0x8(%rbp), %rax
movq (%rax), %rcx
imulq $0x1c, -0x58(%rbp), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0xa8(%rbp)
imulq $0x1c, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0x18(%rcx), %ecx
movl %ecx, 0x18(%rax)
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movabsq $0x80000000, %rdx       # imm = 0x80000000
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
jne 0x28d69b
movq $-0x1, -0x58(%rbp)
jmp 0x28d6b1
movq 0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x58(%rbp)
jmp 0x28d5ce
movq -0xb8(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x28d6c8
jmp 0x28d712
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdx
shlq $0x20, %rdx
movq -0xa8(%rbp), %rax
subq -0xb8(%rbp), %rax
orq %rax, %rdx
movq 0x10(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb0(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x28d5a3
movq $0x0, -0x68(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xa8(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jge 0x28d83b
movq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq 0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq 0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x68(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0xb8(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x68(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movq -0xa8(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x68(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x68(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0xb8(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x68(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x28d741
movq -0xb0(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)