// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: linear_chain_dp(ul_ov_t*, long, ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, unsigned long*, unsigned long*, ma_ug_t*, long)
uint64_t linear_chain_dp(ul_ov_t *ch, int64_t ch_n, ul_ov_t *sv, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, int64_t qlen, int64_t max_skip, uint64_t *idx, uint64_t *track, ma_ug_t *ug, int64_t chain_offset)
{	///all in[].el must be 1
	if(ch_n == 0) return 0;
    int64_t /**mm_ovlp, x,**/ i, j, k, sc, csc, mm_sc, mm_idx, its, ite, jts, jte, cov_i, dq, dt, dd, mm;
    ul_ov_t *li = NULL, *lj = NULL;
    radix_sort_ul_ov_srt_qe(ch, ch + ch_n);
	for (i = 1, j = 0; i <= ch_n; i++) {
		// if(i < ch_n) {
		// 	li = &(ch[i]); 
		// 	fprintf(stderr, "##(%ld) %u\t%u\t%c\tutg%.6d%c(%u)\t%u\t%u\tmm_idx:%ld\tmm_sc:%ld\n", i, li->qs, li->qe, "+-"[li->rev], 
        // 	(int32_t)(li->tn)+1, "lc"[uref->ug->u.a[li->tn].circ], uref->ug->u.a[li->tn].len, li->ts, li->te, mm_idx, mm_sc);
		// }
        if (i == ch_n || ch[i].qe != ch[j].qe) {
            if(i - j > 1) {
                radix_sort_ul_ov_srt_qs(ch+j, ch+i);
            }
            j = i;
        }
    }

	// fprintf(stderr, "[M::%s::] ch_n:%ld\n", __func__, ch_n);
	for (i = 0; i < ch_n; ++i) {
		li = &(ch[i]); 
		// mm_ovlp = max_ovlp_src(uopt, ((li->tn<<1)|li->rev)^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, ch, x+G_CHAIN_INDEL); 
		adjust_rev_tse(li, ug->g->seq[li->tn].len, &its, &ite);
		cov_i = 0;
		csc = retrieve_u_cov_region(uref, li->tn, 0, its, ite, &cov_i);
		mm_sc = csc; mm_idx = -1; 
		for (j = i-1/**x**/; j >= 0; --j) {
			lj = &(ch[j]); 
			// fprintf(stderr, "<0>\n");
			if(lj->qs <= li->qs && lj->qe <= li->qe && lj->ts <= li->ts && lj->te <= li->te) {///co-linear
				assert(li->tn == lj->tn && li->rev == lj->rev);
				// fprintf(stderr, "<1>\n");
				if(lj->qs == li->qs && lj->qe == li->qe && lj->ts == li->ts && lj->te == li->te) continue;
				dq = li->qe - lj->qs; dt = li->te - lj->ts;
				dd = (dq>dt? dq-dt:dt-dq);
				mm = MAX(dq, dt); mm *= diff_ec_ul; if(mm < bw) mm = bw;
				// fprintf(stderr, "+++i->%ld, j->%ld, dd->%ld, mm->%ld\n", i, j, dd, mm);
				if(dd <= mm) {///pass distance checking
					adjust_rev_tse(lj, ug->g->seq[lj->tn].len, &jts, &jte);
					sc = get_add_cov_score(uref, jts, jte, its, ite, li->tn, &cov_i) + pop_sc(track[j]);
					
					if((sc > mm_sc) || (sc == mm_sc && mm_idx == -1)) { ///must be >= instead of >
						mm_sc = sc, mm_idx = j;
					}
					// fprintf(stderr, "<i->%ld, its:%ld, ite:%ld>, <j->%ld, jts:%ld, jte:%ld> sc:%ld, pop_sc(track[j]):%ld, csc:%ld\n", 
					// i, its, ite, j, jts, jte, sc, pop_sc(track[j]), csc);
				}
			}
		}

		// fprintf(stderr, "##(%ld) %u\t%u\t%c\tutg%.6d%c(%u)\t%u\t%u\tmm_idx:%ld\tmm_sc:%ld\n", i, li->qs, li->qe, "+-"[li->rev], 
        // (int32_t)(li->tn)+1, "lc"[uref->ug->u.a[li->tn].circ], uref->ug->u.a[li->tn].len, li->ts, li->te, mm_idx, mm_sc);
		track[i] = push_sc_pre(mm_sc, mm_idx); 
		li->sec = (mm_idx<0?0x3FFFFFFF:i-mm_idx); sv[i] = *li;
	}

	int64_t n_u;
	for (k = ch_n-1, n_u = 0; k >= 0; --k) {
		if(track[k]&((uint64_t)0x80000000)) continue;
		i = k; ch[n_u]=sv[i]; sc = pop_sc(track[i]);
		for (;i>=0;) {
			track[i] |= ((uint64_t)0x80000000);
			if(sv[i].qs < ch[n_u].qs) ch[n_u].qs = sv[i].qs;
			if(sv[i].ts < ch[n_u].ts) ch[n_u].ts = sv[i].ts;
			if(sv[i].qe > ch[n_u].qe) ch[n_u].qe = sv[i].qe;
			if(sv[i].te > ch[n_u].te) ch[n_u].te = sv[i].te;
			// ch[n_u].qn = i;//start idx of read alignment in chain
			i = pop_pre(track[i]);
		}
		adjust_rev_tse(&(ch[n_u]), ug->g->seq[ch[n_u].tn].len, &its, &ite);
		ch[n_u].ts = its; ch[n_u].te = ite; ch[n_u].sec = (sc>0x3FFFFFFF?0x3FFFFFFF:sc);
		// ch[n_u].qn += chain_offset; //start idx of read alignment in chain
		// ch[n_u].tn = k + chain_offset; //end idx of read alignment in chain
		ch[n_u].qn = k + chain_offset; //end idx of read alignment in chain
		n_u++;
	}
	for (i = 0; i < ch_n; ++i) {
		adjust_rev_tse(&(sv[i]), ug->g->seq[sv[i].tn].len, &its, &ite);
		sv[i].ts = its; sv[i].te = ite; 
		k = pop_pre(track[i]); sv[i].tn = k>=0?k+chain_offset:(uint32_t)-1;
	}
	return n_u;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm0, 0x58(%rsp)
movq %r9, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
testq %rsi, %rsi
je 0x1ced66
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rdi, %rsi
addq %r14, %rsi
callq 0x19e2b4
testq %r14, %r14
jle 0x1ced66
movq %r14, 0x10(%rsp)
negq %r14
leaq 0x1c(%rbp), %r12
movl $0x1, %r15d
xorl %edi, %edi
movq %r14, %rax
addq %r15, %rax
je 0x1ce7f8
movl 0x8(%r12), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%rbp,%rcx), %eax
jne 0x1ce7f8
movq %rdi, %rax
jmp 0x1ce820
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1ce820
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbp, %rdi
addq %rax, %rdi
movq %r12, %rsi
callq 0x19e55c
movq %r15, %rax
leaq (%r14,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r12
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1ce7d5
leaq -0x4(%rbp), %rax
movq %rax, 0x40(%rsp)
movq $-0x1, 0x38(%rsp)
xorl %edi, %edi
movq 0x10(%rsp), %r15
movq 0xc8(%rsp), %r12
movq %rbp, 0x48(%rsp)
movq %rdi, 0x8(%rsp)
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rdx
addq %rdi, %rdx
movq 0xd0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl 0xc(%rbp,%rdx), %esi
movl 0x10(%rbp,%rdx), %edi
movl (%rax,%rsi,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl 0x14(%rbp,%rdx), %eax
movq %r8, %rcx
subq %rax, %rcx
subq %rdi, %r8
cmpl $0x0, 0x18(%rbp,%rdx)
cmovnsq %rdi, %rcx
movq %rdx, 0x70(%rsp)
leaq (%rbp,%rdx), %r14
cmovnsq %rax, %r8
movq $0x0, 0x50(%rsp)
movq 0x30(%rsp), %rdi
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
movq %r8, 0x68(%rsp)
leaq 0x50(%rsp), %r9
callq 0x363d1
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1cead7
movq $-0x1, 0x18(%rsp)
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %r15
movl -0x14(%r12), %esi
movl 0x4(%r14), %r10d
cmpl %r10d, %esi
ja 0x1ce9cb
movl -0x10(%r12), %r11d
movl 0x8(%r14), %edx
cmpl %edx, %r11d
ja 0x1ce9cb
movl -0x8(%r12), %ecx
movl 0x10(%r14), %r13d
cmpl %r13d, %ecx
ja 0x1ce9cb
movl -0x4(%r12), %r9d
movl 0x14(%r14), %r8d
cmpl %r8d, %r9d
ja 0x1ce9cb
movq %rax, 0x20(%rsp)
movl 0xc(%r14), %eax
cmpl -0xc(%r12), %eax
jne 0x1ced77
movl (%r12), %edi
movl 0x18(%r14), %ebp
xorl %edi, %ebp
js 0x1ced77
cmpl %r10d, %esi
movq 0x48(%rsp), %rbp
jne 0x1ce97e
cmpl %edx, %r11d
jne 0x1ce97e
cmpl %r13d, %ecx
jne 0x1ce97e
cmpl %r8d, %r9d
je 0x1ce9c1
subl %esi, %edx
subl %ecx, %r8d
movq %rdx, %r10
subq %r8, %r10
movq %r8, %r11
cmovaq %rdx, %r11
movq %r10, %rsi
negq %rsi
cmpl %r8d, %edx
cmovaq %r10, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
mulsd 0x58(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
cmpq 0x28(%rsp), %rdx
jg 0x1ce9bc
movq 0x28(%rsp), %rdx
cmpq %rdx, %rsi
jle 0x1ce9e8
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rax
leaq -0x1(%r15), %rcx
addq $-0x1c, %r12
incq %r15
cmpq $0x1, %r15
movq %rcx, %r15
jg 0x1ce8f9
jmp 0x1cea89
movq 0xd0(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, %rsi
subq %r9, %rsi
subq %rcx, %rdx
testl %edi, %edi
cmovnsq %rcx, %rsi
cmovnsq %r9, %rdx
movq %rax, %r9
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %r8
callq 0x1ce6cc
movq 0xc8(%rsp), %rcx
movq (%rcx,%r15,8), %rdx
movq %rdx, %rcx
shrq $0x20, %rcx
leaq -0x80000000(%rcx), %rsi
negq %rcx
testq %rdx, %rdx
cmovsq %rsi, %rcx
addq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rax, %rcx
movq 0x8(%rsp), %rdi
jg 0x1cea7c
jne 0x1ce9cb
cmpq $-0x1, 0x18(%rsp)
jne 0x1ce9cb
movq %rcx, %rax
movq %r15, 0x18(%rsp)
jmp 0x1ce9cb
movl %eax, %edx
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %edx
movl %eax, %ecx
negl %ecx
testq %rax, %rax
cmovnsq %rdx, %rcx
shlq $0x20, %rcx
movl %edi, %eax
movq 0x18(%rsp), %r8
subl %r8d, %eax
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %eax
testq %r8, %r8
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmovsq %rsi, %r8
cmovsl %edx, %eax
orq %rcx, %r8
movq 0xc8(%rsp), %r12
movq %r8, (%r12,%rdi,8)
movq 0x10(%rsp), %r15
jmp 0x1ceaff
movl %eax, %edx
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %edx
movl %eax, %ecx
negl %ecx
testq %rax, %rax
cmovnsq %rdx, %rcx
shlq $0x20, %rcx
orq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
movq %rcx, (%r12,%rdi,8)
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl 0x18(%r14), %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x18(%r14)
movups (%r14), %xmm0
movq 0x70(%rsp), %rax
movups %xmm0, (%rbx,%rax)
movupd 0xc(%r14), %xmm0
movupd %xmm0, 0xc(%rbx,%rax)
incq %rdi
incq 0x38(%rsp)
addq $0x1c, 0x40(%rsp)
cmpq %r15, %rdi
jne 0x1ce85d
xorl %esi, %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r15, %rdi
movq 0xd8(%rsp), %r14
movl $0x80000000, %r8d       # imm = 0x80000000
leaq -0x1(%rdi), %rax
testb $-0x80, -0x5(%r12,%rdi,8)
jne 0x1ceccd
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
leaq (%rsi,%rsi,8), %r9
leaq (%r9,%r9,2), %r11
movq %rsi, 0x8(%rsp)
addq %rsi, %r11
leaq (%r11,%rbp), %r9
movupd (%rbx,%rdx), %xmm0
movups 0xc(%rbx,%rdx), %xmm1
movups %xmm1, 0xc(%rbp,%r11)
movupd %xmm0, (%rbp,%r11)
movq (%r12,%rax,8), %rdx
movq %rdx, %r10
shrq $0x20, %r10
leaq -0x80000000(%r10), %r14
negq %r10
testq %rdx, %rdx
cmovsq %r14, %r10
movl 0x4(%rbp,%r11), %r14d
movl 0x8(%rbp,%r11), %r15d
movq %rbp, %rdx
movl 0x10(%rbp,%r11), %ebp
movl 0x14(%rdx,%r11), %r11d
movq %rax, %r12
movq 0xc8(%rsp), %rsi
movq (%rsi,%r12,8), %rdx
movq %rdx, %r13
orq %r8, %r13
movq %r13, (%rsi,%r12,8)
leaq (%r12,%r12,8), %r13
leaq (%r13,%r13,2), %r13
addq %r12, %r13
movl 0x4(%rbx,%r13), %r12d
cmpl %r14d, %r12d
jae 0x1cec07
movl %r12d, 0x4(%r9)
movl %r12d, %r14d
addq %rbx, %r13
movl 0x10(%r13), %r12d
cmpl %ebp, %r12d
jae 0x1cec1a
movl %r12d, 0x10(%r9)
movl %r12d, %ebp
movl 0x8(%r13), %r12d
cmpl %r15d, %r12d
jbe 0x1cec2a
movl %r12d, 0x8(%r9)
movl %r12d, %r15d
movl 0x14(%r13), %r12d
cmpl %r11d, %r12d
jbe 0x1cec3a
movl %r12d, 0x14(%r9)
movl %r12d, %r11d
andq %rcx, %rdx
movq %rdx, %r12
cmpq %rcx, %rdx
jne 0x1cebd4
movq 0xd0(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl 0xc(%r9), %r14d
movl 0x18(%r9), %r15d
movl (%rdx,%r14,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
movl %edx, %r14d
subl %r11d, %r14d
subl %ebp, %edx
testl %r15d, %r15d
cmovnsl %ebp, %r14d
cmovnsl %r11d, %edx
movl %r14d, 0x10(%r9)
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
cmpq %rsi, %r10
cmovgeq %rsi, %r10
movl %edx, 0x14(%r9)
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
andl $0xc0000000, %r15d      # imm = 0xC0000000
orl %r10d, %r15d
movl %r15d, 0x18(%r9)
movq 0xd8(%rsp), %r14
leal (%rax,%r14), %edx
movl %edx, (%r9)
movq 0x8(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %r15
movq 0xc8(%rsp), %r12
movq 0x48(%rsp), %rbp
cmpq $0x1, %rdi
movq %rax, %rdi
jg 0x1ceb58
movq %rsi, %rax
movq 0xd0(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
addq $0xc, %rbx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x80000000, %r13d      # imm = 0x80000000
addl %esi, %r13d
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl (%rbx), %r9d
movl 0x4(%rbx), %r10d
movl (%rcx,%r9,8), %r9d
andl %esi, %r9d
movl 0x8(%rbx), %r11d
movl %r9d, %ebp
subl %r11d, %ebp
subl %r10d, %r9d
cmpl $0x0, 0xc(%rbx)
cmovnsl %r10d, %ebp
cmovnsl %r11d, %r9d
movl %ebp, 0x4(%rbx)
movl %r9d, 0x8(%rbx)
movq (%r12,%rdx,8), %r9
andq %rdi, %r9
cmpq %rdi, %r9
movl %r9d, %r10d
cmovel %r13d, %r10d
addl %r14d, %r10d
cmpq %rdi, %r9
cmovel %r8d, %r10d
movl %r10d, (%rbx)
incq %rdx
addq $0x1c, %rbx
cmpq %rdx, %r15
jne 0x1ced0c
jmp 0x1ced68
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x73506(%rip), %rdi     # 0x242284
leaq 0x70ed6(%rip), %rsi     # 0x23fc5b
leaq 0x7351f(%rip), %rcx     # 0x2422ab
movl $0x3460, %edx           # imm = 0x3460
callq 0x2110