// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movsd %xmm0, 0x60(%rsp)
movq %r9, 0x58(%rsp)
testq %rsi, %rsi
je 0x1835aa
movq %rdx, %r13
movq %rdi, %r12
movq 0xe0(%rsp), %r15
xorl %ebx, %ebx
movq %r15, %rdi
xorl %edx, %edx
movq %rsi, 0x30(%rsp)
callq 0x1383e
movq 0x28(%r15), %rax
movq %rax, 0x8(%rsp)
movq (%r15), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
imulq $0x1c, 0x30(%rsp), %rsi
addq %r12, %rsi
movq %r12, %rdi
callq 0x156f5c
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
movl $0x0, %ebp
cmovgq %rsi, %rbp
movq %rbp, %r14
negq %r14
movq %r12, 0x20(%rsp)
leaq 0x1c(%r12), %r15
pushq $0x1
popq %r12
leaq (%r14,%r12), %rax
cmpq $0x1, %rax
je 0x1830e3
cmpq %r12, %rsi
je 0x1830af
movl 0x8(%r15), %eax
imulq $0x1c, %rbx, %rcx
movq 0x20(%rsp), %rdx
cmpl 0x8(%rdx,%rcx), %eax
jne 0x1830af
movq %rbx, %rax
jmp 0x1830d7
movq %r12, %rcx
subq %rbx, %rcx
movq %r12, %rax
cmpq $0x2, %rcx
jl 0x1830d7
imulq $0x1c, %rbx, %rdi
addq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x1571fa
movq 0x30(%rsp), %rsi
movq %r12, %rax
incq %r12
addq $0x1c, %r15
movq %rax, %rbx
jmp 0x183088
leaq (,%rsi,8), %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x20(%rsp), %r15
leaq -0x1c(%r15), %rcx
leaq -0x14(%r15), %rax
movq %rax, 0x50(%rsp)
pushq $-0x1
popq %rax
xorl %r8d, %r8d
movq 0x10(%rsp), %rsi
movq %r13, 0x48(%rsp)
movq %rbp, 0x68(%rsp)
cmpq %rbp, %r8
je 0x1833ad
movq %rax, 0x70(%rsp)
imulq $0x1c, %r8, %rax
leaq (%r15,%rax), %rdx
movq %rdx, 0x18(%rsp)
movl 0x8(%r15,%rax), %ebx
movq %rax, 0x78(%rsp)
subl 0x4(%r15,%rax), %ebx
movq %r8, %r14
subq 0xd0(%rsp), %r14
movl $0x0, %eax
cmovlq %rax, %r14
movq %r8, 0x38(%rsp)
leaq -0x1(%r8), %r12
movq %rcx, 0x80(%rsp)
movq %rcx, %r15
pushq $-0x1
popq %rax
movq %rax, 0x40(%rsp)
movq %r12, %r13
xorl %ebp, %ebp
cmpq %r14, %r13
jl 0x183219
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movsd 0x60(%rsp), %xmm0
movq 0x58(%rsp), %rdx
callq 0x1a33b8
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x1831ab
movq %rbx, %rax
jmp 0x18320a
movq 0x10(%rsp), %rcx
movslq (%rcx,%r13,4), %rcx
addq %rcx, %rax
cmpq %rbx, %rax
jle 0x1831ce
xorl %ecx, %ecx
testq %rbp, %rbp
setg %cl
subq %rcx, %rbp
movq %r13, 0x40(%rsp)
jmp 0x1831ee
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpq %rcx, (%rax,%r13,8)
jne 0x1831eb
cmpq 0xc8(%rsp), %rbp
jge 0x183219
incq %rbp
movq %rbx, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
testq %rcx, %rcx
js 0x18320a
movq 0x8(%rsp), %rdx
movq 0x38(%rsp), %rsi
movq %rsi, (%rdx,%rcx,8)
decq %r13
addq $-0x1c, %r15
movq %rax, %rbx
jmp 0x18317d
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
movq 0x68(%rsp), %rbp
movq 0x20(%rsp), %r15
js 0x18324b
movq 0x18(%rsp), %rax
movl 0x8(%rax), %eax
imulq $0x1c, %rdi, %rcx
movl 0x8(%r15,%rcx), %ecx
addq 0xd8(%rsp), %rcx
cmpq %rax, %rcx
jge 0x183293
movq $-0x80000000, %rax      # imm = 0x80000000
movq 0x50(%rsp), %rcx
pushq $-0x1
popq %rdi
cmpq %r14, %r12
jl 0x183293
movq 0x18(%rsp), %rdx
movl 0x8(%rdx), %edx
movl (%rcx), %esi
addq 0xd8(%rsp), %rsi
cmpq %rdx, %rsi
jl 0x183293
movq 0x10(%rsp), %rdx
movslq (%rdx,%r12,4), %rdx
cmpq %rdx, %rax
cmovleq %rdx, %rax
cmovlq %r12, %rdi
decq %r12
addq $-0x1c, %rcx
jmp 0x18325a
testq %rdi, %rdi
sets %al
cmpq %r13, %rdi
setge %cl
orb %al, %cl
movq %rdi, %r14
jne 0x1832ff
imulq $0x1c, %r14, %rsi
addq %r15, %rsi
movq 0x18(%rsp), %rdi
movsd 0x60(%rsp), %xmm0
movq 0x58(%rsp), %rdx
callq 0x1a33b8
cmpq $-0x80000000, %rax      # imm = 0x80000000
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %rdi
je 0x1832f0
movslq (%rsi,%r14,4), %rcx
addq %rcx, %rax
cmpq %rbx, %rax
cmovgq %rax, %rbx
cmovgq %r14, %rdi
movl %ebx, (%rsi,%r8,4)
movq 0x28(%rsp), %rax
movq %rdi, (%rax,%r8,8)
jmp 0x183328
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %r8
movl %ebx, (%rsi,%r8,4)
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rdi
movq %rdi, (%rax,%r8,8)
movq %r8, %rax
testq %r14, %r14
movq 0x48(%rsp), %r13
js 0x183351
movq 0x18(%rsp), %rax
movl 0x8(%rax), %eax
imulq $0x1c, %r14, %rcx
movl 0x8(%r15,%rcx), %ecx
addq 0xd8(%rsp), %rcx
cmpq %rax, %rcx
jl 0x18334e
cmpl %ebx, (%rsi,%r14,4)
cmovlq %r8, %r14
movq %r14, %rax
movl %r8d, %ecx
subl %edi, %ecx
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %ecx
testq %rdi, %rdi
cmovsl %edx, %ecx
movq 0x18(%rsp), %r9
movl 0x18(%r9), %edx
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %edx
orl %ecx, %edx
movl %edx, 0x18(%r9)
movups (%r9), %xmm0
movq 0x78(%rsp), %rcx
movups %xmm0, (%r13,%rcx)
movups 0xc(%r9), %xmm0
movups %xmm0, 0xc(%r13,%rcx)
incq %r8
movq 0x80(%rsp), %rcx
addq $0x1c, %rcx
addq $0x1c, 0x50(%rsp)
jmp 0x18311e
xorl %eax, %eax
movq 0xf0(%rsp), %r11
movq 0xe8(%rsp), %rbx
movq 0x30(%rsp), %r8
movq 0x8(%rsp), %rcx
cmpq %rax, %rbp
je 0x1833d8
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x1833c9
leaq -0x1(%r11), %rcx
xorl %eax, %eax
leal (%rcx,%r8), %edi
incl %edi
imulq $0x1c, %r8, %r9
addq %r13, %r9
testq %r8, %r8
jle 0x18353e
leaq -0x1(%r8), %r10
decl %edi
addq $-0x1c, %r9
movq 0x8(%rsp), %rdx
cmpq $0x0, -0x8(%rdx,%r8,8)
movq %r10, %r8
jne 0x1833eb
movq %rbp, %rdx
imulq $0x1c, %rax, %r14
leaq (%r15,%r14), %r8
movups (%r9), %xmm0
movups 0xc(%r9), %xmm1
movups %xmm1, 0xc(%r15,%r14)
movups %xmm0, (%r15,%r14)
movl (%rsi,%r10,4), %r9d
movq %r10, %r12
movl 0x4(%r15,%r14), %r10d
movl 0x8(%r15,%r14), %r11d
movl 0x10(%r15,%r14), %ebx
movl 0x14(%r15,%r14), %ebp
movq %r12, %rsi
movq %r12, %r14
testq %r14, %r14
js 0x1834b3
imulq $0x1c, %r14, %r15
movl 0x4(%r13,%r15), %r12d
cmpl %r10d, %r12d
jae 0x183468
movl %r12d, 0x4(%r8)
movl %r12d, %r10d
addq %r13, %r15
movl 0x10(%r15), %r12d
cmpl %ebx, %r12d
jae 0x18347b
movl %r12d, 0x10(%r8)
movl %r12d, %ebx
movl 0x8(%r15), %r12d
cmpl %r11d, %r12d
jbe 0x18348b
movl %r12d, 0x8(%r8)
movl %r12d, %r11d
movl 0x14(%r15), %r15d
cmpl %ebp, %r15d
jbe 0x18349b
movl %r15d, 0x14(%r8)
movl %r15d, %ebp
movq 0x8(%rsp), %r15
movq $0x1, (%r15,%r14,8)
movq 0x28(%rsp), %r15
movq (%r15,%r14,8), %r14
jmp 0x18344e
movq 0xe8(%rsp), %r15
movq 0x18(%r15), %r10
movq 0x20(%r10), %r10
movl 0xc(%r8), %r11d
movl 0x10(%r8), %ebx
movl (%r10,%r11,8), %r10d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r10d
movl 0x14(%r8), %r11d
movl 0x18(%r8), %ebp
movl %r10d, %r14d
subl %r11d, %r14d
subl %ebx, %r10d
testl %ebp, %ebp
cmovnsl %ebx, %r14d
movq %r15, %rbx
cmovnsl %r11d, %r10d
movl %r14d, 0x10(%r8)
movl %r10d, 0x14(%r8)
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
cmpl %r10d, %r9d
cmovgel %r10d, %r9d
andl %r10d, %r9d
andl $0xc0000000, %ebp       # imm = 0xC0000000
orl %r9d, %ebp
movl %ebp, 0x18(%r8)
movl %edi, (%r8)
incq %rax
movq 0xf0(%rsp), %r11
movq %rdx, %rbp
movq 0x20(%rsp), %r15
movq %rsi, %r8
movq 0x10(%rsp), %rsi
jmp 0x1833de
addq $0xc, %r13
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rsi
cmpq %rcx, %rbp
je 0x1835ac
movq 0x18(%rbx), %rdi
movq 0x20(%rdi), %rdi
movl (%r13), %r8d
movl 0x4(%r13), %r9d
movl (%rdi,%r8,8), %edi
andl %edx, %edi
movl 0x8(%r13), %r8d
movl %edi, %r10d
subl %r8d, %r10d
subl %r9d, %edi
cmpl $0x0, 0xc(%r13)
cmovnsl %r9d, %r10d
cmovnsl %r8d, %edi
movl %r10d, 0x4(%r13)
movl %edi, 0x8(%r13)
movq 0x28(%rsp), %rdi
movq (%rdi,%rcx,8), %rdi
leal (%r11,%rdi), %r8d
testq %rdi, %rdi
cmovsl %esi, %r8d
movl %r8d, (%r13)
incq %rcx
addq $0x1c, %r13
jmp 0x18354c
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq