// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %rbp
movq 0x98(%rsp), %r15
movq %rdx, 0x10(%rsp)
movq (%rdx), %rax
movq 0x18(%rax), %rbx
movq (%rdi), %rdi
movq 0x8(%rbp), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
callq 0x19e2b4
movq 0x8(%rbp), %rax
testq %rax, %rax
jle 0x1a8780
incq 0x8(%rsp)
xorl %edi, %edi
movq %rbp, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq %rbx, 0x30(%rsp)
movq (%rbp), %rsi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rcx
addq %rdi, %rcx
movl 0xc(%rsi,%rcx), %eax
movq %rcx, %r11
movl 0x18(%rsi,%rcx), %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 0x1a8638
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 0x1a8612
movl %r10d, %eax
jmp 0x1a863a
xorl %eax, %eax
movq %r11, %r15
addq %rsi, %r15
movl 0x4(%r15), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x18(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x8(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %rdi, 0x48(%rsp)
callq 0x1a83b5
movl %eax, %r14d
movl 0xc(%r15), %esi
movl 0x10(%r15), %ecx
movq %r15, 0x50(%rsp)
movl 0x14(%r15), %r8d
movq 0x10(%rsp), %rdi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x363d1
movq %rax, %r12
testl %r14d, %r14d
js 0x1a8742
movl %r14d, %ebx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %r14
addq %rbx, %r14
addq (%rbp), %r14
movq $-0x1, %r15
movq %r12, %rbp
movl 0xc(%r14), %eax
movl 0x18(%r14), %ecx
shrdl $0x1f, %eax, %ecx
cmpl %ecx, %r13d
je 0x1a8717
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq %r14, %rdx
movq 0x18(%rsp), %r8
movsd 0x20(%rsp), %xmm0
callq 0x1a8445
testq %rax, %rax
je 0x1a8717
movq 0x98(%rsp), %rax
movl 0x4(%rax,%rbx,8), %eax
addq %r12, %rax
cmpq %rbp, %rax
cmovgq %rax, %rbp
cmovgq %rbx, %r15
leaq -0x1(%rbx), %rax
addq $-0x1c, %r14
testq %rbx, %rbx
movq %rax, %rbx
jg 0x1a86ca
testq %r15, %r15
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r15
movq %rbp, %r12
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %rbx
jmp 0x1a8748
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
shlq $0x20, %r12
orq %r12, %r15
movq 0x98(%rsp), %rax
movq 0x48(%rsp), %rdi
movq %r15, (%rax,%rdi,8)
movq %rax, %r15
orq %rdi, %r12
movq 0x40(%rsp), %r13
movq %r12, (%r13,%rdi,8)
incq %rdi
movq 0x8(%rbp), %rax
cmpq %rax, %rdi
jl 0x1a85b6
leaq (,%rax,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x19dd99
movq 0x8(%rbp), %rdx
movq 0x28(%rsp), %r12
movq %rdx, 0x8(%r12)
testq %rdx, %rdx
jle 0x1a88c2
movq 0x90(%rsp), %rcx
xorl %eax, %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edi, %edi
movq %rdi, %r9
movq %rdx, %r8
decq %rdx
movl -0x8(%r13,%r8,8), %r11d
leaq (%rdi,%rdi,8), %r10
leaq (%r10,%r10,2), %r10
addq %rdi, %r10
testb $-0x80, 0x3(%r15,%r11,8)
jne 0x1a8831
movq (%rbp), %rbx
leaq (%r11,%r11,8), %r14
leaq (%r14,%r14,2), %r14
addq %r11, %r14
movq (%r12), %r15
incq %rdi
movupd (%rbx,%r14), %xmm0
movups 0xc(%rbx,%r14), %xmm1
movups %xmm1, 0xc(%r15,%r10)
movupd %xmm0, (%r15,%r10)
movq 0x98(%rsp), %r15
movq (%r15,%r11,8), %rbx
movq %rbx, %r14
btsq $0x1f, %r14
movq %r14, (%r15,%r11,8)
andq %rsi, %rbx
addq $0x1c, %r10
movq %rbx, %r11
cmpq %rsi, %rbx
jne 0x1a87d4
movq %rdi, %r10
subq %r9, %r10
je 0x1a884c
movl 0x4(%r13,%rdx,8), %r9d
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%rcx,%rax,8)
incq %rax
cmpq $0x1, %r8
jg 0x1a87bb
testq %rax, %rax
jle 0x1a88c4
movq (%rbp), %rdx
movq (%r12), %rsi
leaq -0x18(%rsi), %rdi
addq $0x14, %rdx
xorl %r9d, %r9d
xorl %r8d, %r8d
movq (%rcx,%r8,8), %r10
movl %r10d, %r11d
addq %r9, %r11
shrq $0x20, %r10
movl %r10d, -0x14(%rdx)
movl %r9d, -0x4(%rdx)
movl %r11d, (%rdx)
leaq (%r11,%r11,8), %r10
leaq (%r10,%r10,2), %r10
addq %r11, %r10
movl (%rdi,%r10), %r10d
movl %r10d, -0x10(%rdx)
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r10
addq %r9, %r10
movl 0x8(%rsi,%r10), %r9d
movl %r9d, -0xc(%rdx)
incq %r8
addq $0x1c, %rdx
movq %r11, %r9
cmpq %r8, %rax
jne 0x1a8871
jmp 0x1a88c4
xorl %eax, %eax
movq %rax, 0x8(%rbp)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq