// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: gen_cns_chain_linear_hard(ul_ov_t*, long, asg_t const*, long, long, double, Chain_Data*, long, long, long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
uint64_t gen_cns_chain_linear_hard(ul_ov_t *a, int64_t a_n, const asg_t *rg, int64_t qlen, int64_t bw, double diff_thre, Chain_Data* dp, 
int64_t max_skip, int64_t max_iter, int64_t max_dis, uint64_t mqs, uint64_t mqe, uint64_t ulid, uint64_t ltn, uint64_t rtn)
{
    if(a_n == 0) return 0;
    uint32_t li_v, lj_v; int32_t *f; int64_t *p, *t, st, max_ii, max, qo, cL; ul_ov_t *li, *lj; 
    int64_t mm_ovlp, x, i, j, sc, csc, mm_sc, mm_idx, n_skip, end_j, ch_sc, ch_i; 
    resize_Chain_Data(dp, a_n, NULL); t = dp->tmp; f = dp->score; p = dp->pre;
        
    radix_sort_ul_ov_srt_qe(a, a + a_n);
    for (i = 1, j = 0; i <= a_n; i++) {
        if (i == a_n || a[i].qe != a[j].qe) {
            if(i - j > 1) radix_sort_ul_ov_srt_qs(a+j, a+i);
            j = i;
        }
    }

    memset(t, 0, (a_n*sizeof((*t)))); 
    ch_sc = ch_i = INT32_MIN; 
    for (i = st = 0, max_ii = -1; i < a_n; ++i) {
        li = &(a[i]); 
        mm_ovlp = max_ovlp(rg, ((li->tn<<1)|li->rev)^1);        
        x = (li->qs + mm_ovlp)*diff_thre; 
        if(x < bw) x = bw; 
        x += li->qs + mm_ovlp;
        if (x > qlen+1) x = qlen+1;
        x = find_ul_ov_max(i, a, x+G_CHAIN_INDEL); 

        csc = 1; if(li->el) csc = 0;
        mm_sc = INT32_MIN+1; mm_idx = -1; n_skip = 0; end_j = -1; 

        li_v = (li->tn<<1)|li->rev; li_v ^= 1;
        if ((x-st) > max_iter) st = x-max_iter;
        for (j = x; j >= st; --j) { // collect potential destination vertices
            lj = &(a[j]); 
            if(lj->qe+G_CHAIN_INDEL <= li->qs) break;//even this pair has a overlap, its length will be very small; just ignore
            if(f[j] == INT32_MIN) continue;///could not reach the left end
            sc = INT32_MIN;
            if(lj->qs <= li->qs && lj->qe <= li->qe) {///not contained
                lj_v = (lj->tn<<1)|lj->rev; lj_v ^= 1;
                qo = infer_rovlp(li, lj, NULL, NULL, NULL, NULL);
                if(gconnect_test(rg, li_v, lj_v, bw, diff_thre, qo)) {
                    sc = f[j] + csc; 
                }
            }
            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 || (a[i].qe > (a[max_ii].qe+max_dis))) {//too long
            max = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && (a[i].qe<=(max_dis+a[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 = &(a[max_ii]); 
            if(((lj->qe+G_CHAIN_INDEL)>li->qs) && (lj->qs<=li->qs) && (lj->qe<=li->qe) && (f[max_ii]!=INT32_MIN)) {
                lj_v = (lj->tn<<1)|lj->rev; lj_v ^= 1; sc = INT32_MIN;
                qo = infer_rovlp(li, lj, NULL, NULL, NULL, NULL);
                if(gconnect_test(rg, li_v, lj_v, bw, diff_thre, qo)) {
                    sc = f[max_ii] + csc; 
                }
                if(sc != INT32_MIN) {
                    if(sc > mm_sc) {
                        mm_sc = sc, mm_idx = max_ii;
                    }
                }
            }
        }

		if(mm_idx == -1) {
			if((ltn) != ((uint64_t)-1)) {
				if(ltn != li->tn) mm_sc = mm_idx = INT32_MIN;
			} else {
				if(((int64_t)li->qs) > ((int64_t)(mqs+bw))) mm_sc = mm_idx = INT32_MIN;
			}
		}
        
        if((mm_sc==(INT32_MIN+1))) {
            mm_sc = csc; 
        }

        
        f[i] = mm_sc; p[i] = mm_idx; 

		// if(ulid == 7723 && ltn == (7723>>1) && rtn == (7829>>1)) {
		// 	fprintf(stderr, "[M::%s::id->%u::%.*s] i::%ld, q::[%u, %u), t::[%u, %u), mm_idx::%ld, mm_sc::%ld\n", 
		// 	__func__, li->tn, (int)Get_NAME_LENGTH(R_INF, li->tn), Get_NAME(R_INF, li->tn), i, 
		// 	li->qs, li->qe, li->ts, li->te, mm_idx, mm_sc);
        // }

		if(mm_sc == INT32_MIN) continue;
		if((rtn != ((uint64_t)-1)) && (rtn != li->tn)) continue;
		if((rtn == ((uint64_t)-1)) && (((int64_t)(a[i].qe+bw))<((int64_t)(mqe)))) continue;
        if(mm_sc >= ch_sc) {
			ch_sc = mm_sc; ch_i = i;
		}
		if ((max_ii < 0) || (f[max_ii]<f[i])) {
            max_ii = i;
        }
    }
    // if(ulid == 442462) {
    //  fprintf(stderr, "[M::%s::] ch_i::%ld, mqs::%lu, mqe::%lu\n", 
    //  __func__, ch_i, mqs, mqe);
    // }
    if(ch_i == INT32_MIN) return 0;

    i = ch_i; cL = 0;
    while (i >= 0) {t[cL++] = i; i = p[i];}
    for (i = 0; i < cL; i++) a[i] = a[t[cL-i-1]];
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movsd %xmm0, 0x60(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x78(%rsp)
testq %rsi, %rsi
je 0x1d434e
movq %r9, %r14
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r13
movq %r9, %rdi
xorl %edx, %edx
callq 0x3f828
movq 0x28(%r14), %rax
movq %rax, 0x70(%rsp)
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rax
movq %rax, 0x18(%rsp)
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x195511
testq %rbp, %rbp
jle 0x1d3d4e
movq %rbp, %rbx
negq %rbx
leaq 0x1c(%r13), %r14
movl $0x1, %r15d
xorl %edi, %edi
movq %rbx, %rax
addq %r15, %rax
je 0x1d3d0f
movl 0x8(%r14), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r13,%rcx), %eax
jne 0x1d3d0f
movq %rdi, %rax
jmp 0x1d3d37
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1d3d37
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rdi
addq %rax, %rdi
movq %r14, %rsi
callq 0x1957bd
movq %r15, %rax
leaq (%rbx,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r14
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1d3cec
leaq (,%rbp,8), %rdx
movq 0x70(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
testq %rbp, %rbp
jle 0x1d4332
movq %rbp, 0x90(%rsp)
incq 0x78(%rsp)
movq 0xf8(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq -0x14(%r13), %r14
movq $-0x1, 0x80(%rsp)
movq $-0x80000000, 0x48(%rsp) # imm = 0x80000000
movq $-0x80000000, 0x68(%rsp) # imm = 0x80000000
xorl %edi, %edi
movq $-0x1, %rbp
movq $0x0, 0x38(%rsp)
movq %r13, 0x98(%rsp)
movq %r12, 0x20(%rsp)
movq 0x38(%rsp), %rbx
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rdi, %rsi
movl 0xc(%r13,%rsi), %eax
movl 0x18(%r13,%rsi), %ecx
shrdl $0x1f, %eax, %ecx
xorl $0x1, %ecx
movq 0x28(%r12), %rax
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0x1d3e54
movq %rsi, %r10
movq 0x8(%r12), %rdx
shrq $0x20, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %esi, %esi
movl %esi, %r9d
movl (%rcx,%rdx), %r8d
cmpl %r8d, %esi
movl %r8d, %esi
cmoval %r9d, %esi
testl %r8d, %r8d
cmovsl %r9d, %esi
addq $0x18, %rdx
cmpq %rdx, %rax
jne 0x1d3e2c
movl %esi, %eax
movq %r10, %rsi
jmp 0x1d3e56
xorl %eax, %eax
movq %r14, 0x58(%rsp)
addq %r13, %rsi
movq %rsi, 0x10(%rsp)
movl 0x4(%rsi), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x30(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x78(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movq %rdi, (%rsp)
movq %r13, %rsi
callq 0x19f679
movq 0x10(%rsp), %rdi
cltq
movl 0xc(%rdi), %esi
movl 0x18(%rdi), %ecx
xorl %edx, %edx
btl $0x1e, %ecx
setae %dl
movq %rdx, 0x8(%rsp)
shldl $0x1, %ecx, %esi
xorl $0x1, %esi
movl %esi, 0x44(%rsp)
movq %rax, %rcx
subq %rbx, %rcx
movq %rax, %rsi
movq 0xe8(%rsp), %rdx
subq %rdx, %rsi
cmpq %rdx, %rcx
cmovleq %rbx, %rsi
movq %rsi, 0x38(%rsp)
cmpq %rax, %rsi
jle 0x1d3f13
movq $-0x1, 0x50(%rsp)
movq $-0x7fffffff, %r12      # imm = 0x80000001
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r10
xorl %r11d, %r11d
jmp 0x1d405c
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r14
addq %r13, %r14
addq %rax, %r14
movq $-0x7fffffff, %r13      # imm = 0x80000001
movq $-0x1, 0x50(%rsp)
xorl %esi, %esi
movq %rax, %rbx
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r10
xorl %r11d, %r11d
movl 0x8(%r14), %eax
leal 0x80(%rax), %edx
movl 0x4(%rdi), %ecx
cmpl %ecx, %edx
jbe 0x1d4056
cmpl (%r10,%rbx,4), %r11d
jo 0x1d3ff4
movq $-0x80000000, %r12      # imm = 0x80000000
cmpl %ecx, 0x4(%r14)
ja 0x1d3feb
cmpl 0x8(%rdi), %eax
ja 0x1d3feb
movl 0xc(%r14), %eax
movl 0x18(%r14), %r15d
shrdl $0x1f, %eax, %r15d
xorl $0x1, %r15d
movq %rsi, 0xa0(%rsp)
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6f050
movq 0x20(%rsp), %rdi
movl 0x44(%rsp), %esi
movl %r15d, %edx
movq 0x70(%rsp), %r15
movq 0x30(%rsp), %rcx
movsd 0x60(%rsp), %xmm0
movq %rax, %r8
callq 0x1d1b71
movq 0xa0(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %r11d, %r11d
movq 0x28(%rsp), %r10
movq 0x18(%rsp), %r9
testq %rax, %rax
je 0x1d3feb
movslq (%r10,%rbx,4), %r12
addq 0x8(%rsp), %r12
cmpq $-0x80000000, %r12      # imm = 0x80000000
jne 0x1d4012
movq %r13, %r12
leaq -0x1(%rbx), %rax
addq $-0x1c, %r14
movq %r12, %r13
cmpq 0x38(%rsp), %rbx
movq %rax, %rbx
jg 0x1d3f43
jmp 0x1d405c
cmpq %r13, %r12
jle 0x1d4029
xorl %eax, %eax
testq %rsi, %rsi
setg %al
subq %rax, %rsi
movq %rbx, 0x50(%rsp)
jmp 0x1d4043
movq (%rsp), %rax
cmpq %rax, (%r15,%rbx,8)
jne 0x1d4040
cmpq 0xe0(%rsp), %rsi
jge 0x1d4056
incq %rsi
movq %r13, %r12
movq (%r9,%rbx,8), %rax
testq %rax, %rax
js 0x1d3ff7
movq (%rsp), %rcx
movq %rcx, (%r15,%rax,8)
jmp 0x1d3ff7
movq %rbx, %rax
movq %r13, %r12
testq %rbp, %rbp
movq 0x98(%rsp), %r13
movq 0xf0(%rsp), %rbx
movq 0x58(%rsp), %r14
js 0x1d409d
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %ecx
leaq (,%rbp,8), %rdx
addq %rbp, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rbp, %rdx
movl 0x8(%r13,%rdx), %edx
addq %rbx, %rdx
cmpq %rcx, %rdx
jge 0x1d40f5
movq $-0x1, %rbp
movq 0x38(%rsp), %rcx
cmpq %rcx, (%rsp)
jle 0x1d40f5
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %ecx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq %r14, %rsi
movq 0x80(%rsp), %rdi
movl (%rsi), %r8d
addq %rbx, %r8
cmpq %rcx, %r8
jl 0x1d40f5
movslq (%r10,%rdi,4), %r8
cmpq %r8, %rdx
cmovlq %rdi, %rbp
cmovleq %r8, %rdx
leaq -0x1(%rdi), %r8
addq $-0x1c, %rsi
cmpq 0x38(%rsp), %rdi
movq %r8, %rdi
jg 0x1d40c9
testq %rbp, %rbp
js 0x1d416c
cmpq %rax, %rbp
movq 0x50(%rsp), %rbx
jge 0x1d4154
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %rsi
addq %rbp, %rsi
movl 0x8(%r13,%rsi), %eax
leal 0x80(%rax), %edx
movq 0x10(%rsp), %r8
movl 0x4(%r8), %ecx
cmpl %ecx, %edx
jbe 0x1d4146
addq %r13, %rsi
cmpl %ecx, 0x4(%rsi)
ja 0x1d4146
cmpl 0x8(%r8), %eax
ja 0x1d4146
cmpl (%r10,%rbp,4), %r11d
jno 0x1d4279
movq 0x110(%rsp), %rsi
movq (%rsp), %rdi
jmp 0x1d4165
movq 0x110(%rsp), %rsi
movq (%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r11
jmp 0x1d4187
movq 0x110(%rsp), %rsi
movq (%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r11
movq 0x50(%rsp), %rbx
cmpq $-0x1, %rbx
jne 0x1d41d1
cmpq $-0x1, %rsi
je 0x1d41ac
movl 0xc(%r8), %eax
xorl %ebx, %ebx
cmpq %rsi, %rax
sete %bl
movq $-0x80000000, %rax      # imm = 0x80000000
cmovneq %rax, %r12
jmp 0x1d41c8
movl 0x4(%r8), %eax
xorl %ebx, %ebx
cmpq %rax, 0x88(%rsp)
setge %bl
movq $-0x80000000, %rax      # imm = 0x80000000
cmovlq %rax, %r12
negl %ebx
orq $-0x80000000, %rbx      # imm = 0x80000000
cmpq $-0x7fffffff, %r12      # imm = 0x80000001
cmovneq %r12, %r11
movl %r11d, (%r10,%rdi,4)
movq %rbx, (%r9,%rdi,8)
cmpq $-0x80000000, %r11      # imm = 0x80000000
jne 0x1d41f4
movq 0x20(%rsp), %r12
jmp 0x1d4257
movq 0x118(%rsp), %rcx
cmpq $-0x1, %rcx
je 0x1d4212
movl 0xc(%r8), %eax
cmpq %rcx, %rax
movq 0x20(%rsp), %r12
je 0x1d422a
jmp 0x1d4257
movl 0x8(%r8), %eax
addq 0x30(%rsp), %rax
cmpq 0x100(%rsp), %rax
movq 0x20(%rsp), %r12
jl 0x1d4257
movq 0x68(%rsp), %rax
cmpq %rax, %r11
cmovgq %r11, %rax
movq %rax, 0x68(%rsp)
movq 0x48(%rsp), %rax
cmovgeq %rdi, %rax
movq %rax, 0x48(%rsp)
testq %rbp, %rbp
js 0x1d4254
cmpl %r11d, (%r10,%rbp,4)
jge 0x1d4257
movq %rdi, %rbp
incq %rdi
incq 0x80(%rsp)
addq $0x1c, %r14
cmpq 0x90(%rsp), %rdi
jne 0x1d3dd1
jmp 0x1d4340
movl 0xc(%rsi), %eax
movl 0x18(%rsi), %r14d
shrdl $0x1f, %eax, %r14d
xorl $0x1, %r14d
movq %r8, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6f050
movq 0x20(%rsp), %rdi
movl 0x44(%rsp), %esi
movl %r14d, %edx
movq 0x30(%rsp), %rcx
movsd 0x60(%rsp), %xmm0
movq %rax, %r8
callq 0x1d1b71
testq %rax, %rax
je 0x1d42e8
movq 0x28(%rsp), %r10
movslq (%r10,%rbp,4), %rax
movq 0x8(%rsp), %r11
addq %r11, %rax
movq 0x18(%rsp), %r9
movq 0x110(%rsp), %rsi
movq 0x58(%rsp), %r14
movq (%rsp), %rdi
jmp 0x1d430f
movq $-0x80000000, %rax      # imm = 0x80000000
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x110(%rsp), %rsi
movq 0x58(%rsp), %r14
movq (%rsp), %rdi
movq 0x8(%rsp), %r11
cmpq $-0x80000000, %rax      # imm = 0x80000000
setne %cl
cmpq %r12, %rax
setg %dl
testb %dl, %cl
cmovneq %rax, %r12
cmovneq %rbp, %rbx
movq 0x10(%rsp), %r8
jmp 0x1d4187
movq $-0x80000000, 0x48(%rsp) # imm = 0x80000000
movq 0x18(%rsp), %r9
movq 0x48(%rsp), %rcx
cmpq $-0x80000000, %rcx      # imm = 0x80000000
jne 0x1d4352
xorl %eax, %eax
jmp 0x1d439f
xorl %eax, %eax
testq %rcx, %rcx
js 0x1d4366
movq %rcx, (%r15,%rax,8)
incq %rax
movq (%r9,%rcx,8), %rcx
jmp 0x1d4354
testq %rax, %rax
je 0x1d439f
movq %r13, %rcx
movq %rax, %rdx
movq -0x8(%r15,%rdx,8), %rsi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
movupd (%r13,%rdi), %xmm0
movups 0xc(%r13,%rdi), %xmm1
movups %xmm1, 0xc(%rcx)
movupd %xmm0, (%rcx)
addq $0x1c, %rcx
decq %rdx
jne 0x1d4371
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq