// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, %rbp
movq %rdi, %r13
movq 0x98(%rsp), %r12
movq %rdx, 0x20(%rsp)
movq (%rdx), %rax
movq 0x18(%rax), %rbx
movq (%rdi), %rdi
movq 0x8(%r13), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
callq 0x195511
movq 0x8(%r13), %rax
testq %rax, %rax
jle 0x19fa3a
incq 0x10(%rsp)
xorl %edi, %edi
movq %rbp, 0x40(%rsp)
movq %r13, 0x18(%rsp)
movq %rbx, 0x38(%rsp)
movq (%r13), %rsi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %r12
addq %rdi, %r12
movl 0xc(%rsi,%r12), %eax
movl 0x18(%rsi,%r12), %r13d
shrdl $0x1f, %eax, %r13d
movl %r13d, %eax
xorl $0x1, %eax
movq 0x28(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0x19f8fa
movq 0x8(%rbx), %rdx
shrq $0x20, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %r10d, %r10d
movl %r10d, %r8d
movl (%rcx,%rdx), %r9d
cmpl %r9d, %r10d
movl %r9d, %r10d
cmoval %r8d, %r10d
testl %r9d, %r9d
cmovsl %r8d, %r10d
addq $0x18, %rdx
cmpq %rdx, %rax
jne 0x19f8d4
movl %r10d, %eax
jmp 0x19f8fc
xorl %eax, %eax
addq %rsi, %r12
movl 0x4(%r12), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x28(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x10(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %rdi, 0x48(%rsp)
callq 0x19f679
movl %eax, %ebp
movl 0xc(%r12), %esi
movl 0x10(%r12), %ecx
movl 0x14(%r12), %r8d
movq 0x20(%rsp), %rdi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x36ec9
movq %rax, %r14
testl %ebp, %ebp
js 0x19f9e4
movl %ebp, %ebx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rbp
addq %rbx, %rbp
movq 0x18(%rsp), %rax
addq (%rax), %rbp
movq $-0x1, %r15
movq %r14, 0x50(%rsp)
movl 0xc(%rbp), %eax
movl 0x18(%rbp), %ecx
shrdl $0x1f, %eax, %ecx
cmpl %ecx, %r13d
je 0x19f9d2
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %r8
movsd 0x30(%rsp), %xmm0
callq 0x19f709
testq %rax, %rax
je 0x19f9d2
movq 0x98(%rsp), %rax
movl 0x4(%rax,%rbx,8), %eax
addq 0x50(%rsp), %rax
cmpq %r14, %rax
cmovgq %rax, %r14
cmovgq %rbx, %r15
leaq -0x1(%rbx), %rax
addq $-0x1c, %rbp
testq %rbx, %rbx
movq %rax, %rbx
jg 0x19f987
jmp 0x19f9eb
movq $-0x1, %r15
shlq $0x20, %r14
testq %r15, %r15
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r15
orq %r14, %r15
movq 0x98(%rsp), %r12
movq 0x48(%rsp), %rdi
movq %r15, (%r12,%rdi,8)
orq %rdi, %r14
movq 0x8(%rsp), %rax
movq %r14, (%rax,%rdi,8)
incq %rdi
movq 0x18(%rsp), %r13
movq 0x8(%r13), %rax
cmpq %rax, %rdi
movq 0x40(%rsp), %rbp
movq 0x38(%rsp), %rbx
jl 0x19f87a
movq 0x90(%rsp), %r14
movq 0x8(%rsp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
movq 0x8(%r13), %rax
movq %rax, 0x8(%rbp)
movq 0x8(%r13), %rcx
xorl %eax, %eax
testq %rcx, %rcx
jle 0x19fb1e
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq $-0x1, %rsi
xorl %edi, %edi
movq %rdi, %r9
movq %rcx, %r8
decq %rcx
movq 0x8(%rsp), %r10
movl -0x8(%r10,%r8,8), %r11d
leaq (%rdi,%rdi,8), %r10
leaq (%r10,%r10,2), %r10
addq %rdi, %r10
testb $-0x80, 0x3(%r12,%r11,8)
jne 0x19faf4
movq (%r13), %rbx
leaq (%r11,%r11,8), %r15
leaq (%r15,%r15,2), %r15
addq %r11, %r15
movq (%rbp), %r12
incq %rdi
movupd (%rbx,%r15), %xmm0
movups 0xc(%rbx,%r15), %xmm1
movups %xmm1, 0xc(%r12,%r10)
movupd %xmm0, (%r12,%r10)
movq 0x98(%rsp), %r12
movq (%r12,%r11,8), %rbx
movq %rbx, %r15
btsq $0x1f, %r15
movq %r15, (%r12,%r11,8)
andq %rdx, %rbx
addq $0x1c, %r10
cmpq %rdx, %rbx
cmoveq %rsi, %rbx
movq %rbx, %r11
jne 0x19fa93
movq %rdi, %r10
subq %r9, %r10
je 0x19fb14
movq 0x8(%rsp), %r9
movl 0x4(%r9,%rcx,8), %r9d
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%r14,%rax,8)
incq %rax
cmpq $0x1, %r8
jg 0x19fa75
testq %rax, %rax
jle 0x19fb87
movq (%r13), %rcx
movq (%rbp), %rdx
leaq -0x18(%rdx), %rsi
addq $0x14, %rcx
xorl %r8d, %r8d
xorl %edi, %edi
movq (%r14,%rdi,8), %r9
movl %r9d, %r10d
addq %r8, %r10
shrq $0x20, %r9
movl %r9d, -0x14(%rcx)
movl %r8d, -0x4(%rcx)
movl %r10d, (%rcx)
leaq (%r10,%r10,8), %r9
leaq (%r9,%r9,2), %r9
addq %r10, %r9
movl (%rsi,%r9), %r9d
movl %r9d, -0x10(%rcx)
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movl 0x8(%rdx,%r9), %r8d
movl %r8d, -0xc(%rcx)
incq %rdi
addq $0x1c, %rcx
movq %r10, %r8
cmpq %rdi, %rax
jne 0x19fb38
movq %rax, 0x8(%r13)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq