// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_cns_chain_linear(ul_ov_t*, long, asg_t const*, R_to_U*, long, long, double, Chain_Data*, long, long, long, unsigned long, unsigned long, unsigned long)
uint64_t gen_cns_chain_linear(ul_ov_t *a, int64_t a_n, /**ul_ov_t *ab,**/ const asg_t *rg, R_to_U *ri, 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)
{
	if(a_n == 0) return 0;
	uint32_t li_v, lj_v; int32_t *f, *c_n, *len; int64_t *p, *t, st, max_ii, max, qo, cL, sn, ln, csn, mm_sn, cln, mm_ln; 
	int64_t mm_ovlp, x, i, j, sc, csc, mm_sc, mm_idx, n_skip, end_j, ch_sc, cn_sn, ch_ln, ch_i; ul_ov_t *li, *lj; 
	resize_Chain_Data(dp, a_n, NULL); 
	t = dp->tmp; f = dp->score; p = dp->pre; c_n = dp->occ; len = dp->indels;
        
	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 = cn_sn = INT32_MIN; ch_ln = INT32_MAX;
	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 = 0; csn = 1; cln = 0; 
		if(is_contain_r((*ri), li->tn)) {csc = -1; csn = 0; cln = rg->seq[li->tn].len;}
        mm_sc = INT32_MIN+1; mm_sn = INT32_MIN+1; mm_ln = INT32_MAX; 
		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 = sn = ln = 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; sn = c_n[j] + csn; ln = len[j] + cln;
				}
			}
			if(sc == INT32_MIN) continue;
			if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_sn)) || 
				((sc == mm_sc) && (sn == mm_sn) && (ln < mm_ln))) {
				mm_sc = sc, mm_idx = j, mm_sn = sn, mm_ln = ln;
				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 = sn = ln = 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; sn = c_n[max_ii] + csn; ln = len[max_ii] + cln;
				}
				if(sc != INT32_MIN) {
					if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_sn)) || 
						((sc == mm_sc) && (sn == mm_sn) && (ln < mm_ln))) {
						mm_sc = sc, mm_idx = max_ii, mm_sn = sn, mm_ln = ln;
					}
				}
			}
		}

		if((mm_idx == -1) && (((int64_t)li->qs) > ((int64_t)(mqs+bw)))) {
			mm_sc = mm_idx = mm_sn = INT32_MIN; mm_ln = INT32_MAX;
		}
		if((mm_sc==(INT32_MIN+1))) {
			mm_sc = csc; mm_sn = csn; mm_ln = cln;
		}

        
        f[i] = mm_sc; p[i] = mm_idx; c_n[i] = mm_sn; len[i] = mm_ln;
        if ((max_ii < 0) || ((a[i].qe<=max_dis+a[max_ii].qe) && (f[max_ii]<f[i]))) {
            max_ii = i;
        }

		if((mm_sc!=INT32_MIN) && (((int64_t)(a[i].qe+bw))>=((int64_t)(mqe)))) {
			if((mm_sc > ch_sc) || ((mm_sc == ch_sc) && (mm_sn > cn_sn)) || 
				((mm_sc == ch_sc) && (mm_sn == cn_sn) && (mm_ln < ch_ln))) {
				ch_sc = mm_sc; ch_i = i; cn_sn = mm_sn; ch_ln = mm_ln;
			}
		}
		// if(ulid == 442462) {
		// 	fprintf(stderr, "[M::%s::id->%u::%.*s] i::%ld, q::[%u, %u), t::[%u, %u), is_cr::%u, mm_idx::%ld, end::%u\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, !!(is_contain_r((*ri), li->tn)), mm_idx, !!(((int64_t)(a[i].qe+bw))>=((int64_t)(mqe))));
		// }
    }
	// 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 $0xe8, %rsp
movsd %xmm0, 0x98(%rsp)
movq %r9, 0x30(%rsp)
movq %r8, 0xb8(%rsp)
movq %rcx, 0xd8(%rsp)
movq %rdx, 0x58(%rsp)
testq %rsi, %rsi
je 0x18ec6b
movq %rsi, %r14
movq %rdi, %rbp
movq 0x140(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x120(%rsp), %r12
xorl %ebx, %ebx
movq %r12, %rdi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%r12), %rax
movq %rax, 0x48(%rsp)
movq (%r12), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x90(%rsp)
movq 0x20(%r12), %rax
movq %rax, 0x88(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x80(%rsp)
imulq $0x1c, %r14, %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156f5c
testq %r14, %r14
movl $0x0, %r15d
cmovgq %r14, %r15
movq %r15, 0xe0(%rsp)
negq %r15
leaq 0x1c(%rbp), %r12
pushq $0x1
popq %r13
leaq (%r15,%r13), %rax
cmpq $0x1, %rax
je 0x18e5f7
cmpq %r13, %r14
je 0x18e5ca
movl 0x8(%r12), %eax
imulq $0x1c, %rbx, %rcx
cmpl 0x8(%rbp,%rcx), %eax
jne 0x18e5ca
movq %rbx, %rax
jmp 0x18e5eb
movq %r13, %rcx
subq %rbx, %rcx
movq %r13, %rax
cmpq $0x2, %rcx
jl 0x18e5eb
imulq $0x1c, %rbx, %rdi
addq %rbp, %rdi
movq %r12, %rsi
callq 0x1571fa
movq %r13, %rax
incq %r13
addq $0x1c, %r12
movq %rax, %rbx
jmp 0x18e5a7
shlq $0x3, %r14
movq 0x48(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20e0
incq 0xb8(%rsp)
movq 0x70(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq -0x14(%rbp), %rax
movq %rax, 0xc8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0xa8(%rsp)
movq $-0x80000000, 0x78(%rsp) # imm = 0x80000000
pushq $-0x1
popq %rax
movq %rax, 0xc0(%rsp)
xorl %r10d, %r10d
movq %rax, %r11
xorl %r13d, %r13d
movq $-0x80000000, 0x68(%rsp) # imm = 0x80000000
movq $-0x80000000, 0xa0(%rsp) # imm = 0x80000000
movq 0x138(%rsp), %r15
movq %rbp, 0xb0(%rsp)
cmpq 0xe0(%rsp), %r13
je 0x18ec5a
movq %r10, %rbx
movq %r11, 0x38(%rsp)
imulq $0x1c, %r13, %r14
movl 0xc(%rbp,%r14), %eax
movl 0x18(%rbp,%r14), %esi
shrdl $0x1f, %eax, %esi
xorl $0x1, %esi
movq 0x58(%rsp), %rdi
callq 0x15d450
movl 0x4(%rbp,%r14), %ecx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x98(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x30(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0xb8(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movl %r13d, %edi
movq %rbp, %rsi
callq 0x160717
movq %r14, %rdi
movl 0xc(%rbp,%r14), %ecx
movq 0xd8(%rsp), %rdx
cmpq %rcx, (%rdx)
movq %r13, 0x10(%rsp)
jbe 0x18e750
movq 0x8(%rdx), %rdx
cmpl $0x0, (%rdx,%rcx,4)
movq 0x28(%rsp), %r8
movl $0x0, %r9d
js 0x18e773
movq 0x58(%rsp), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
pushq $-0x1
popq %rdx
movq %rdx, (%rsp)
jmp 0x18e78c
pushq $0x1
popq %rdx
movq %rdx, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, (%rsp)
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
jmp 0x18e78c
pushq $0x1
popq %rdx
movq %rdx, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, (%rsp)
movq 0x38(%rsp), %r11
addq %rbp, %rdi
movslq %eax, %r13
movl 0x18(%rdi), %eax
shrdl $0x1f, %ecx, %eax
xorl $0x1, %eax
movl %eax, 0x44(%rsp)
movq %r13, %rax
subq %rbx, %rax
movq %r13, %r10
movq 0x130(%rsp), %rcx
subq %rcx, %r10
cmpq %rcx, %rax
cmovleq %rbx, %r10
movq %rbp, %rax
imulq $0x1c, %r13, %rbp
addq %rax, %rbp
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq $-0x7fffffff, %r14      # imm = 0x80000001
movq $-0x7fffffff, %r12      # imm = 0x80000001
pushq $-0x1
popq %rax
movq %rax, 0xd0(%rsp)
movq $0x0, 0x50(%rsp)
movq %r10, 0x60(%rsp)
movq %rdi, 0x8(%rsp)
cmpq %r10, %r13
jl 0x18e9a5
movl 0x8(%rbp), %eax
leal 0x80(%rax), %edx
movl 0x4(%rdi), %ecx
cmpl %ecx, %edx
jbe 0x18e9a5
cmpl (%r8,%r13,4), %r9d
jo 0x18e828
cmpl %ecx, 0x4(%rbp)
ja 0x18e828
cmpl 0x8(%rdi), %eax
jbe 0x18e843
movq %r14, %rax
movq %rbx, %rdx
movq %r12, %rcx
decq %r13
addq $-0x1c, %rbp
movq %rax, %r14
movq %rdx, %rbx
movq %rcx, %r12
jmp 0x18e7fb
movl 0xc(%rbp), %eax
movl 0x18(%rbp), %r15d
shrdl $0x1f, %eax, %r15d
xorl $0x1, %r15d
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3d261
movq 0x58(%rsp), %rdi
movl 0x44(%rsp), %esi
movl %r15d, %edx
movq 0x30(%rsp), %rcx
movsd 0x98(%rsp), %xmm0
movq %rax, %r8
callq 0x18e462
testq %rax, %rax
je 0x18e8c8
movq 0x28(%rsp), %r8
movslq (%r8,%r13,4), %rcx
addq (%rsp), %rcx
cmpq $-0x80000000, %rcx      # imm = 0x80000000
movq 0x138(%rsp), %r15
movl $0x0, %r9d
movq 0x38(%rsp), %r11
movq 0x60(%rsp), %r10
jne 0x18e8e8
movq %r14, %rax
movq %rbx, %rdx
movq %r12, %rcx
jmp 0x18e96c
movq %r14, %rax
movq %rbx, %rdx
movq %r12, %rcx
movq 0x138(%rsp), %r15
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
movq 0x38(%rsp), %r11
jmp 0x18e967
movq 0x88(%rsp), %rax
movslq (%rax,%r13,4), %rax
addq 0x18(%rsp), %rax
movq 0x80(%rsp), %rdx
movslq (%rdx,%r13,4), %rdx
addq 0x20(%rsp), %rdx
cmpq %r12, %rcx
movq 0x8(%rsp), %rdi
jg 0x18e92a
jne 0x18e91b
cmpq %r14, %rax
jg 0x18e92a
cmpq %r12, %rcx
jne 0x18e976
cmpq %r14, %rax
jne 0x18e976
cmpq %rbx, %rdx
jge 0x18e976
xorl %esi, %esi
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
setg %sil
subq %rsi, %rdi
movq %rdi, 0x50(%rsp)
movq %r13, 0xd0(%rsp)
movq 0x90(%rsp), %rsi
movq (%rsi,%r13,8), %rsi
testq %rsi, %rsi
movq 0x48(%rsp), %rdi
js 0x18e96c
movq 0x10(%rsp), %r10
movq %r10, (%rdi,%rsi,8)
movq 0x60(%rsp), %r10
movq 0x8(%rsp), %rdi
jmp 0x18e831
movq 0x48(%rsp), %rax
movq 0x10(%rsp), %rcx
cmpq %rcx, (%rax,%r13,8)
jne 0x18e99a
movq 0x50(%rsp), %rax
cmpq 0x128(%rsp), %rax
jge 0x18e9a5
incq 0x50(%rsp)
movq %r14, %rax
movq %rbx, %rdx
movq %r12, %rcx
jmp 0x18e948
testq %r11, %r11
movq 0xb0(%rsp), %rbp
js 0x18e9c5
movl 0x8(%rdi), %eax
imulq $0x1c, %r11, %rcx
movl 0x8(%rbp,%rcx), %ecx
addq %r15, %rcx
cmpq %rax, %rcx
jge 0x18ea18
movq $-0x80000000, %rax      # imm = 0x80000000
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %rdx
pushq $-0x1
popq %r11
leaq 0x1(%rdx), %rsi
cmpq %r10, %rsi
jle 0x18ea18
movl 0x8(%rdi), %esi
movl (%rcx), %edi
addq %r15, %rdi
cmpq %rsi, %rdi
jl 0x18ea13
movslq (%r8,%rdx,4), %rsi
cmpq %rsi, %rax
cmovlq %rdx, %r11
cmovleq %rsi, %rax
decq %rdx
addq $-0x1c, %rcx
movq 0x8(%rsp), %rdi
jmp 0x18e9e0
movq 0x8(%rsp), %rdi
testq %r11, %r11
js 0x18ead3
cmpq %r13, %r11
jge 0x18ead3
imulq $0x1c, %r11, %rsi
movl 0x8(%rbp,%rsi), %eax
leal 0x80(%rax), %edx
movl 0x4(%rdi), %ecx
cmpl %ecx, %edx
jbe 0x18ead3
addq %rbp, %rsi
cmpl %ecx, 0x4(%rsi)
ja 0x18ead3
cmpl 0x8(%rdi), %eax
ja 0x18ead3
cmpl (%r8,%r11,4), %r9d
jo 0x18ead3
movl 0xc(%rsi), %eax
movl 0x18(%rsi), %ebp
shrdl $0x1f, %eax, %ebp
xorl $0x1, %ebp
movq 0x8(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %r11, %r13
callq 0x3d261
movq 0x58(%rsp), %rdi
movl 0x44(%rsp), %esi
movl %ebp, %edx
movq 0xb0(%rsp), %rbp
movq 0x30(%rsp), %rcx
movsd 0x98(%rsp), %xmm0
movq %rax, %r8
callq 0x18e462
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %r10
movq %r13, %r11
movq 0x28(%rsp), %r8
testq %rax, %rax
je 0x18ead3
movslq (%r8,%r11,4), %rcx
addq (%rsp), %rcx
cmpq $-0x80000000, %rcx      # imm = 0x80000000
jne 0x18ebf6
movq 0xd0(%rsp), %rcx
cmpq $-0x1, %rcx
je 0x18eaec
movq 0x10(%rsp), %r13
movq (%rsp), %rdx
jmp 0x18eb1c
movl 0x4(%rdi), %eax
pushq $-0x1
popq %rcx
cmpq %rax, 0x70(%rsp)
movq 0x10(%rsp), %r13
movq (%rsp), %rdx
jge 0x18eb1c
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq $-0x80000000, %r14      # imm = 0x80000000
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %rcx      # imm = 0x80000000
cmpq $-0x7fffffff, %r12      # imm = 0x80000001
cmoveq 0x18(%rsp), %r14
cmoveq 0x20(%rsp), %rbx
cmovneq %r12, %rdx
movl %edx, (%r8,%r13,4)
movq 0x90(%rsp), %rax
movq %rcx, (%rax,%r13,8)
movq 0x88(%rsp), %rax
movl %r14d, (%rax,%r13,4)
movq 0x80(%rsp), %rax
movl %ebx, (%rax,%r13,4)
testq %r11, %r11
js 0x18eb7d
movl 0x8(%rdi), %eax
imulq $0x1c, %r11, %rcx
movl 0x8(%rbp,%rcx), %ecx
addq %r15, %rcx
cmpq %rax, %rcx
jl 0x18eb80
movl (%r8,%r11,4), %eax
cmpl (%r8,%r13,4), %eax
jge 0x18eb80
movq %r13, %r11
cmpq $-0x80000000, %rdx      # imm = 0x80000000
je 0x18ebdd
movl 0x8(%rdi), %eax
addq 0x30(%rsp), %rax
cmpq 0x148(%rsp), %rax
jl 0x18ebdd
cmpq 0x78(%rsp), %rdx
jg 0x18ebc3
jne 0x18ebab
cmpq 0x68(%rsp), %r14
jg 0x18ebc3
cmpq 0x78(%rsp), %rdx
jne 0x18ebdd
cmpq 0x68(%rsp), %r14
jne 0x18ebdd
cmpq 0xa8(%rsp), %rbx
jge 0x18ebdd
movq %rdx, 0x78(%rsp)
movq %r14, 0x68(%rsp)
movq %rbx, 0xa8(%rsp)
movq %r13, 0xa0(%rsp)
incq %r13
incq 0xc0(%rsp)
addq $0x1c, 0xc8(%rsp)
jmp 0x18e67c
movq 0x88(%rsp), %rax
movslq (%rax,%r11,4), %rdx
addq 0x18(%rsp), %rdx
movq 0x80(%rsp), %rax
movslq (%rax,%r11,4), %rax
addq 0x20(%rsp), %rax
cmpq %r12, %rcx
jle 0x18ec28
movq %rdx, %r14
movq %rax, %rbx
movq %rcx, %r12
jmp 0x18ec52
jne 0x18ec34
cmpq %r14, %rdx
jle 0x18ec34
movq %rdx, %r14
jmp 0x18ec4f
cmpq %r12, %rcx
jne 0x18ead3
cmpq %r14, %rdx
jne 0x18ead3
cmpq %rbx, %rax
jge 0x18ead3
movq %rax, %rbx
movq %r11, %rcx
jmp 0x18eae1
movq 0xa0(%rsp), %rdx
cmpq $-0x80000000, %rdx      # imm = 0x80000000
jne 0x18ec7f
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
movq 0x48(%rsp), %rdi
movq 0x90(%rsp), %rcx
testq %rdx, %rdx
js 0x18eca0
movq %rdx, (%rdi,%rax,8)
incq %rax
movq (%rcx,%rdx,8), %rdx
jmp 0x18ec8e
movq %rbp, %rcx
movq %rax, %rdx
subq $0x1, %rdx
jb 0x18ec6d
imulq $0x1c, (%rdi,%rdx,8), %rsi
movupd (%rbp,%rsi), %xmm0
movups 0xc(%rbp,%rsi), %xmm1
movups %xmm1, 0xc(%rcx)
movupd %xmm0, (%rcx)
addq $0x1c, %rcx
jmp 0x18eca6