// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movsd %xmm0, 0x60(%rsp)
movq %r9, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x10(%rsp)
testq %rsi, %rsi
je 0x182fc1
movq %rsi, %rbx
movq %rdi, %r13
imulq $0x1c, %rsi, %rsi
addq %rdi, %rsi
callq 0x156f5c
xorl %eax, %eax
testq %rbx, %rbx
movl $0x0, %r15d
movq %rbx, 0x30(%rsp)
cmovgq %rbx, %r15
movq %r15, %rbx
negq %rbx
leaq 0x1c(%r13), %r12
pushq $0x1
popq %r14
leaq (%rbx,%r14), %rcx
cmpq $0x1, %rcx
je 0x182ab2
cmpq %r14, 0x30(%rsp)
je 0x182a85
movl 0x8(%r12), %ecx
imulq $0x1c, %rax, %rdx
cmpl 0x8(%r13,%rdx), %ecx
jne 0x182a85
movq %rax, %rcx
jmp 0x182aa6
movq %r14, %rdx
subq %rax, %rdx
movq %r14, %rcx
cmpq $0x2, %rdx
jl 0x182aa6
imulq $0x1c, %rax, %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x1571fa
movq %r14, %rcx
incq %r14
addq $0x1c, %r12
movq %rcx, %rax
jmp 0x182a5f
leaq -0x4(%r13), %rbx
pushq $-0x1
popq %rax
xorl %edx, %edx
movq %rax, 0x28(%rsp)
movq %r15, 0x40(%rsp)
movq %r13, 0x38(%rsp)
cmpq %r15, %rdx
je 0x182d65
movq %rdx, 0x78(%rsp)
imulq $0x1c, %rdx, %rdx
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl 0xc(%r13,%rdx), %esi
movl 0x10(%r13,%rdx), %edi
movl (%rax,%rsi,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x14(%r13,%rdx), %eax
movq %rbp, %rcx
subq %rax, %rcx
subq %rdi, %rbp
cmpl $0x0, 0x18(%r13,%rdx)
cmovnsq %rdi, %rcx
cmovnsq %rax, %rbp
movq %rdx, 0x70(%rsp)
leaq (%rdx,%r13), %r15
andq $0x0, 0x58(%rsp)
movq 0x50(%rsp), %rdi
xorl %edx, %edx
movq %rcx, 0x68(%rsp)
movq %rbp, %r8
leaq 0x58(%rsp), %r9
callq 0xa240
movq %rax, 0x20(%rsp)
movq %rbx, 0x80(%rsp)
movq %rbx, %r13
movq 0x28(%rsp), %rbx
pushq $-0x1
popq %rax
movq %rax, 0x18(%rsp)
leaq 0x1(%rbx), %rax
testq %rax, %rax
jle 0x182cc2
movl -0x14(%r13), %esi
movl 0x4(%r15), %r10d
cmpl %r10d, %esi
ja 0x182cb6
movl -0x10(%r13), %r11d
movl 0x8(%r15), %edx
cmpl %edx, %r11d
ja 0x182cb6
movl -0x8(%r13), %eax
movl 0x10(%r15), %r14d
cmpl %r14d, %eax
ja 0x182cb6
movl -0x4(%r13), %ecx
movl 0x14(%r15), %r8d
cmpl %r8d, %ecx
ja 0x182cb6
movl 0xc(%r15), %r9d
cmpl -0xc(%r13), %r9d
jne 0x182fd5
movl (%r13), %edi
movl 0x18(%r15), %r12d
xorl %edi, %r12d
js 0x182fd5
cmpl %r10d, %esi
jne 0x182be8
cmpl %edx, %r11d
jne 0x182be8
cmpl %r14d, %eax
jne 0x182be8
cmpl %r8d, %ecx
je 0x182cb6
subl %esi, %edx
subl %eax, %r8d
movq %rdx, %r10
subq %r8, %r10
movq %r8, %r11
cmovaq %rdx, %r11
movq %r10, %rsi
negq %rsi
cmpl %r8d, %edx
cmovaq %r10, %rsi
cvtsi2sd %r11, %xmm0
mulsd 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
cmpq 0x48(%rsp), %rdx
jg 0x182c23
movq 0x48(%rsp), %rdx
cmpq %rdx, %rsi
jg 0x182cb6
movq 0xe0(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%r9,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, %rsi
subq %rcx, %rsi
subq %rax, %rdx
testl %edi, %edi
cmovnsq %rax, %rsi
cmovnsq %rcx, %rdx
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rcx
movq %rbp, %r8
callq 0x182953
movq 0xd8(%rsp), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rdx, %rcx
shrq $0x20, %rcx
leaq -0x80000000(%rcx), %rsi
negq %rcx
testq %rdx, %rdx
cmovsq %rsi, %rcx
addq %rax, %rcx
cmpq 0x20(%rsp), %rcx
jg 0x182cac
jne 0x182cb6
cmpq $-0x1, 0x18(%rsp)
jne 0x182cb6
movq %rcx, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
decq %rbx
addq $-0x1c, %r13
jmp 0x182b60
movq 0x20(%rsp), %rdx
movl %edx, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
movl %edx, %ecx
negl %ecx
testq %rdx, %rdx
cmovnsq %rax, %rcx
shlq $0x20, %rcx
movq 0x78(%rsp), %rdx
movl %edx, %eax
movq 0x18(%rsp), %r8
subl %r8d, %eax
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl %esi, %eax
testq %r8, %r8
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmovsq %rdi, %r8
cmovsl %esi, %eax
orq %rcx, %r8
movq 0xd8(%rsp), %rcx
movq %r8, (%rcx,%rdx,8)
movl 0x18(%r15), %ecx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %ecx
orl %eax, %ecx
movl %ecx, 0x18(%r15)
movups (%r15), %xmm0
movq 0x10(%rsp), %rax
movq 0x70(%rsp), %rcx
movups %xmm0, (%rax,%rcx)
movupd 0xc(%r15), %xmm0
movupd %xmm0, 0xc(%rax,%rcx)
incq %rdx
incq 0x28(%rsp)
movq 0x80(%rsp), %rbx
addq $0x1c, %rbx
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %r13
jmp 0x182aca
movq 0xe8(%rsp), %rax
leaq -0x1(%rax), %r10
xorl %eax, %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rsi
movq 0x30(%rsp), %r8
leal (%r10,%r8), %r9d
incl %r9d
imulq $0x1c, %r8, %rdi
addq 0x10(%rsp), %rdi
testq %r8, %r8
jle 0x182f1a
leaq -0x1(%r8), %r11
decl %r9d
addq $-0x1c, %rdi
movq 0xd8(%rsp), %rcx
testb $-0x80, -0x5(%rcx,%r8,8)
movq %r11, %r8
jne 0x182d90
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
imulq $0x1c, %rax, %r8
leaq (%r8,%r13), %r10
movupd (%rdi), %xmm0
movups 0xc(%rdi), %xmm1
movups %xmm1, 0xc(%r13,%r8)
movupd %xmm0, (%r13,%r8)
movq %r11, %rdi
movq (%rcx,%r11,8), %rax
movq %rax, %r11
shrq $0x20, %r11
leaq -0x80000000(%r11), %rcx
movq %rcx, 0x28(%rsp)
movl 0x4(%r13,%r8), %ecx
movl 0x8(%r13,%r8), %r15d
movl 0x10(%r13,%r8), %r12d
movl 0x14(%r13,%r8), %r13d
movq %rdi, 0x30(%rsp)
movq %rdi, %r8
testq %rdi, %rdi
js 0x182e8d
movq 0xd8(%rsp), %r14
movq (%r14,%r8,8), %rdi
movq %rdi, %rbp
movl $0x80000000, %ebx       # imm = 0x80000000
orq %rbx, %rbp
movq %rbp, (%r14,%r8,8)
imulq $0x1c, %r8, %r8
movq 0x10(%rsp), %rbp
movl 0x4(%rbp,%r8), %ebp
cmpl %ecx, %ebp
jae 0x182e4c
movl %ebp, 0x4(%r10)
movl %ebp, %ecx
addq 0x10(%rsp), %r8
movl 0x10(%r8), %ebp
cmpl %r12d, %ebp
jae 0x182e61
movl %ebp, 0x10(%r10)
movl %ebp, %r12d
movl 0x8(%r8), %ebp
cmpl %r15d, %ebp
jbe 0x182e71
movl %ebp, 0x8(%r10)
movl %ebp, %r15d
movl 0x14(%r8), %r8d
cmpl %r13d, %r8d
jbe 0x182e81
movl %r8d, 0x14(%r10)
movl %r8d, %r13d
andq %rdx, %rdi
cmpq %rdx, %rdi
cmoveq %rsi, %rdi
jmp 0x182e11
negq %r11
testq %rax, %rax
cmovsq 0x28(%rsp), %r11
movq 0xe0(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0xc(%r10), %edi
movl 0x10(%r10), %r8d
movl (%rcx,%rdi,8), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movl 0x14(%r10), %edi
movl 0x18(%r10), %ebx
movl %ecx, %ebp
subl %edi, %ebp
subl %r8d, %ecx
testl %ebx, %ebx
cmovnsl %r8d, %ebp
cmovnsl %edi, %ecx
movl %ebp, 0x10(%r10)
movl %ecx, 0x14(%r10)
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpq %rax, %r11
cmovgeq %rax, %r11
andl $0x3fffffff, %r11d      # imm = 0x3FFFFFFF
andl $0xc0000000, %ebx       # imm = 0xC0000000
orl %r11d, %ebx
movl %ebx, 0x18(%r10)
movl %r9d, (%r10)
movq 0x20(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %r13
movq 0x18(%rsp), %r10
jmp 0x182d7b
addq $0xc, 0x10(%rsp)
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rdi
cmpq %rcx, %r15
je 0x182fc3
movq 0xe0(%rsp), %r8
movq 0x18(%r8), %r8
movq 0x20(%r8), %r8
movq 0x10(%rsp), %rbx
movl (%rbx), %r9d
movl 0x4(%rbx), %r10d
movl (%r8,%r9,8), %r8d
andl %edx, %r8d
movl 0x8(%rbx), %r9d
movl %r8d, %r11d
subl %r9d, %r11d
subl %r10d, %r8d
cmpl $0x0, 0xc(%rbx)
cmovnsl %r10d, %r11d
cmovnsl %r9d, %r8d
movl %r11d, 0x4(%rbx)
movl %r8d, 0x8(%rbx)
movq 0xd8(%rsp), %r8
movq (%r8,%rcx,8), %r8
andq %rsi, %r8
movl $0x80000000, %r9d       # imm = 0x80000000
addl %edx, %r9d
cmpq %rsi, %r8
cmovnel %r8d, %r9d
movq 0xe8(%rsp), %r10
addl %r10d, %r9d
cmpq %rsi, %r8
cmovel %edi, %r9d
movl %r9d, (%rbx)
incq %rcx
addq $0x1c, %rbx
movq %rbx, 0x10(%rsp)
jmp 0x182f2f
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x71170(%rip), %rdi     # 0x1f414c
leaq 0x6eb40(%rip), %rsi     # 0x1f1b23
leaq 0x71189(%rip), %rcx     # 0x1f4173
movl $0x3460, %edx           # imm = 0x3460
callq 0x2110