// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: linear_chain_dp_adv(ul_ov_t*, long, ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, long, long, Chain_Data*, ma_ug_t*, long)
uint64_t linear_chain_dp_adv(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, int64_t max_iter, int64_t max_dis, Chain_Data* dp, 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, max;
    ul_ov_t *li = NULL, *lj = NULL; int64_t *p, *t, st, plus, max_ii, n_skip, end_j; int32_t *f;
	resize_Chain_Data(dp, ch_n, NULL);
	t = dp->tmp; f = dp->score; p = dp->pre;

    radix_sort_ul_ov_srt_qe(ch, ch + ch_n);
	for (i = 1, j = 0; i <= ch_n; i++) {
        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);
	memset(t, 0, (ch_n*sizeof((*t))));
	for (i = st = plus = 0, max_ii = -1; i < ch_n; ++i) {
		li = &(ch[i]); csc = li->qe - li->qs;
		mm_sc = csc; mm_idx = -1; n_skip = 0; end_j = -1; 
		st = (i<max_iter)?(0):(i-max_iter);
		for (j = i-1; j >= st; --j) {
			lj = &(ch[j]); 
			sc = comput_linear_sc(li, lj, diff_ec_ul, bw); ///should allow contain
			if(sc == INT32_MIN) continue;
			sc += f[j];
			if(sc > mm_sc) {
				mm_sc = sc, mm_idx = j;
				if (n_skip > 0) --n_skip;
			} else if (t[j] == i) {
				if (++n_skip > max_skip)
					break;
			}
			if (p[j] >= 0) t[p[j]] = i;
		}
		end_j = j;
		if (max_ii < 0 || (ch[i].qe>(ch[max_ii].qe+max_dis))) {//too long
			max = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && (ch[i].qe<=(max_dis+ch[j].qe)); --j) {
                if (max < f[j]) {
                    max = f[j], max_ii = j;
                }
            }
		}
		
        if (max_ii >= 0 && max_ii < end_j) {///just have a try with a[i]<->a[max_ii]
			lj = &(ch[max_ii]); 
			sc = comput_linear_sc(li, lj, diff_ec_ul, bw); ///should allow contain
			if(sc != INT32_MIN) {
				sc += f[max_ii];
				if(sc > mm_sc) {
					mm_sc = sc; mm_idx = max_ii;
				}
			}
		}

		f[i] = mm_sc; p[i] = mm_idx; 
        if ((max_ii < 0) || ((ch[i].qe<=max_dis+ch[max_ii].qe) && (f[max_ii]<f[i]))) {
            max_ii = i;
        }
		li->sec = (mm_idx<0?0x3FFFFFFF:i-mm_idx); sv[i] = *li;
		// 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;
	}

	for (i = 0; i < ch_n; ++i) t[i] = 0;
	int64_t n_u;
	for (k = ch_n-1, n_u = 0; k >= 0; --k) {
		if(t[k]) continue;
		i = k; ch[n_u]=sv[i]; sc = f[i];
		for (;i>=0;) {
			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
			t[i] = 1; i = p[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 = p[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
movq %r9, 0x38(%rsp)
testq %rsi, %rsi
je 0x1c547e
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r13
movsd %xmm0, 0x60(%rsp)
movq 0xd0(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x3f828
movq 0x28(%rbp), %rax
movq %rax, 0x8(%rsp)
movq (%rbp), %rax
movq %rax, (%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x18(%rsp)
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
addq %rbx, %rsi
movq %r13, %rdi
callq 0x195511
testq %rbx, %rbx
jle 0x1c4ef2
movq %rbx, %r14
negq %r14
leaq 0x1c(%r13), %rbp
movl $0x1, %r15d
xorl %edi, %edi
movq %r14, %rax
addq %r15, %rax
je 0x1c4eb3
movl 0x8(%rbp), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r13,%rcx), %eax
jne 0x1c4eb3
movq %rdi, %rax
jmp 0x1c4edb
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1c4edb
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rdi
addq %rax, %rdi
movq %rbp, %rsi
callq 0x1957bd
movq %r15, %rax
leaq (%r14,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %rbp
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1c4e91
leaq (,%rbx,8), %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %rdx, 0x58(%rsp)
callq 0x20e0
testq %rbx, %rbx
movq %rbx, 0x28(%rsp)
movq %r13, 0x40(%rsp)
movsd 0x60(%rsp), %xmm1
jle 0x1c5280
leaq -0x8(%r13), %rax
movq %rax, 0x48(%rsp)
leaq -0x14(%r13), %r15
movq $-0x1, %r11
xorl %r8d, %r8d
movq %r15, 0x70(%rsp)
leaq (%r8,%r8,8), %rax
leaq (%rax,%rax,2), %rax
addq %r8, %rax
leaq (%rax,%r13), %rcx
movq %rcx, 0x50(%rsp)
movl 0x8(%r13,%rax), %ecx
movq %rax, 0x68(%rsp)
movl 0x4(%r13,%rax), %eax
movq %rcx, 0x20(%rsp)
subl %eax, %ecx
movq %r8, %rax
subq 0xc0(%rsp), %rax
movl $0x0, %edi
cmovgeq %rax, %rdi
leaq -0x1(%r8), %r9
movq %rcx, 0x30(%rsp)
movq %rcx, %rax
movq $-0x1, 0x10(%rsp)
movq %r9, %rsi
cmpq %rdi, %r8
movq 0x18(%rsp), %rdx
jle 0x1c50ae
movq 0x50(%rsp), %rax
movl 0x14(%rax), %ecx
movq $-0x1, 0x10(%rsp)
movq 0x48(%rsp), %r15
xorl %r13d, %r13d
movq %r9, %rbp
movq 0x30(%rsp), %r10
movq $-0x80000000, %rax      # imm = 0x80000000
cmpl %ecx, (%r15)
ja 0x1c5036
movq %r12, %rdx
movl -0x10(%r15), %eax
movq 0x20(%rsp), %r14
subl %eax, %r14d
movl %ecx, %eax
subl -0x4(%r15), %eax
movq %r14, %rsi
subq %rax, %rsi
movq %rax, %rbx
cmovaq %r14, %rbx
movq %rsi, %r12
negq %r12
cmpl %eax, %r14d
cmovaq %rsi, %r12
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r14
cmpq 0x38(%rsp), %r14
jg 0x1c501d
movq 0x38(%rsp), %r14
movq $-0x80000000, %rax      # imm = 0x80000000
cmpq %r14, %r12
jg 0x1c502e
movq 0x30(%rsp), %rax
movq %rdx, %r12
movq 0x18(%rsp), %rdx
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x1c5043
movq %r10, %rax
jmp 0x1c5097
movq (%rsp), %rsi
movslq (%rsi,%rbp,4), %rsi
addq %rsi, %rax
cmpq %r10, %rax
jle 0x1c5066
xorl %esi, %esi
testq %r13, %r13
setg %sil
subq %rsi, %r13
movq %rbp, 0x10(%rsp)
jmp 0x1c5085
movq 0x8(%rsp), %rax
cmpq %r8, (%rax,%rbp,8)
jne 0x1c5082
cmpq 0xb8(%rsp), %r13
jge 0x1c5275
incq %r13
movq %r10, %rax
movq (%rdx,%rbp,8), %rsi
testq %rsi, %rsi
js 0x1c5097
movq 0x8(%rsp), %r10
movq %r8, (%r10,%rsi,8)
leaq -0x1(%rbp), %rsi
addq $-0x1c, %r15
movq %rax, %r10
cmpq %rdi, %rbp
movq %rsi, %rbp
jg 0x1c4fc5
testq %r11, %r11
movq 0x40(%rsp), %r13
movq 0xc8(%rsp), %r14
movq 0x70(%rsp), %r15
movq 0x50(%rsp), %rbp
js 0x1c50e4
leaq (%r11,%r11,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r11, %rcx
movl 0x8(%r13,%rcx), %ecx
addq %r14, %rcx
cmpq 0x20(%rsp), %rcx
jge 0x1c512a
movq $-0x1, %r11
cmpq %rdi, %r8
jle 0x1c512a
movq $-0x80000000, %rcx      # imm = 0x80000000
movq %r15, %r10
movl (%r10), %ebx
addq %r14, %rbx
cmpq 0x20(%rsp), %rbx
jl 0x1c512a
movq (%rsp), %rdx
movslq (%rdx,%r9,4), %rbx
cmpq %rbx, %rcx
cmovleq %rbx, %rcx
cmovlq %r9, %r11
leaq -0x1(%r9), %rbx
addq $-0x1c, %r10
cmpq %rdi, %r9
movq %rbx, %r9
jg 0x1c50fa
testq %r11, %r11
sets %cl
cmpq %rsi, %r11
setge %sil
orb %cl, %sil
leaq (%r11,%r11,8), %rcx
jne 0x1c51de
leaq (%rcx,%rcx,2), %rdi
addq %r11, %rdi
movl 0x14(%rbp), %esi
movq $-0x80000000, %r9       # imm = 0x80000000
cmpl %esi, 0x14(%r13,%rdi)
movq 0x28(%rsp), %rbx
ja 0x1c51b8
addq %r13, %rdi
movq 0x20(%rsp), %rdx
subl 0x4(%rdi), %edx
subl 0x10(%rdi), %esi
movq %rdx, %rdi
subq %rsi, %rdi
movq %rsi, %r9
cmovaq %rdx, %r9
movq %rdi, %r10
negq %r10
cmpl %esi, %edx
cmovaq %rdi, %r10
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
movq 0x38(%rsp), %rsi
cmpq %rsi, %rdx
cmovleq %rsi, %rdx
cmpq %rdx, %r10
movq $-0x80000000, %rdx      # imm = 0x80000000
movq 0x30(%rsp), %r9
cmovgq %rdx, %r9
cmpq $-0x80000000, %r9       # imm = 0x80000000
je 0x1c51e3
movq (%rsp), %rdx
movslq (%rdx,%r11,4), %rdx
addq %rdx, %r9
cmpq %rax, %r9
cmovgq %r9, %rax
movq 0x10(%rsp), %rsi
cmovgq %r11, %rsi
jmp 0x1c51e8
movq 0x28(%rsp), %rbx
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
movl %eax, (%rdx,%r8,4)
movq 0x18(%rsp), %rdx
movq %rsi, (%rdx,%r8,8)
testq %r11, %r11
js 0x1c521f
movl 0x8(%rbp), %edx
leaq (%rcx,%rcx,2), %rcx
addq %r11, %rcx
movl 0x8(%r13,%rcx), %ecx
addq %r14, %rcx
cmpq %rdx, %rcx
jl 0x1c5222
movq (%rsp), %rcx
cmpl %eax, (%rcx,%r11,4)
jge 0x1c5222
movq %r8, %r11
movl %r8d, %eax
subl %esi, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
testq %rsi, %rsi
cmovsl %ecx, %eax
movl 0x18(%rbp), %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x18(%rbp)
movups (%rbp), %xmm0
movq 0x68(%rsp), %rax
movups %xmm0, (%r12,%rax)
movupd 0xc(%rbp), %xmm0
movupd %xmm0, 0xc(%r12,%rax)
incq %r8
addq $0x1c, 0x48(%rsp)
addq $0x1c, %r15
cmpq %rbx, %r8
jne 0x1c4f3b
jmp 0x1c5280
movq %r10, %rax
movq %rbp, %rsi
jmp 0x1c50ae
testq %rbx, %rbx
jle 0x1c5296
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq 0x58(%rsp), %rdx
callq 0x20e0
movq 0xe0(%rsp), %r15
xorl %eax, %eax
testq %rbx, %rbx
jle 0x1c53fe
movq %rbx, %r8
leaq -0x1(%r8), %rsi
movq 0x8(%rsp), %rcx
cmpq $0x0, -0x8(%rcx,%r8,8)
jne 0x1c53f1
movq %r15, %rdi
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rsi, %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
leaq (%rdx,%r13), %r10
movupd (%r12,%rcx), %xmm0
movupd 0xc(%r12,%rcx), %xmm1
movupd %xmm1, 0xc(%r13,%rdx)
movupd %xmm0, (%r13,%rdx)
movq (%rsp), %rcx
movl (%rcx,%rsi,4), %r11d
movl 0x4(%r13,%rdx), %ecx
movl 0x8(%r13,%rdx), %r14d
movl 0x10(%r13,%rdx), %r15d
movl 0x14(%r13,%rdx), %r13d
movq %rsi, %r9
movq 0x18(%rsp), %rbx
leaq (%r9,%r9,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r9, %rdx
movl 0x4(%r12,%rdx), %ebp
cmpl %ecx, %ebp
jae 0x1c5337
movl %ebp, 0x4(%r10)
movl %ebp, %ecx
addq %r12, %rdx
movl 0x10(%rdx), %ebp
cmpl %r15d, %ebp
jae 0x1c5349
movl %ebp, 0x10(%r10)
movl %ebp, %r15d
movl 0x8(%rdx), %ebp
cmpl %r14d, %ebp
jbe 0x1c5358
movl %ebp, 0x8(%r10)
movl %ebp, %r14d
movl 0x14(%rdx), %edx
cmpl %r13d, %edx
jbe 0x1c5367
movl %edx, 0x14(%r10)
movl %edx, %r13d
movq 0x8(%rsp), %rdx
movq $0x1, (%rdx,%r9,8)
movq (%rbx,%r9,8), %r9
testq %r9, %r9
jns 0x1c531d
movq 0xd8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0xc(%r10), %edx
movl 0x10(%r10), %r9d
movl (%rcx,%rdx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
movl 0x14(%r10), %edx
movl 0x18(%r10), %ebx
movl %ecx, %ebp
subl %edx, %ebp
subl %r9d, %ecx
testl %ebx, %ebx
cmovnsl %r9d, %ebp
cmovnsl %edx, %ecx
movl %ebp, 0x10(%r10)
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
cmpl %edx, %r11d
cmovgel %edx, %r11d
movl %ecx, 0x14(%r10)
andl %edx, %r11d
andl $0xc0000000, %ebx       # imm = 0xC0000000
orl %r11d, %ebx
movl %ebx, 0x18(%r10)
movq %rdi, %r15
leal (%rsi,%rdi), %ecx
movl %ecx, (%r10)
incq %rax
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r13
cmpq $0x1, %r8
movq %rsi, %r8
jg 0x1c52ac
testq %rbx, %rbx
jle 0x1c5480
movq 0xd8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rdx
addq $0xc, %r12
xorl %esi, %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r14
movl (%r12), %ecx
movl 0x4(%r12), %r9d
movl (%rdx,%rcx,8), %ecx
andl %edi, %ecx
movl 0x8(%r12), %r10d
movl %ecx, %r11d
subl %r10d, %r11d
subl %r9d, %ecx
cmpl $0x0, 0xc(%r12)
cmovnsl %r9d, %r11d
cmovnsl %r10d, %ecx
movl %r11d, 0x4(%r12)
movl %ecx, 0x8(%r12)
movq (%r14,%rsi,8), %rcx
leal (%rcx,%r15), %r9d
testq %rcx, %rcx
cmovsl %r8d, %r9d
movl %r9d, (%r12)
incq %rsi
addq $0x1c, %r12
cmpq %rsi, %rbx
jne 0x1c5429
jmp 0x1c5480
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq