// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %rcx, 0x80(%rsp)
testq %rsi, %rsi
je 0x1de388
movq %r9, %r14
movq %rsi, %r12
movq %rdi, %r13
movq %rdx, 0x18(%rsp)
movq %r9, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq 0x28(%r14), %rbx
movq (%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%r14), %rbp
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
addq %r12, %rsi
movq %r13, %rdi
callq 0x19e2b4
testq %r12, %r12
jle 0x1de38d
movq %rbx, 0x30(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %r12, %rbx
negq %rbx
leaq 0x1c(%r13), %r14
movl $0x1, %r15d
xorl %edi, %edi
movq %rbx, %rax
addq %r15, %rax
je 0x1ddd62
movl 0x8(%r14), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r13,%rcx), %eax
jne 0x1ddd62
movq %rdi, %rax
jmp 0x1ddd8a
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1ddd8a
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rdi
addq %rax, %rdi
movq %r14, %rsi
callq 0x19e55c
movq %r15, %rax
leaq (%rbx,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r14
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1ddd3f
leaq (,%r12,8), %rdx
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
incq 0x80(%rsp)
movq 0x50(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq -0x14(%r13), %rax
movq %rax, 0x90(%rsp)
movq $-0x1, 0x88(%rsp)
movq $-0x80000000, 0x58(%rsp) # imm = 0x80000000
movq $-0x80000000, 0x78(%rsp) # imm = 0x80000000
xorl %edi, %edi
movq $-0x1, %r15
xorl %esi, %esi
movq %r13, 0xa0(%rsp)
movq 0x18(%rsp), %rdx
movq %r12, 0x98(%rsp)
movq %rbp, 0x40(%rsp)
movq %rsi, %r12
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %r14
addq %rdi, %r14
movl 0xc(%r13,%r14), %eax
movl 0x18(%r13,%r14), %ecx
shrdl $0x1f, %eax, %ecx
xorl $0x1, %ecx
movq 0x28(%rdx), %rax
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rax
je 0x1dde94
movq 0x8(%rdx), %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 0x1dde6f
movl %esi, %eax
jmp 0x1dde96
xorl %eax, %eax
addq %r13, %r14
movl 0x4(%r14), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x20(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x80(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movq %rdi, 0x48(%rsp)
movq %r13, %rsi
callq 0x1a83b5
cltq
movl 0xc(%r14), %esi
movl 0x18(%r14), %ecx
xorl %edx, %edx
btl $0x1e, %ecx
setae %dl
movq %rdx, 0x70(%rsp)
shldl $0x1, %ecx, %esi
xorl $0x1, %esi
movl %esi, 0x2c(%rsp)
movq %rax, %rcx
subq %r12, %rcx
movq %rax, %rsi
movq 0xe8(%rsp), %rdx
subq %rdx, %rsi
cmpq %rdx, %rcx
cmovleq %r12, %rsi
cmpq %rax, %rsi
movq %rsi, 0x8(%rsp)
jle 0x1ddf4e
movq $-0x1, 0x68(%rsp)
movq $-0x7fffffff, %rbx      # imm = 0x80000001
movq 0x10(%rsp), %r9
movq 0xf0(%rsp), %r10
xorl %r11d, %r11d
jmp 0x1de0cc
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rbp
addq %r13, %rbp
addq %rax, %rbp
movq $-0x7fffffff, %r12      # imm = 0x80000001
movq $-0x1, 0x68(%rsp)
movq $0x0, 0x38(%rsp)
movq %rax, %r13
movq 0x10(%rsp), %r9
movq 0xf0(%rsp), %r10
xorl %r11d, %r11d
movl 0x8(%rbp), %eax
leal 0x80(%rax), %edx
movl 0x4(%r14), %ecx
cmpl %ecx, %edx
jbe 0x1de0c6
cmpl (%r9,%r13,4), %r11d
jo 0x1de01d
cmpl %ecx, 0x4(%rbp)
ja 0x1de01d
cmpl 0x8(%r14), %eax
ja 0x1de01d
movl 0xc(%rbp), %eax
movl 0x18(%rbp), %ebx
shrdl $0x1f, %eax, %ebx
xorl $0x1, %ebx
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6ea22
movq 0x18(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl %ebx, %edx
movq 0x20(%rsp), %rcx
movsd 0x60(%rsp), %xmm0
movq %rax, %r8
callq 0x1dbbdf
testq %rax, %rax
je 0x1de03c
movq 0x10(%rsp), %r9
movslq (%r9,%r13,4), %rbx
addq 0x70(%rsp), %rbx
cmpq $-0x80000000, %rbx      # imm = 0x80000000
movq 0xf0(%rsp), %r10
movl $0x0, %r11d
movq 0x8(%rsp), %rsi
jne 0x1de056
movq %r12, %rbx
leaq -0x1(%r13), %rax
addq $-0x1c, %rbp
movq %rbx, %r12
cmpq %rsi, %r13
movq %rax, %r13
jg 0x1ddf88
jmp 0x1de0cc
movq %r12, %rbx
movq 0x10(%rsp), %r9
movq 0xf0(%rsp), %r10
xorl %r11d, %r11d
movq 0x8(%rsp), %rsi
jmp 0x1de020
cmpq %r12, %rbx
jle 0x1de077
xorl %eax, %eax
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
setg %al
subq %rax, %rcx
movq %rcx, 0x38(%rsp)
movq %r13, 0x68(%rsp)
jmp 0x1de0a1
movq 0x30(%rsp), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, (%rax,%r13,8)
jne 0x1de09e
movq 0x38(%rsp), %rax
cmpq 0xe0(%rsp), %rax
jge 0x1de0c6
incq %rax
movq %rax, 0x38(%rsp)
movq %r12, %rbx
movq 0x40(%rsp), %rax
movq (%rax,%r13,8), %rax
testq %rax, %rax
js 0x1de020
movq 0x30(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
jmp 0x1de020
movq %r13, %rax
movq %r12, %rbx
testq %r15, %r15
movq 0xa0(%rsp), %r13
movq 0x98(%rsp), %r12
movq 0x40(%rsp), %rbp
js 0x1de102
movl 0x8(%r14), %ecx
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r15, %rdx
movl 0x8(%r13,%rdx), %edx
addq %r10, %rdx
cmpq %rcx, %rdx
jge 0x1de160
movq $-0x1, %r15
movq 0x8(%rsp), %rsi
cmpq %rsi, 0x48(%rsp)
jle 0x1de228
movl 0x8(%r14), %ecx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdi
movl (%rsi), %r8d
addq %r10, %r8
cmpq %rcx, %r8
jl 0x1de160
movslq (%r9,%rdi,4), %r8
cmpq %r8, %rdx
cmovlq %rdi, %r15
cmovleq %r8, %rdx
leaq -0x1(%rdi), %r8
addq $-0x1c, %rsi
cmpq 0x8(%rsp), %rdi
movq %r8, %rdi
jg 0x1de134
testq %r15, %r15
js 0x1de223
cmpq %rax, %r15
jge 0x1de223
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r15, %rsi
movl 0x8(%r13,%rsi), %eax
leal 0x80(%rax), %edx
movl 0x4(%r14), %ecx
cmpl %ecx, %edx
jbe 0x1de223
addq %r13, %rsi
cmpl %ecx, 0x4(%rsi)
ja 0x1de223
cmpl 0x8(%r14), %eax
ja 0x1de223
cmpl (%r9,%r15,4), %r11d
jo 0x1de223
movl 0xc(%rsi), %eax
movl 0x18(%rsi), %ebp
shrdl $0x1f, %eax, %ebp
xorl $0x1, %ebp
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6ea22
movq 0x18(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl %ebp, %edx
movq 0x20(%rsp), %rcx
movsd 0x60(%rsp), %xmm0
movq %rax, %r8
callq 0x1dbbdf
testq %rax, %rax
je 0x1de219
movq 0x10(%rsp), %r9
movslq (%r9,%r15,4), %rcx
addq 0x70(%rsp), %rcx
cmpq $-0x80000000, %rcx      # imm = 0x80000000
movq 0x40(%rsp), %rbp
movq 0x8(%rsp), %rsi
je 0x1de228
movq %r15, %rax
cmpq %rbx, %rcx
jg 0x1de27e
jmp 0x1de228
movq 0x40(%rsp), %rbp
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %rsi
movq 0x68(%rsp), %rax
cmpq $-0x1, %rax
jne 0x1de27b
movq 0x110(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x1de25a
movl 0xc(%r14), %ecx
xorl %eax, %eax
cmpq %rdx, %rcx
movq $-0x80000000, %rcx      # imm = 0x80000000
cmovneq %rcx, %rbx
sete %al
jmp 0x1de273
movl 0x4(%r14), %ecx
xorl %eax, %eax
cmpq %rcx, 0x50(%rsp)
movq $-0x80000000, %rcx      # imm = 0x80000000
cmovlq %rcx, %rbx
setge %al
negl %eax
orq $-0x80000000, %rax      # imm = 0x80000000
movq %rbx, %rcx
cmpq $-0x7fffffff, %rcx      # imm = 0x80000001
movq 0x70(%rsp), %r8
cmovneq %rcx, %r8
movq 0x48(%rsp), %rdi
movl %r8d, (%r9,%rdi,4)
movq %rax, (%rbp,%rdi,8)
cmpq $-0x80000000, %r8       # imm = 0x80000000
jne 0x1de2ac
movq 0x18(%rsp), %rdx
jmp 0x1de30a
movq 0x118(%rsp), %rcx
cmpq $-0x1, %rcx
movq 0x18(%rsp), %rdx
je 0x1de2ca
movl 0xc(%r14), %eax
cmpq %rcx, %rax
je 0x1de2dd
jmp 0x1de30a
movl 0x8(%r14), %eax
addq 0x20(%rsp), %rax
cmpq 0x100(%rsp), %rax
jl 0x1de30a
movq 0x78(%rsp), %rax
cmpq %rax, %r8
cmovgq %r8, %rax
movq %rax, 0x78(%rsp)
movq 0x58(%rsp), %rax
cmovgeq %rdi, %rax
movq %rax, 0x58(%rsp)
testq %r15, %r15
js 0x1de307
cmpl %r8d, (%r9,%r15,4)
jge 0x1de30a
movq %rdi, %r15
incq %rdi
incq 0x88(%rsp)
addq $0x1c, 0x90(%rsp)
cmpq %r12, %rdi
jne 0x1dde1b
movq 0x58(%rsp), %rax
testq %rax, %rax
js 0x1de388
xorl %r14d, %r14d
movq 0x30(%rsp), %r8
movq %rax, (%r8,%r14,8)
incq %r14
movq (%rbp,%rax,8), %rax
testq %rax, %rax
jns 0x1de339
leaq -0x1(%r14), %rax
movq %r14, %rcx
movq %r13, %rdx
movq -0x8(%r8,%rcx,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(%rdx)
movupd %xmm0, (%rdx)
addq $0x1c, %rdx
decq %rcx
addq $-0x1, %rax
jb 0x1de354
jmp 0x1de3a1
xorl %r14d, %r14d
jmp 0x1de3a1
shlq $0x3, %r12
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20e0
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq