// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r13
movq %r8, %r14
movsd %xmm0, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq 0x98(%rsp), %rbp
movq %rdx, 0x8(%rsp)
movq (%rdx), %rax
movq 0x18(%rax), %rax
movq %rax, 0x20(%rsp)
movq (%rdi), %rdi
imulq $0x1c, 0x8(%rbx), %rsi
addq %rdi, %rsi
callq 0x156f5c
incq %r14
movq %r14, 0x28(%rsp)
xorl %ecx, %ecx
movq %r12, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq 0x8(%rbx), %rax
cmpq %rax, %rcx
jge 0x160a33
movq (%rbx), %r15
imulq $0x1c, %rcx, %r14
leaq (%r15,%r14), %rax
movq %rax, 0x50(%rsp)
movl 0xc(%r15,%r14), %eax
movl 0x18(%r15,%r14), %ebp
shrdl $0x1f, %eax, %ebp
movl %ebp, %esi
xorl $0x1, %esi
movq %rcx, %r12
movq 0x20(%rsp), %rdi
callq 0x15d450
movl 0x4(%r15,%r14), %ecx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x10(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x28(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %r12, 0x40(%rsp)
movl %r12d, %edi
movq %r15, %rsi
callq 0x160717
movslq %eax, %r13
movl 0xc(%r15,%r14), %esi
movl 0x10(%r15,%r14), %ecx
movl 0x14(%r15,%r14), %r8d
movq 0x8(%rsp), %rdi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xa240
movq %rax, %r15
imulq $0x1c, %r13, %r14
movq %rax, 0x48(%rsp)
pushq $-0x1
popq %r12
testq %r13, %r13
js 0x1609f4
movq (%rbx), %rdx
movl 0xc(%rdx,%r14), %eax
movl 0x18(%rdx,%r14), %ecx
shrdl $0x1f, %eax, %ecx
cmpl %ecx, %ebp
je 0x1609eb
addq %r14, %rdx
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %r8
movsd 0x18(%rsp), %xmm0
callq 0x160799
testq %rax, %rax
je 0x1609eb
movq 0x98(%rsp), %rax
movl 0x4(%rax,%r13,8), %eax
addq 0x48(%rsp), %rax
cmpq %r15, %rax
cmovgq %rax, %r15
cmovgq %r13, %r12
decq %r13
addq $-0x1c, %r14
jmp 0x160992
shlq $0x20, %r15
testq %r12, %r12
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r12
orq %r15, %r12
movq 0x98(%rsp), %rbp
movq 0x40(%rsp), %rcx
movq %r12, (%rbp,%rcx,8)
orq %rcx, %r15
movq 0x30(%rsp), %r13
movq %r15, (%r13,%rcx,8)
incq %rcx
movq 0x38(%rsp), %r12
jmp 0x1608e1
leaq (,%rax,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x156b0d
movq 0x8(%rbx), %rcx
movq %rcx, 0x8(%r12)
xorl %r9d, %r9d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rsi
xorl %eax, %eax
testq %rcx, %rcx
jle 0x160b03
movq %r9, %rdi
decq %rcx
movl (%r13,%rcx,8), %r10d
imulq $0x1c, %r9, %r9
xorl %r8d, %r8d
testq %r10, %r10
js 0x160acf
testb $-0x80, 0x3(%rbp,%r10,8)
jne 0x160acf
movq (%rbx), %r11
imulq $0x1c, %r10, %r14
movq (%r12), %r15
movupd (%r11,%r14), %xmm0
movups 0xc(%r11,%r14), %xmm1
movups %xmm1, 0xc(%r15,%r9)
movupd %xmm0, (%r15,%r9)
movq (%rbp,%r10,8), %r11
movq %r11, %r14
btsq $0x1f, %r14
andq %rdx, %r11
cmpq %rdx, %r11
cmoveq %rsi, %r11
movq %r14, (%rbp,%r10,8)
decq %r8
addq $0x1c, %r9
movq %r11, %r10
jmp 0x160a77
movq %rdi, %r9
subq %r8, %r9
testq %r8, %r8
je 0x160a5c
movl 0x4(%r13,%rcx,8), %r9d
shlq $0x20, %r9
subq %r8, %rdi
negq %r8
orq %r9, %r8
movq 0x90(%rsp), %r9
movq %r8, (%r9,%rax,8)
incq %rax
movq %rdi, %r9
jmp 0x160a5c
pushq $0x14
popq %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x160b61
movq 0x90(%rsp), %rdi
movq (%rdi,%rdx,8), %rdi
movl %edi, %r8d
addq %rsi, %r8
shrq $0x20, %rdi
movq (%rbx), %r9
movl %edi, -0x14(%r9,%rcx)
movl %esi, -0x4(%r9,%rcx)
movl %r8d, (%r9,%rcx)
movq (%r12), %rdi
imulq $0x1c, %r8, %r10
movl -0x18(%rdi,%r10), %r10d
movl %r10d, -0x10(%r9,%rcx)
imulq $0x1c, %rsi, %rsi
movl 0x8(%rdi,%rsi), %esi
movl %esi, -0xc(%r9,%rcx)
incq %rdx
addq $0x1c, %rcx
movq %r8, %rsi
jmp 0x160b0a
movq %rax, 0x8(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq