// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movsd %xmm0, 0x78(%rsp)
movq %r8, 0x70(%rsp)
testq %rsi, %rsi
je 0x18e2d1
movq %rdx, %r13
movq %rdi, %rbp
movq 0xe8(%rsp), %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq %rsi, 0x10(%rsp)
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%r14), %rax
movq %rax, 0x18(%rsp)
movq (%r14), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%r14), %rax
movq %rax, 0x50(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
imulq $0x1c, 0x10(%rsp), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156f5c
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
movl $0x0, %r15d
cmovgq %rdx, %r15
movq %r15, 0x40(%rsp)
negq %r15
leaq 0x1c(%rbp), %r14
pushq $0x1
popq %r12
leaq (%r15,%r12), %rax
cmpq $0x1, %rax
je 0x18ddf6
cmpq %r12, %rdx
je 0x18ddc4
movl 0x8(%r14), %eax
imulq $0x1c, %rbx, %rcx
cmpl 0x8(%rbp,%rcx), %eax
jne 0x18ddc4
movq %rbx, %rax
jmp 0x18ddea
movq %r12, %rcx
subq %rbx, %rcx
movq %r12, %rax
cmpq $0x2, %rcx
jl 0x18ddea
imulq $0x1c, %rbx, %rdi
addq %rbp, %rdi
movq %r14, %rsi
callq 0x1571fa
movq 0x10(%rsp), %rdx
movq %r12, %rax
incq %r12
addq $0x1c, %r14
movq %rax, %rbx
jmp 0x18dda2
leaq (,%rdx,8), %rdx
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
leaq -0x1c(%rbp), %r12
leaq -0x14(%rbp), %r8
pushq $-0x1
popq %r9
xorl %ecx, %ecx
movq 0x58(%rsp), %r15
movq 0x40(%rsp), %rbx
movq %r13, 0x48(%rsp)
movq %rbp, 0x80(%rsp)
cmpq %rbx, %rcx
je 0x18e17a
movq %r8, 0x68(%rsp)
imulq $0x1c, %rcx, %rdx
leaq (%rdx,%rbp), %r14
movl 0x8(%rbp,%rdx), %eax
subl 0x4(%rbp,%rdx), %eax
movl 0x14(%rbp,%rdx), %esi
movq %rdx, 0x90(%rsp)
subl 0x10(%rbp,%rdx), %esi
cmpl %esi, %eax
cmovbl %eax, %esi
movq %rsi, 0x88(%rsp)
movq %rcx, %r13
subq 0xd8(%rsp), %r13
movl $0x0, %eax
cmovlq %rax, %r13
movq %rcx, (%rsp)
leaq -0x1(%rcx), %rbx
movq %r12, 0x60(%rsp)
movq %r12, %rbp
pushq $-0x1
popq %rax
movq %rax, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %rbx, %r12
movq $0x0, 0x38(%rsp)
cmpq %r13, %r12
jl 0x18df68
movl 0x8(%rbp), %eax
cmpl 0x4(%r14), %eax
jbe 0x18df68
movq %r9, %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %r8
movsd 0x78(%rsp), %xmm0
movq 0x70(%rsp), %r9
callq 0x1a36f8
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x18def0
movq %r15, %r9
jmp 0x18df5c
cmpq 0x20(%rsp), %rax
jle 0x18df1b
xorl %ecx, %ecx
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
setg %cl
subq %rcx, %rdx
movq %rdx, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %r15, %r9
jmp 0x18df41
movq 0x18(%rsp), %rax
movq (%rsp), %rcx
cmpq %rcx, (%rax,%r12,8)
movq %r15, %r9
jne 0x18df41
movq 0x38(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jge 0x18df68
incq 0x38(%rsp)
movq 0x50(%rsp), %rax
movq (%rax,%r12,8), %rax
testq %rax, %rax
js 0x18df5c
movq 0x18(%rsp), %rcx
movq (%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
decq %r12
addq $-0x1c, %rbp
jmp 0x18dea7
testq %r9, %r9
movq 0x80(%rsp), %rbp
movq 0x58(%rsp), %r15
movq 0xe0(%rsp), %rdi
movq 0x68(%rsp), %r8
js 0x18df9b
movl 0x8(%r14), %eax
imulq $0x1c, %r9, %rcx
movl 0x8(%rbp,%rcx), %ecx
addq %rdi, %rcx
cmpq %rax, %rcx
jge 0x18dfd4
movq $-0x80000000, %rax      # imm = 0x80000000
movq %r8, %rcx
pushq $-0x1
popq %r9
cmpq %r13, %rbx
jl 0x18dfd4
movl 0x8(%r14), %edx
movl (%rcx), %esi
addq %rdi, %rsi
cmpq %rdx, %rsi
jl 0x18dfd4
movslq (%r15,%rbx,4), %rdx
cmpq %rdx, %rax
cmovleq %rdx, %rax
cmovlq %rbx, %r9
decq %rbx
addq $-0x1c, %rcx
jmp 0x18dfa9
testq %r9, %r9
sets %al
cmpq %r12, %r9
setge %cl
orb %al, %cl
jne 0x18e06a
imulq $0x1c, %r9, %rsi
movl 0x8(%rbp,%rsi), %eax
cmpl 0x4(%r14), %eax
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbx
movq 0x60(%rsp), %r12
jbe 0x18e079
addq %rbp, %rsi
movq %r14, %rdi
movq %r9, %rdx
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %r8
movsd 0x78(%rsp), %xmm0
movq %r9, %r13
movq 0x70(%rsp), %r9
callq 0x1a36f8
movq %r13, %r9
movq 0x48(%rsp), %r13
movq 0x68(%rsp), %r8
movq 0xe0(%rsp), %rdi
cmpq $-0x80000000, %rax      # imm = 0x80000000
setne %cl
movq 0x20(%rsp), %rsi
cmpq %rsi, %rax
setg %dl
testb %dl, %cl
movq 0x30(%rsp), %r10
cmovneq %r9, %r10
cmovneq %rax, %rsi
movq %rsi, 0x20(%rsp)
jmp 0x18e07e
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbx
movq 0x60(%rsp), %r12
movq 0x30(%rsp), %r10
testq %r10, %r10
movq 0x20(%rsp), %rax
cmovsl 0x88(%rsp), %eax
movq (%rsp), %rdx
movl %eax, (%r15,%rdx,4)
movq 0x50(%rsp), %rax
movq %r10, (%rax,%rdx,8)
movl 0x4(%r14), %eax
movq 0x28(%rsp), %rcx
movl %eax, (%rcx,%rdx,4)
movl 0x10(%r14), %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%rdx,4)
js 0x18e0f8
movq 0x28(%rsp), %rdx
movl (%rdx,%r10,4), %ecx
movq (%rsp), %rsi
cmpl %ecx, (%rdx,%rsi,4)
jle 0x18e0df
movq 0x28(%rsp), %rax
movq (%rsp), %rdx
movl %ecx, (%rax,%rdx,4)
movq 0x8(%rsp), %rax
movl (%rax,%rdx,4), %eax
movq 0x8(%rsp), %rcx
movl (%rcx,%r10,4), %ecx
cmpl %ecx, %eax
jle 0x18e0f8
movq 0x8(%rsp), %rax
movq (%rsp), %rdx
movl %ecx, (%rax,%rdx,4)
testq %r9, %r9
js 0x18e11f
movl 0x8(%r14), %eax
imulq $0x1c, %r9, %rcx
movl 0x8(%rbp,%rcx), %ecx
addq %rdi, %rcx
cmpq %rax, %rcx
jl 0x18e123
movl (%r15,%r9,4), %eax
movq (%rsp), %rcx
cmpl (%r15,%rcx,4), %eax
jge 0x18e123
movq (%rsp), %r9
movq (%rsp), %rsi
movl %esi, %eax
subl %r10d, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
testq %r10, %r10
cmovsl %ecx, %eax
movl 0x18(%r14), %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x18(%r14)
movups (%r14), %xmm0
movq 0x90(%rsp), %rax
movups %xmm0, (%r13,%rax)
movups 0xc(%r14), %xmm0
movq %rsi, %rcx
movups %xmm0, 0xc(%r13,%rax)
incq %rcx
addq $0x1c, %r12
addq $0x1c, %r8
jmp 0x18de2f
xorl %eax, %eax
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rsi
cmpq %rax, %rbx
je 0x18e1a8
movl (%r15,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%rsi,%rax,8)
movq 0x8(%rsp), %rcx
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0x18e186
movq 0x18(%rsp), %rdi
leaq (%rdi,%rdx,8), %rsi
callq 0x156cf6
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
jle 0x18e2d3
movq 0x18(%rsp), %rdi
movl -0x8(%rdi,%rsi,8), %r8d
decq %rsi
movq 0x8(%rsp), %rdi
cmpl $0x0, (%rdi,%r8,4)
jne 0x18e1c7
movq %rsi, 0x10(%rsp)
imulq $0x1c, %r8, %rdi
imulq $0x1c, %rax, %r9
leaq (%r9,%rbp), %rsi
movups (%r13,%rdi), %xmm0
movups 0xc(%r13,%rdi), %xmm1
movups %xmm1, 0xc(%rbp,%r9)
movups %xmm0, (%rbp,%r9)
movl (%r15,%r8,4), %edi
testq %r8, %r8
js 0x18e273
imulq $0x1c, %r8, %r9
movl 0x4(%r13,%r9), %r10d
cmpl 0x4(%rsi), %r10d
jae 0x18e22e
movl %r10d, 0x4(%rsi)
addq %r13, %r9
movl 0x10(%r9), %r10d
cmpl 0x10(%rsi), %r10d
jae 0x18e23f
movl %r10d, 0x10(%rsi)
movl 0x8(%r9), %r10d
cmpl 0x8(%rsi), %r10d
jbe 0x18e24d
movl %r10d, 0x8(%rsi)
movl 0x14(%r9), %r9d
cmpl 0x14(%rsi), %r9d
jbe 0x18e25b
movl %r9d, 0x14(%rsi)
movq 0x8(%rsp), %r9
movl $0x1, (%r9,%r8,4)
movq 0x50(%rsp), %r9
movq (%r9,%r8,8), %r8
jmp 0x18e216
movq 0xf0(%rsp), %r8
movq 0x20(%r8), %r8
movl 0xc(%rsi), %r9d
movl 0x10(%rsi), %r10d
movl (%r8,%r9,8), %r8d
andl %ecx, %r8d
movl 0x14(%rsi), %r9d
movl 0x18(%rsi), %r11d
movl %r8d, %ebx
subl %r9d, %ebx
subl %r10d, %r8d
testl %r11d, %r11d
cmovnsl %r10d, %ebx
cmovnsl %r9d, %r8d
movl %ebx, 0x10(%rsi)
movl %r8d, 0x14(%rsi)
cmpl %edx, %edi
cmovgel %edx, %edi
andl %edx, %edi
andl $0xc0000000, %r11d      # imm = 0xC0000000
orl %edi, %r11d
movl %r11d, 0x18(%rsi)
andl $0x0, (%rsi)
incq %rax
jmp 0x18e1c2
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq