// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: linear_rchain_dp_adv(ul_ov_t*, long, ul_ov_t*, ug_opt_t const*, long, double, long, long, long, long, Chain_Data*, asg_t const*, long)
uint64_t linear_rchain_dp_adv(ul_ov_t *ch, int64_t ch_n, ul_ov_t *sv, 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, const asg_t *rg, int64_t ulid)
{	///all in[].el must be 1
	if(ch_n == 0) return 0;
    int64_t i, j, k, sc, csc, mm_sc, mm_idx, its, ite, max; int32_t *f, *bq, *bt;
    ul_ov_t *li = NULL, *lj = NULL; int64_t *p, *t, st, plus, max_ii, n_skip, end_j; 
	resize_Chain_Data(dp, ch_n, NULL);
	t = dp->tmp; f = dp->score; p = dp->pre; bq = dp->indels; bt = dp->self_length;

    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;
        }
    }

	memset(t, 0, (ch_n*sizeof((*t))));
	for (i = st = plus = 0, max_ii = -1; i < ch_n; ++i) {
		li = &(ch[i]); csc = MIN((li->qe-li->qs), (li->te-li->ts));
		mm_sc = /**csc**/-1; 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]); 
			if(lj->qe <= li->qs) break;
			sc = comput_rlinear_sc(li, lj, j, bq, bt, diff_ec_ul, bw); ///should allow contain
			if(sc == INT32_MIN) continue;
			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]); 
			if(lj->qe > li->qs) {
				sc = comput_rlinear_sc(li, lj, max_ii, bq, bt, diff_ec_ul, bw); ///should allow contain
				if(sc != INT32_MIN) {
					if(sc > mm_sc) {
						mm_sc = sc; mm_idx = max_ii;
					}
				}
			}
		}

		if(mm_idx < 0) mm_sc = csc;
		f[i] = mm_sc; p[i] = mm_idx; 
		bq[i] = li->qs; bt[i] = li->ts;
		if(mm_idx >= 0) {
			if(bq[i] > bq[mm_idx]) bq[i] = bq[mm_idx];
			if(bt[i] > bt[mm_idx]) bt[i] = bt[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;
		// if(ulid == 442462) {    
		// 	fprintf(stderr, "[M::%s::id->%u::%.*s]\tq::[%u, %u),\tt::[%u, %u),\tis_rev::%u\n", 
		// 	__func__, li->tn, (int)Get_NAME_LENGTH(R_INF, li->tn), Get_NAME(R_INF, li->tn), 
		// 	li->qs, li->qe, li->ts, li->te, li->rev);
		// }

	}
	// radix_sort_gfa64i
	for (i = 0; i < ch_n; ++i) {
		t[i] = ((uint64_t)f[i])<<32; t[i] += i; bt[i] = 0;
	}
	radix_sort_gfa64i(t, t+ch_n);
	int64_t n_u, z;
	for (z = ch_n-1, n_u = 0; z >= 0; --z) {
		k = (uint32_t)t[z];
		if(bt[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
			bt[i] = 1; i = p[i];
		}
		adjust_rev_tse(&(ch[n_u]), rg->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 = 0; 
		n_u++;
	}
	return n_u;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, 0x60(%rsp)
testq %rsi, %rsi
je 0x1db99d
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movsd %xmm0, 0x80(%rsp)
movq 0xf8(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq 0x28(%rbp), %rax
movq %rax, 0x10(%rsp)
movq (%rbp), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%rbp), %r15
movq 0x10(%rbp), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%rbp), %r12
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rsi
addq %rbx, %rsi
addq %r13, %rsi
movq %rbx, %rdi
callq 0x19e2b4
testq %r13, %r13
jle 0x1db9a1
movq %r12, 0x18(%rsp)
movq %r15, 0x50(%rsp)
movq %r13, %r15
negq %r15
leaq 0x1c(%rbx), %rbp
movl $0x1, %r12d
xorl %edi, %edi
movq %rbx, 0x28(%rsp)
movq 0x10(%rsp), %rsi
movq %r15, %rax
addq %r12, %rax
je 0x1db2be
movl 0x8(%rbp), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%rbx,%rcx), %eax
jne 0x1db2be
movq %rdi, %rax
jmp 0x1db2f0
movq %r12, %rcx
subq %rdi, %rcx
movq %r12, %rax
cmpq $0x2, %rcx
jl 0x1db2f0
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbx, %rdi
addq %rax, %rdi
movq %rbp, %rsi
callq 0x19e55c
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rbx
movq %r12, %rax
leaq (%r15,%r12), %rcx
incq %rcx
incq %r12
addq $0x1c, %rbp
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1db29d
leaq (,%r13,8), %rdx
xorl %ebp, %ebp
movq %rsi, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x28(%rsp), %r10
leaq -0x8(%r10), %rax
movq %rax, 0x78(%rsp)
leaq -0x14(%r10), %rax
movq %rax, 0x70(%rsp)
movq $-0x1, %rdi
movq 0x58(%rsp), %rbx
movsd 0x80(%rsp), %xmm1
movq %r14, 0x68(%rsp)
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %rax
addq %rbp, %rax
movl 0x4(%r10,%rax), %r12d
movl 0x8(%r10,%rax), %r11d
movl %r11d, %ecx
subl %r12d, %ecx
movl 0x14(%r10,%rax), %r8d
movq %rax, 0xa0(%rsp)
movl 0x10(%r10,%rax), %edx
movl %r8d, %eax
movl %edx, 0x44(%rsp)
subl %edx, %eax
cmpl %eax, %ecx
movl %eax, 0x3c(%rsp)
movl %ecx, 0x40(%rsp)
cmovbl %ecx, %eax
movq %rax, 0x98(%rsp)
movq %rbp, %rcx
subq 0xe8(%rsp), %rcx
movl $0x0, %eax
cmovgeq %rcx, %rax
leaq -0x1(%rbp), %r15
movq $-0x1, 0x8(%rsp)
movq $-0x1, %rcx
movq %r15, %rdx
cmpq %rax, %rbp
movq %r8, (%rsp)
jle 0x1db5dc
movl 0x40(%rsp), %ecx
movq %rcx, 0x90(%rsp)
movl 0x3c(%rsp), %ecx
movq %rcx, 0x88(%rsp)
movq $-0x1, %rsi
movq 0x78(%rsp), %r9
movq $0x0, 0x48(%rsp)
movq %r15, %r10
movq $-0x1, 0x8(%rsp)
movl %r12d, 0x24(%rsp)
movl -0xc(%r9), %edx
cmpl %r12d, %edx
jbe 0x1db5d6
movl (%r9), %ecx
cmpl %ecx, %r8d
jae 0x1db431
movq %rsi, %rcx
jmp 0x1db4f9
cmpl %edx, %r11d
cmovbl %r11d, %edx
movl -0x10(%r9), %ebx
cmpl %ebx, %r12d
movl %ebx, %r8d
cmoval %r12d, %r8d
cmpl %r8d, %edx
jbe 0x1db4d1
movq %rbp, %r12
movq %r14, %rbp
movq %r13, %r14
movl -0x4(%r9), %edx
movl 0x44(%rsp), %r13d
cmpl %edx, %r13d
movl %edx, %r8d
cmoval %r13d, %r8d
cmpl %r8d, %ecx
jbe 0x1db4df
movl %r11d, %r8d
subl %ebx, %r8d
movq (%rsp), %rbx
subl %edx, %ebx
movq %r8, %rdx
subq %rbx, %rdx
movq %rbx, %r13
cmovaq %r8, %r13
movq %rdx, %rcx
negq %rcx
cmpl %ebx, %r8d
cmovaq %rdx, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
cmpq 0x60(%rsp), %rdx
jg 0x1db4b3
movq 0x60(%rsp), %rdx
cmpq %rdx, %rcx
movq %r14, %r13
movq 0x58(%rsp), %rbx
movq %r12, %rbp
jle 0x1db515
movq %rsi, %rcx
movq 0x68(%rsp), %r14
movq (%rsp), %r8
jmp 0x1db4f4
movq %rsi, %rcx
movq 0x58(%rsp), %rbx
movq (%rsp), %r8
jmp 0x1db4f9
movq %rsi, %rcx
movq 0x58(%rsp), %rbx
movq (%rsp), %r8
movq %r14, %r13
movq %rbp, %r14
movq %r12, %rbp
movl 0x24(%rsp), %r12d
leaq -0x1(%r10), %rdx
addq $-0x1c, %r9
movq %rcx, %rsi
cmpq %rax, %r10
movq %rdx, %r10
jg 0x1db414
jmp 0x1db5dc
movq 0x30(%rsp), %rcx
movslq (%rcx,%r10,4), %rcx
movq %r11, %rdx
subq %rcx, %rdx
movq 0x18(%rsp), %rcx
movslq (%rcx,%r10,4), %r8
movq (%rsp), %rcx
subq %r8, %rcx
movq 0x90(%rsp), %r8
cmpq %r8, %rdx
cmovleq %r8, %rdx
movq 0x88(%rsp), %r8
cmpq %r8, %rcx
cmovleq %r8, %rcx
cmpq %rcx, %rdx
cmovlq %rdx, %rcx
cmpq %rsi, %rcx
movq 0x68(%rsp), %r14
movl 0x24(%rsp), %r12d
jle 0x1db584
xorl %edx, %edx
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
setg %dl
subq %rdx, %rsi
movq %rsi, 0x48(%rsp)
movq %r10, 0x8(%rsp)
jmp 0x1db5b2
movq 0x10(%rsp), %rcx
cmpq %rbp, (%rcx,%r10,8)
jne 0x1db5af
movq 0x48(%rsp), %rcx
cmpq 0xe0(%rsp), %rcx
movq (%rsp), %r8
jge 0x1db5d6
incq %rcx
movq %rcx, 0x48(%rsp)
movq %rsi, %rcx
jmp 0x1db5b6
movq %rsi, %rcx
movq (%rsp), %r8
movq 0x50(%rsp), %rdx
movq (%rdx,%r10,8), %rdx
testq %rdx, %rdx
js 0x1db4f9
movq 0x10(%rsp), %rsi
movq %rbp, (%rsi,%rdx,8)
jmp 0x1db4f9
movq %rsi, %rcx
movq %r10, %rdx
testq %rdi, %rdi
movq 0x28(%rsp), %r10
js 0x1db603
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl 0x8(%r10,%rsi), %esi
addq 0xf0(%rsp), %rsi
cmpq %r11, %rsi
jge 0x1db64e
movq $-0x1, %rdi
cmpq %rax, %rbp
jle 0x1db74d
movq $-0x80000000, %rsi      # imm = 0x80000000
movq 0x70(%rsp), %r9
movl (%r9), %r8d
addq 0xf0(%rsp), %r8
cmpq %r11, %r8
jl 0x1db64e
movslq (%rbx,%r15,4), %r8
cmpq %r8, %rsi
cmovleq %r8, %rsi
cmovlq %r15, %rdi
leaq -0x1(%r15), %r8
addq $-0x1c, %r9
cmpq %rax, %r15
movq %r8, %r15
jg 0x1db61f
testq %rdi, %rdi
sets %al
cmpq %rdx, %rdi
setge %dl
orb %al, %dl
movq 0x50(%rsp), %r15
jne 0x1db752
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rax
movl 0x8(%r10,%rax), %edx
cmpl %r12d, %edx
jbe 0x1db752
addq %r10, %rax
movl 0x14(%rax), %esi
cmpl %esi, (%rsp)
jb 0x1db752
cmpl %edx, %r11d
cmovbl %r11d, %edx
movl 0x4(%rax), %r8d
cmpl %r8d, %r12d
cmovbel %r8d, %r12d
cmpl %r12d, %edx
jbe 0x1db752
movl 0x10(%rax), %eax
movl 0x44(%rsp), %edx
cmpl %eax, %edx
cmovbel %eax, %edx
cmpl %edx, %esi
movq 0x18(%rsp), %r12
jbe 0x1db757
movl %r11d, %edx
subl %r8d, %edx
movq (%rsp), %rsi
subl %eax, %esi
movq %rdx, %rax
subq %rsi, %rax
movq %rsi, %r8
cmovaq %rdx, %r8
movq %rax, %r9
negq %r9
cmpl %esi, %edx
cmovaq %rax, %r9
cvtsi2sd %r8, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq 0x60(%rsp), %rdx
cmpq %rdx, %rax
cmovleq %rdx, %rax
cmpq %rax, %r9
jg 0x1db757
movq 0x30(%rsp), %rax
movslq (%rax,%rdi,4), %rax
subq %rax, %r11
movslq (%r12,%rdi,4), %rax
movq (%rsp), %rdx
subq %rax, %rdx
movl 0x40(%rsp), %eax
cmpq %rax, %r11
cmovleq %rax, %r11
movl 0x3c(%rsp), %eax
cmpq %rax, %rdx
cmovleq %rax, %rdx
cmpq %rdx, %r11
cmovlq %r11, %rdx
cmpq %rcx, %rdx
cmovgq %rdx, %rcx
movq 0x8(%rsp), %r9
cmovgq %rdi, %r9
jmp 0x1db75c
movq 0x50(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %r9
movq 0xa0(%rsp), %r8
leaq (%r10,%r8), %rax
testq %r9, %r9
cmovsl 0x98(%rsp), %ecx
movl %ecx, (%rbx,%rbp,4)
movq %r9, (%r15,%rbp,8)
movl 0x4(%rax), %ecx
movq 0x30(%rsp), %rdx
movl %ecx, (%rdx,%rbp,4)
movl 0x10(%rax), %ecx
movl %ecx, (%r12,%rbp,4)
js 0x1db7b4
movq 0x30(%rsp), %rsi
movl (%rsi,%r9,4), %edx
cmpl %edx, (%rsi,%rbp,4)
jle 0x1db7a8
movq 0x30(%rsp), %rcx
movl %edx, (%rcx,%rbp,4)
movl (%r12,%rbp,4), %ecx
movl (%r12,%r9,4), %edx
cmpl %edx, %ecx
jle 0x1db7b4
movl %edx, (%r12,%rbp,4)
testq %rdi, %rdi
js 0x1db7e1
movl 0x8(%rax), %ecx
leaq (%rdi,%rdi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rdi, %rdx
movl 0x8(%r10,%rdx), %edx
addq 0xf0(%rsp), %rdx
cmpq %rcx, %rdx
jl 0x1db7e4
movl (%rbx,%rdi,4), %ecx
cmpl (%rbx,%rbp,4), %ecx
jge 0x1db7e4
movq %rbp, %rdi
movl %ebp, %ecx
subl %r9d, %ecx
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %ecx
testq %r9, %r9
cmovsl %edx, %ecx
movl 0x18(%rax), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
orl %ecx, %edx
movl %edx, 0x18(%rax)
movups (%rax), %xmm0
movups %xmm0, (%r14,%r8)
movupd 0xc(%rax), %xmm0
movupd %xmm0, 0xc(%r14,%r8)
incq %rbp
addq $0x1c, 0x78(%rsp)
addq $0x1c, 0x70(%rsp)
cmpq %r13, %rbp
jne 0x1db34c
xorl %eax, %eax
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %r15
movq 0x18(%rsp), %r12
movl (%rbx,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%rdi,%rax,8)
movl $0x0, (%r12,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x1db842
leaq (%rdi,%r13,8), %rsi
callq 0x19e008
movq 0x10(%rsp), %rsi
xorl %ebp, %ebp
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movq 0x28(%rsp), %r9
movl -0x8(%rsi,%r13,8), %edi
cmpl $0x0, (%r12,%rdi,4)
jne 0x1db98a
leaq (%rdi,%rdi,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rdi, %rsi
leaq (,%rbp,8), %rdx
addq %rbp, %rdx
leaq (%rdx,%rdx,2), %r8
addq %rbp, %r8
leaq (%r9,%r8), %rdx
movupd (%r14,%rsi), %xmm0
movupd 0xc(%r14,%rsi), %xmm1
movupd %xmm1, 0xc(%r9,%r8)
movupd %xmm0, (%r9,%r8)
movl (%rbx,%rdi,4), %esi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
movl 0x4(%r14,%r8), %r9d
cmpl 0x4(%rdx), %r9d
jae 0x1db8e7
movl %r9d, 0x4(%rdx)
addq %r14, %r8
movl 0x10(%r8), %r9d
cmpl 0x10(%rdx), %r9d
jae 0x1db8f8
movl %r9d, 0x10(%rdx)
movl 0x8(%r8), %r9d
cmpl 0x8(%rdx), %r9d
jbe 0x1db906
movl %r9d, 0x8(%rdx)
movl 0x14(%r8), %r8d
cmpl 0x14(%rdx), %r8d
jbe 0x1db914
movl %r8d, 0x14(%rdx)
movl $0x1, (%r12,%rdi,4)
movq (%r15,%rdi,8), %rdi
testq %rdi, %rdi
jns 0x1db8cd
movq 0x100(%rsp), %rdi
movq 0x20(%rdi), %rdi
movl 0xc(%rdx), %r8d
movl 0x10(%rdx), %r9d
movl (%rdi,%r8,8), %edi
andl %eax, %edi
movl 0x14(%rdx), %r8d
movl 0x18(%rdx), %r10d
movl %edi, %r11d
subl %r8d, %r11d
subl %r9d, %edi
testl %r10d, %r10d
cmovnsl %r9d, %r11d
cmovnsl %r8d, %edi
movl %r11d, 0x10(%rdx)
movl %edi, 0x14(%rdx)
cmpl %ecx, %esi
cmovgel %ecx, %esi
andl %ecx, %esi
andl $0xc0000000, %r10d      # imm = 0xC0000000
orl %esi, %r10d
movl %r10d, 0x18(%rdx)
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %r9
movl $0x0, (%rdx)
incq %rbp
leaq -0x1(%r13), %rdx
cmpq $0x1, %r13
movq %rdx, %r13
jg 0x1db87f
jmp 0x1db9c6
xorl %ebp, %ebp
jmp 0x1db9c6
leaq (,%r13,8), %rdx
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
leaq (%rbx,%r13,8), %rsi
movq %rbx, %rdi
callq 0x19e008
movq %rbp, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq