// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: flat_contain(All_reads*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, long, long, ul_ov_t*, long, int*, int*, long*, long*, ul_ov_t*)
int64_t flat_contain(All_reads *ridx, const ul_idx_t *uref, 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, 
ul_ov_t *a, int64_t a_n, int32_t *t, int32_t *c_n, int64_t *p, int64_t *f, ul_ov_t *idx)
{
	if(a_n <= 0) return 0;
	int64_t mm_ovlp, x, i, j, st, max_ii, mm_sc, mm_n, mm_idx, n_skip, end_j, qo, sc, sn, is_c, cl, csc; 
	uint32_t li_v, lj_v; ul_ov_t *li, *lj; int64_t max, max_n, tot_sc = INT32_MIN, tot_n = INT32_MIN, tot_i = -1;
	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;
		}
	}
	// fprintf(stderr, "\n[M::%s::] sc::%u\n", __func__, idx->qn);
	memset(t, 0, (a_n*sizeof((*t)))); 
	for (i = st = 0, max_ii = -1; i < a_n; ++i) {
        li = &(a[i]); li_v = (li->tn<<1)|li->rev;
		// fprintf(stderr, "[i::%ld] (id::%u)%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\tc::%u\n", i, li->tn,
        //             (int)Get_NAME_LENGTH(R_INF, a[i].tn), Get_NAME(R_INF, a[i].tn), 
		// 			"+-"[a[i].rev], a[i].qs, a[i].qe, a[i].ts, a[i].te, !a[i].el);
        mm_ovlp = max_ovlp_src(uopt, li_v^1);        
        x = (li->qs + mm_ovlp)*diff_ec_ul; 
        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 = ((li->el)?(li->qe-li->qs):(0));
        mm_sc = csc; mm_n = 1; mm_idx = -1; n_skip = 0; end_j = -1; 
        if ((x-st) > max_iter) st = x-max_iter;
		for (j = x; j >= st; --j) { // collect potential destination vertices
            lj = &(a[j]); lj_v = (lj->tn<<1)|lj->rev;
            if(lj->qe <= li->qs) break;//even this pair has a overlap, its length will be very small; just ignore
            // if(lj->qs >= li->qs) continue;///no contain
            qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
            if(li_v != lj_v && get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, bw, diff_ec_ul, qo, &is_c)) {
				if(is_c == 0) {
					sc = f[j] + csc; sn = c_n[j] + 1;
					// if(li->tn == 20171) {
					// 	fprintf(stderr, "[i::%ld] (id::%u)%.*s\t%c\tj::%ld\tsc::%ld\tsn::%ld\n", i, li->tn,
					// 		(int)Get_NAME_LENGTH(R_INF, a[i].tn), Get_NAME(R_INF, a[i].tn), 
					// 		"+-"[a[i].rev], j, sc, sn);
					// }
					if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_n))) {
						mm_sc = sc, mm_idx = j; mm_n = sn;
						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_n = 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_n < c_n[j]))) {
                    max = f[j]; max_n = c_n[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]); lj_v = (lj->tn<<1)|lj->rev;
            if(lj->qe > li->qs && lj->qs < li->qs) {
				qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
                if(li_v != lj_v && get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, bw, diff_ec_ul, qo, &is_c)) {
					if(is_c == 0) {
						sc = f[max_ii] + csc; sn = c_n[max_ii] + 1;
						if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_n))) {
							mm_sc = sc; mm_idx = max_ii; mm_n = sn;
						}
					}
				}
            }
        }

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

		if((tot_sc < f[i]) || ((tot_sc == f[i]) && (tot_n < c_n[i]))) {
			tot_sc = f[i]; tot_n = c_n[i]; tot_i = i;
		}
	}
	cl = 0; i = tot_i; 
	while (i >= 0) {
        t[cl++] = i; i = p[i];
    }
	idx->qs = a[t[cl-1]].qs; idx->qe = a[t[cl-1]].qe;
	for (i = 0; i < cl; i++) {
		a[i] = a[t[cl-i-1]];
		if(a[i].qs < idx->qs) idx->qs = a[i].qs;
		if(a[i].qe > idx->qe) idx->qe = a[i].qe;
	}
	return cl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x98(%rsp)
movq %r8, 0x68(%rsp)
movsd %xmm0, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rdi, 0x90(%rsp)
movq 0xf8(%rsp), %r13
testq %r13, %r13
jle 0x1b6a97
movq 0x100(%rsp), %r12
movq 0xf0(%rsp), %rax
movq %r13, %r14
negq %r14
leaq 0x1c(%rax), %rbx
movl $0x1, %r15d
xorl %eax, %eax
movq %r14, %rcx
addq %r15, %rcx
je 0x1b64d2
movl 0x8(%rbx), %ecx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movq 0xf0(%rsp), %rsi
cmpl 0x8(%rsi,%rdx), %ecx
jne 0x1b64d2
movq %rax, %rcx
jmp 0x1b64ff
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1b64ff
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq 0xf0(%rsp), %rdi
movq %rbx, %rsi
callq 0x1957bd
movq %r15, %rcx
leaq (%r14,%r15), %rdx
incq %rdx
incq %r15
addq $0x1c, %rbx
movq %rcx, %rax
cmpq $0x1, %rdx
jne 0x1b64a9
leaq (,%r13,4), %rdx
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
incq 0x68(%rsp)
movq 0xf0(%rsp), %r15
leaq -0x14(%r15), %rax
movq %rax, 0x80(%rsp)
movq $-0x80000000, 0x70(%rsp) # imm = 0x80000000
movq $-0x1, 0x78(%rsp)
movq $-0x1, %rsi
movq $-0x80000000, 0x88(%rsp) # imm = 0x80000000
movq $-0x1, %r8
movq $0x0, 0x28(%rsp)
movq %r8, 0x8(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0x28(%rsp), %rbx
leaq (%r14,%r14,8), %rax
movq %r14, %r12
leaq (%rax,%rax,2), %r14
addq %r12, %r14
leaq (%r15,%r14), %r13
movl 0xc(%r15,%r14), %eax
movl 0x18(%r15,%r14), %esi
shrdl $0x1f, %eax, %esi
movl %esi, 0x34(%rsp)
xorl $0x1, %esi
movq 0x48(%rsp), %rdi
movl %esi, 0x30(%rsp)
callq 0x19c00f
movl 0x4(%r15,%r14), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x58(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x50(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x68(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movq %r12, 0x18(%rsp)
movl %r12d, %edi
movq %r15, %rsi
callq 0x19f679
testb $0x40, 0x1b(%r15,%r14)
jne 0x1b661c
movq $0x0, 0x20(%rsp)
jmp 0x1b6629
movl 0x8(%r13), %ecx
subl 0x4(%r13), %ecx
movq %rcx, 0x20(%rsp)
movq %r13, %rdi
cltq
movq %rax, %rcx
subq %rbx, %rcx
movq %rax, %rsi
movq 0xe0(%rsp), %rdx
subq %rdx, %rsi
cmpq %rdx, %rcx
cmovleq %rbx, %rsi
movq %rsi, 0x28(%rsp)
cmpq %rax, %rsi
movq %r13, 0x10(%rsp)
jle 0x1b6671
movq $-0x1, 0x38(%rsp)
movl $0x1, %r13d
movq 0x20(%rsp), %rbp
jmp 0x1b67f3
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r14
addq %r15, %r14
addq %rax, %r14
movl $0x1, %r12d
movq $-0x1, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x20(%rsp), %r15
movq %rax, %rbx
movl 0x8(%r14), %eax
cmpl 0x4(%rdi), %eax
jbe 0x1b67ea
movl 0xc(%r14), %eax
movl 0x18(%r14), %r13d
shrdl $0x1f, %eax, %r13d
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x90(%rsp), %r8
xorl %r9d, %r9d
callq 0x6f050
cmpl %r13d, 0x34(%rsp)
jne 0x1b66ff
movq %r15, %rbp
movq %r12, %r13
movq 0x10(%rsp), %rdi
leaq -0x1(%rbx), %rax
addq $-0x1c, %r14
movq %r13, %r12
movq %rbp, %r15
cmpq 0x28(%rsp), %rbx
movq %rax, %rbx
jg 0x1b669f
jmp 0x1b67f3
xorl $0x1, %r13d
leaq 0x60(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x48(%rsp), %rsi
movl 0x30(%rsp), %edx
movl %r13d, %ecx
movq 0x50(%rsp), %r8
movsd 0x58(%rsp), %xmm0
movq %rax, %r9
callq 0x1b3728
testq %rax, %rax
je 0x1b66d7
cmpq $0x0, 0x60(%rsp)
movq 0x10(%rsp), %rdi
jne 0x1b6785
movq 0x118(%rsp), %rax
movq (%rax,%rbx,8), %rbp
addq 0x20(%rsp), %rbp
movq 0x108(%rsp), %rax
movslq (%rax,%rbx,4), %r13
incq %r13
cmpq %r15, %rbp
jg 0x1b6769
jne 0x1b6790
cmpq %r13, %r12
jge 0x1b6790
xorl %eax, %eax
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
setg %al
subq %rax, %rcx
movq %rcx, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
jmp 0x1b67c0
movq %r15, %rbp
movq %r12, %r13
jmp 0x1b66e2
movq 0x100(%rsp), %rax
movslq (%rax,%rbx,4), %rax
cmpq %rax, 0x18(%rsp)
jne 0x1b67ba
movq 0x40(%rsp), %rax
cmpq 0x98(%rsp), %rax
jge 0x1b67ea
incq %rax
movq %rax, 0x40(%rsp)
movq %r15, %rbp
movq %r12, %r13
movq 0x110(%rsp), %rax
movq (%rax,%rbx,8), %rax
testq %rax, %rax
js 0x1b66e2
movq 0x100(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %edx, (%rcx,%rax,4)
jmp 0x1b66e2
movq %rbx, %rax
movq %r15, %rbp
movq %r12, %r13
cmpq $0x0, 0x8(%rsp)
movq 0xf0(%rsp), %r15
movq 0x118(%rsp), %rbx
movq 0x108(%rsp), %r10
movq 0xe8(%rsp), %r11
movq 0x18(%rsp), %r14
js 0x1b6844
movl 0x8(%rdi), %ecx
movq 0x8(%rsp), %rsi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movl 0x8(%r15,%rdx), %edx
addq %r11, %rdx
cmpq %rcx, %rdx
jge 0x1b68c6
movq $-0x1, 0x8(%rsp)
cmpq 0x28(%rsp), %r14
jle 0x1b68c6
movl 0x8(%rdi), %ecx
movq $-0x1, 0x8(%rsp)
movq $-0x80000000, %rdx      # imm = 0x80000000
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %r12
movq $-0x80000000, %r8       # imm = 0x80000000
movl (%r12), %r9d
addq %r11, %r9
cmpq %rcx, %r9
jl 0x1b68c6
movq (%rbx,%rsi,8), %r9
cmpq %r9, %r8
jge 0x1b689e
movslq (%r10,%rsi,4), %rdx
movq %rsi, 0x8(%rsp)
movq %r9, %r8
jmp 0x1b68b4
jne 0x1b68b4
movq %r10, %rdi
movslq (%r10,%rsi,4), %r10
cmpq %r10, %rdx
movq %rdi, %r10
movq 0x10(%rsp), %rdi
jl 0x1b6890
addq $-0x1c, %r12
leaq -0x1(%rsi), %r9
cmpq 0x28(%rsp), %rsi
movq %r9, %rsi
jg 0x1b687b
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
js 0x1b69cc
cmpq %rax, %rcx
jge 0x1b69cc
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movl 0x4(%rdi), %ecx
cmpl %ecx, 0x8(%r15,%rax)
jbe 0x1b69cc
leaq (%r15,%rax), %rsi
cmpl %ecx, 0x4(%rsi)
jae 0x1b69cc
movq %r10, %r12
movl 0xc(%r15,%rax), %ecx
movl 0x18(%r15,%rax), %r14d
shrdl $0x1f, %ecx, %r14d
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x90(%rsp), %r8
xorl %r9d, %r9d
callq 0x6f050
cmpl %r14d, 0x34(%rsp)
jne 0x1b694a
movq 0xe8(%rsp), %r11
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rdi
movq %r12, %r10
jmp 0x1b69cc
xorl $0x1, %r14d
leaq 0x60(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x48(%rsp), %rsi
movl 0x30(%rsp), %edx
movl %r14d, %ecx
movq 0x50(%rsp), %r8
movsd 0x58(%rsp), %xmm0
movq %rax, %r9
callq 0x1b3728
testq %rax, %rax
je 0x1b6930
cmpq $0x0, 0x60(%rsp)
movq 0xe8(%rsp), %r11
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rdi
movq %r12, %r10
jne 0x1b69cc
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rdx
addq (%rbx,%rax,8), %rdx
movslq (%r10,%rax,4), %rax
incq %rax
movq %rdx, %rcx
cmpq %rbp, %rdx
jg 0x1b69bc
jne 0x1b69cc
cmpq %rax, %r13
jge 0x1b69cc
movq %rcx, %rbp
movq %rax, %r13
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rbp, (%rbx,%r14,8)
movq 0x110(%rsp), %rdx
movq 0x38(%rsp), %rax
movq %rax, (%rdx,%r14,8)
movl %r13d, (%r10,%r14,4)
movq 0x8(%rsp), %r8
testq %r8, %r8
js 0x1b6a14
movl 0x8(%rdi), %eax
leaq (%r8,%r8,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r8, %rcx
movl 0x8(%r15,%rcx), %ecx
addq %r11, %rcx
cmpq %rax, %rcx
jl 0x1b6a17
movq (%rbx,%r8,8), %rax
cmpq (%rbx,%r14,8), %rax
jge 0x1b6a17
movq %r14, %r8
movq (%rbx,%r14,8), %rax
cmpq %rax, 0x88(%rsp)
movq 0xa0(%rsp), %rsi
jl 0x1b6a39
jne 0x1b6a4c
movslq %r13d, %rcx
cmpq %rcx, 0x70(%rsp)
jge 0x1b6a4c
movslq %r13d, %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x88(%rsp)
movq %r14, %rsi
incq %r14
addq $0x1c, 0x80(%rsp)
incq 0x78(%rsp)
movq 0xf8(%rsp), %rax
cmpq %rax, %r14
movq 0x100(%rsp), %r9
jne 0x1b6579
testq %rsi, %rsi
movq 0x120(%rsp), %r8
js 0x1b6a9e
xorl %eax, %eax
movl %esi, (%r9,%rax,4)
incq %rax
movq (%rdx,%rsi,8), %rsi
testq %rsi, %rsi
jns 0x1b6a85
jmp 0x1b6aa0
xorl %eax, %eax
jmp 0x1b6b25
xorl %eax, %eax
movslq -0x4(%r9,%rax,4), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0x4(%r15,%rdx), %ecx
movl %ecx, 0x4(%r8)
movslq -0x4(%r9,%rax,4), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0x8(%r15,%rdx), %ecx
movl %ecx, 0x8(%r8)
testq %rax, %rax
je 0x1b6b25
leaq 0x8(%r15), %rcx
movq %rax, %rdx
movslq -0x4(%r9,%rdx,4), %rsi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
movupd (%r15,%rdi), %xmm0
movups 0xc(%r15,%rdi), %xmm1
movupd %xmm0, -0x8(%rcx)
movups %xmm1, 0x4(%rcx)
movl -0x4(%rcx), %esi
cmpl 0x4(%r8), %esi
jae 0x1b6b10
movl %esi, 0x4(%r8)
movl (%rcx), %esi
cmpl 0x8(%r8), %esi
jbe 0x1b6b1c
movl %esi, 0x8(%r8)
addq $0x1c, %rcx
decq %rdx
jne 0x1b6ade
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq