// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %r8, 0x70(%rsp)
testq %rsi, %rsi
je 0x1d195b
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %rax
movsd %xmm0, 0xa0(%rsp)
movq 0x108(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
movq %rax, 0x18(%rsp)
callq 0x3f828
movq 0x28(%rbp), %r13
movq (%rbp), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0x38(%rsp)
movq 0x18(%rbp), %rax
movq %rax, 0x60(%rsp)
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
movq 0x18(%rsp), %rax
addq %rbx, %rax
addq %rax, %rsi
movq 0x18(%rsp), %rdi
callq 0x195511
movq 0x18(%rsp), %rdx
movq %rbx, 0x10(%rsp)
testq %rbx, %rbx
jle 0x1d1223
movq 0x10(%rsp), %r14
negq %r14
leaq 0x1c(%rdx), %rbp
movl $0x1, %r15d
xorl %edi, %edi
movq %r14, %rax
addq %r15, %rax
je 0x1d11df
movl 0x8(%rbp), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%rdx,%rcx), %eax
jne 0x1d11df
movq %rdi, %rax
jmp 0x1d120c
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1d120c
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdx, %rdi
addq %rax, %rdi
movq %rbp, %rsi
callq 0x1957bd
movq 0x18(%rsp), %rdx
movq %r15, %rax
leaq (%r14,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %rbp
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1d11be
movq 0x10(%rsp), %r14
leaq (,%r14,8), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x18(%rsp), %r10
testq %r14, %r14
movq 0x8(%rsp), %rbp
movsd 0xa0(%rsp), %xmm1
jle 0x1d17e5
leaq -0x8(%r10), %rax
movq %rax, 0x90(%rsp)
leaq -0x14(%r10), %rax
movq %rax, 0x88(%rsp)
movq $-0x1, %r11
xorl %r8d, %r8d
movq %r12, 0x80(%rsp)
leaq (%r8,%r8,8), %rax
leaq (%rax,%rax,2), %rax
addq %r8, %rax
movl 0x4(%r10,%rax), %edi
movl 0x8(%r10,%rax), %ecx
movl %ecx, %esi
subl %edi, %esi
movl 0x14(%r10,%rax), %edx
movq %rax, 0x78(%rsp)
movl 0x10(%r10,%rax), %r9d
movq %rdx, 0x68(%rsp)
movl %edx, %eax
movl %r9d, 0x5c(%rsp)
subl %r9d, %eax
cmpl %eax, %esi
movl %eax, 0x50(%rsp)
movl %esi, 0x54(%rsp)
cmovbl %esi, %eax
movq %rax, 0x28(%rsp)
movq %r8, %rax
subq 0xf8(%rsp), %rax
movl $0x0, %esi
cmovgeq %rax, %rsi
leaq -0x1(%r8), %r12
movq $-0x1, 0x48(%rsp)
movq $-0x1, %r15
movq %r12, %rdx
movq %r8, 0x30(%rsp)
cmpq %rsi, %r8
movq %rcx, 0x20(%rsp)
jle 0x1d14fc
movl 0x54(%rsp), %eax
movq %rax, 0xb0(%rsp)
movl 0x50(%rsp), %eax
movq %rax, 0xa8(%rsp)
movq $-0x1, %r9
movq 0x90(%rsp), %r14
xorl %r8d, %r8d
movq %r12, %rax
movq $-0x1, 0x48(%rsp)
movl %edi, 0x58(%rsp)
movl -0xc(%r14), %ebx
cmpl %edi, %ebx
jbe 0x1d14f6
movl (%r14), %edx
movq $-0x80000000, %r15      # imm = 0x80000000
cmpl %edx, 0x68(%rsp)
jb 0x1d1479
movq %r13, %rbp
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
movl -0x10(%r14), %ecx
cmpl %ecx, %edi
movl %ecx, %r13d
cmoval %edi, %r13d
cmpl %r13d, %ebx
jbe 0x1d1458
movq %r8, 0x98(%rsp)
movq %r10, %r8
movq 0x68(%rsp), %r10
cmpl %edx, %r10d
cmovbl %r10d, %edx
movl -0x4(%r14), %ebx
movl 0x5c(%rsp), %edi
cmpl %ebx, %edi
movl %ebx, %r13d
cmoval %edi, %r13d
cmpl %r13d, %edx
jbe 0x1d1462
movq 0x20(%rsp), %r13
subl %ecx, %r13d
movl %r10d, %ecx
subl %ebx, %ecx
movq %r13, %rbx
subq %rcx, %rbx
movq %rcx, %r10
cmovaq %r13, %r10
movq %rbx, %rdx
negq %rdx
cmpl %ecx, %r13d
cmovaq %rbx, %rdx
cvtsi2sd %r10, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
cmpq 0x70(%rsp), %rbx
jg 0x1d13f1
movq 0x70(%rsp), %rbx
cmpq %rbx, %rdx
movq 0x18(%rsp), %r10
movq %rbp, %r13
movq 0x8(%rsp), %rbp
movl 0x58(%rsp), %edi
movq 0x98(%rsp), %r8
jg 0x1d1479
movq 0x38(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movq 0x20(%rsp), %rdx
subq %rcx, %rdx
movq 0x60(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movq 0x68(%rsp), %r15
subq %rcx, %r15
movq 0xb0(%rsp), %rcx
cmpq %rcx, %rdx
cmovleq %rcx, %rdx
movq 0xa8(%rsp), %rcx
cmpq %rcx, %r15
cmovleq %rcx, %r15
cmpq %r15, %rdx
cmovlq %rdx, %r15
jmp 0x1d1479
movq %rbp, %r13
movq 0x8(%rsp), %rbp
jmp 0x1d1479
movq %rbp, %r13
movq 0x8(%rsp), %rbp
movq %r8, %r10
movl 0x58(%rsp), %edi
movq 0x98(%rsp), %r8
cmpq $-0x80000000, %r15      # imm = 0x80000000
jne 0x1d148c
movq %r9, %r15
movq 0x20(%rsp), %rcx
jmp 0x1d14dd
cmpq %r9, %r15
jle 0x1d14a8
xorl %ecx, %ecx
testq %r8, %r8
setg %cl
subq %rcx, %r8
movq %rax, 0x48(%rsp)
movq 0x20(%rsp), %rcx
jmp 0x1d14c9
movq 0x30(%rsp), %rcx
cmpq %rcx, (%r13,%rax,8)
movq 0x20(%rsp), %rcx
jne 0x1d14c6
cmpq 0xf0(%rsp), %r8
jge 0x1d14f6
incq %r8
movq %r9, %r15
movq (%rbp,%rax,8), %rdx
testq %rdx, %rdx
js 0x1d14dd
movq 0x30(%rsp), %r9
movq %r9, (%r13,%rdx,8)
leaq -0x1(%rax), %rdx
addq $-0x1c, %r14
movq %r15, %r9
cmpq %rsi, %rax
movq %rdx, %rax
jg 0x1d1341
jmp 0x1d14fc
movq %r9, %r15
movq %rax, %rdx
testq %r11, %r11
movq 0x40(%rsp), %rbx
movq 0x100(%rsp), %r14
js 0x1d1528
leaq (%r11,%r11,8), %rax
leaq (%rax,%rax,2), %rax
addq %r11, %rax
movl 0x8(%r10,%rax), %eax
addq %r14, %rax
cmpq 0x20(%rsp), %rax
jge 0x1d1572
movq $-0x1, %r11
cmpq %rsi, 0x30(%rsp)
jle 0x1d1572
movq $-0x80000000, %rax      # imm = 0x80000000
movq 0x88(%rsp), %r9
movl (%r9), %ecx
addq %r14, %rcx
cmpq 0x20(%rsp), %rcx
jl 0x1d1572
movslq (%rbx,%r12,4), %rcx
cmpq %rcx, %rax
cmovleq %rcx, %rax
cmovlq %r12, %r11
leaq -0x1(%r12), %rcx
addq $-0x1c, %r9
cmpq %rsi, %r12
movq %rcx, %r12
jg 0x1d1545
testq %r11, %r11
sets %al
cmpq %rdx, %r11
setge %cl
orb %al, %cl
leaq (%r11,%r11,8), %rax
jne 0x1d1693
leaq (%rax,%rax,2), %rsi
addq %r11, %rsi
movl 0x8(%r10,%rsi), %ebp
cmpl %edi, %ebp
movq 0x80(%rsp), %r12
jbe 0x1d16a2
addq %r10, %rsi
movl 0x14(%rsi), %r9d
movq $-0x80000000, %rdx      # imm = 0x80000000
movq 0x68(%rsp), %rcx
cmpl %r9d, %ecx
jb 0x1d16b8
movq 0x20(%rsp), %r8
cmpl %ebp, %r8d
cmovbl %r8d, %ebp
movl 0x4(%rsi), %r14d
cmpl %r14d, %edi
cmovbel %r14d, %edi
cmpl %edi, %ebp
jbe 0x1d16b8
cmpl %r9d, %ecx
cmovbl %ecx, %r9d
movl 0x10(%rsi), %esi
movl 0x5c(%rsp), %edi
cmpl %esi, %edi
cmovbel %esi, %edi
cmpl %edi, %r9d
movq 0x8(%rsp), %rbp
jbe 0x1d17db
movq %r8, %rbx
movq %rcx, %r8
movl %ebx, %ecx
subl %r14d, %ecx
movl %r8d, %edi
subl %esi, %edi
movq %rcx, %rsi
subq %rdi, %rsi
movq %rdi, %r9
cmovaq %rcx, %r9
movq %rsi, %r10
negq %r10
cmpl %edi, %ecx
cmovaq %rsi, %r10
cvtsi2sd %r9, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq 0x70(%rsp), %rsi
cmpq %rsi, %rcx
cmovleq %rsi, %rcx
cmpq %rcx, %r10
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %rdi
jg 0x1d16c7
movq 0x38(%rsp), %rcx
movslq (%rcx,%r11,4), %rcx
subq %rcx, %rbx
movq 0x60(%rsp), %rcx
movslq (%rcx,%r11,4), %rcx
subq %rcx, %r8
movl 0x54(%rsp), %ecx
cmpq %rcx, %rbx
cmovleq %rcx, %rbx
movl 0x50(%rsp), %ecx
cmpq %rcx, %r8
cmovleq %rcx, %r8
cmpq %r8, %rbx
cmovlq %rbx, %r8
movq %r8, %rdx
jmp 0x1d16c7
movq 0x80(%rsp), %r12
movq 0x10(%rsp), %r14
jmp 0x1d16ac
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbp
movq 0x78(%rsp), %r9
movq 0x28(%rsp), %rdi
jmp 0x1d16f2
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbp
movq 0x28(%rsp), %rdi
cmpq $-0x80000000, %rdx      # imm = 0x80000000
setne %cl
cmpq %r15, %rdx
setg %sil
testb %sil, %cl
movq 0x48(%rsp), %rcx
cmovneq %r11, %rcx
movq %rcx, 0x48(%rsp)
cmovneq %rdx, %r15
movq 0x78(%rsp), %r9
movq 0x30(%rsp), %r8
leaq (%r10,%r9), %rcx
movq 0x48(%rsp), %rbx
testq %rbx, %rbx
cmovsl %edi, %r15d
movq 0x40(%rsp), %rdx
movl %r15d, (%rdx,%r8,4)
movq %rbx, (%rbp,%r8,8)
movl 0x4(%rcx), %edx
movq 0x38(%rsp), %rsi
movl %edx, (%rsi,%r8,4)
movl 0x10(%rcx), %edx
movq 0x60(%rsp), %rsi
movl %edx, (%rsi,%r8,4)
js 0x1d1753
movq 0x38(%rsp), %rdi
movl (%rdi,%rbx,4), %edx
cmpl %edx, (%rdi,%r8,4)
jle 0x1d1746
movq 0x38(%rsp), %rdi
movl %edx, (%rdi,%r8,4)
movl (%rsi,%rbx,4), %edx
cmpl %edx, (%rsi,%r8,4)
jle 0x1d1753
movl %edx, (%rsi,%r8,4)
testq %r11, %r11
js 0x1d1783
movl 0x8(%rcx), %edx
leaq (%rax,%rax,2), %rax
addq %r11, %rax
movl 0x8(%r10,%rax), %eax
addq 0x100(%rsp), %rax
cmpq %rdx, %rax
jl 0x1d1786
movq 0x40(%rsp), %rdx
movl (%rdx,%r11,4), %eax
cmpl (%rdx,%r8,4), %eax
jge 0x1d1786
movq %r8, %r11
movl %r8d, %eax
subl %ebx, %eax
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %eax
testq %rbx, %rbx
cmovsl %edx, %eax
movl 0x18(%rcx), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
orl %eax, %edx
movl %edx, 0x18(%rcx)
movups (%rcx), %xmm0
movups %xmm0, (%r12,%r9)
movupd 0xc(%rcx), %xmm0
movupd %xmm0, 0xc(%r12,%r9)
incq %r8
addq $0x1c, 0x90(%rsp)
addq $0x1c, 0x88(%rsp)
cmpq %r14, %r8
jne 0x1d1280
jmp 0x1d17e5
movq 0x10(%rsp), %r14
jmp 0x1d16c2
testq %r14, %r14
movq 0x40(%rsp), %rdx
movq 0x60(%rsp), %r15
jle 0x1d1815
xorl %eax, %eax
movl (%rdx,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%r13,%rax,8)
movl $0x0, (%r15,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x1d17f6
leaq (,%r14,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x19525e
testq %r14, %r14
jle 0x1d195b
movq 0x110(%rsp), %rcx
xorl %eax, %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl -0x8(%r13,%r14,8), %r9d
cmpl $0x0, (%r15,%r9,4)
jne 0x1d1948
leaq (%r9,%r9,8), %rdi
leaq (%rdi,%rdi,2), %r8
addq %r9, %r8
leaq (%rax,%rax,8), %rdi
leaq (%rdi,%rdi,2), %r10
addq %rax, %r10
movq 0x18(%rsp), %r11
leaq (%r11,%r10), %rdi
movupd (%r12,%r8), %xmm0
movupd 0xc(%r12,%r8), %xmm1
movupd %xmm1, 0xc(%r11,%r10)
movupd %xmm0, (%r11,%r10)
movq 0x40(%rsp), %r8
movl (%r8,%r9,4), %r8d
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r10
addq %r9, %r10
movl 0x4(%r12,%r10), %r11d
cmpl 0x4(%rdi), %r11d
jae 0x1d18b1
movl %r11d, 0x4(%rdi)
addq %r12, %r10
movl 0x10(%r10), %r11d
cmpl 0x10(%rdi), %r11d
jae 0x1d18c2
movl %r11d, 0x10(%rdi)
movl 0x8(%r10), %r11d
cmpl 0x8(%rdi), %r11d
jbe 0x1d18d0
movl %r11d, 0x8(%rdi)
movl 0x14(%r10), %r10d
cmpl 0x14(%rdi), %r10d
jbe 0x1d18de
movl %r10d, 0x14(%rdi)
movl $0x1, (%r15,%r9,4)
movq (%rbp,%r9,8), %r9
testq %r9, %r9
jns 0x1d1897
movq 0x20(%rcx), %r9
movl 0xc(%rdi), %r10d
movl 0x10(%rdi), %r11d
movl (%r9,%r10,8), %r9d
andl %edx, %r9d
movl 0x14(%rdi), %r10d
movl 0x18(%rdi), %ebx
movl %r9d, %ebp
subl %r10d, %ebp
subl %r11d, %r9d
testl %ebx, %ebx
cmovnsl %r11d, %ebp
cmovnsl %r10d, %r9d
movl %ebp, 0x10(%rdi)
movq 0x8(%rsp), %rbp
movl %r9d, 0x14(%rdi)
cmpl %esi, %r8d
cmovgel %esi, %r8d
andl %esi, %r8d
andl $0xc0000000, %ebx       # imm = 0xC0000000
orl %r8d, %ebx
movl %ebx, 0x18(%rdi)
movl $0x0, (%rdi)
incq %rax
leaq -0x1(%r14), %rdi
cmpq $0x1, %r14
movq %rdi, %r14
jg 0x1d1845
jmp 0x1d195d
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq