// File: /workspace/github_cpp_makefile_star2_O3/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, 0xa0(%rsp)
movq %r8, 0x80(%rsp)
movsd %xmm0, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rdi, 0x98(%rsp)
movq 0xf8(%rsp), %r13
testq %r13, %r13
jle 0x1c0690
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 0x1c003a
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 0x1c003a
movq %rax, %rcx
jmp 0x1c0067
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1c0067
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq 0xf0(%rsp), %rdi
movq %rbx, %rsi
callq 0x19e55c
movq %r15, %rcx
leaq (%r14,%r15), %rdx
incq %rdx
incq %r15
addq $0x1c, %rbx
movq %rcx, %rax
cmpq $0x1, %rdx
jne 0x1c0011
leaq (,%r13,4), %rdx
movq %r12, %rdi
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x20e0
incq 0x80(%rsp)
movq 0xf0(%rsp), %r15
leaq -0x14(%r15), %rax
movq %rax, 0x90(%rsp)
movq $-0x80000000, 0x50(%rsp) # imm = 0x80000000
movq $-0x1, 0x88(%rsp)
movq $-0x1, 0x48(%rsp)
movq $-0x80000000, 0x58(%rsp) # imm = 0x80000000
movq $-0x1, 0x20(%rsp)
xorl %r11d, %r11d
movq %r11, %rbx
leaq (%r12,%r12,8), %rax
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, 0x44(%rsp)
xorl $0x1, %esi
movq 0x60(%rsp), %rdi
movl %esi, 0x40(%rsp)
callq 0x1a4c50
movl 0x4(%r15,%r14), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x70(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x68(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x80(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movl %r12d, %edi
movq %r15, %rsi
callq 0x1a83b5
testb $0x40, 0x1b(%r15,%r14)
jne 0x1c0171
movq $0x0, 0x30(%rsp)
jmp 0x1c017e
movl 0x8(%r13), %ecx
subl 0x4(%r13), %ecx
movq %rcx, 0x30(%rsp)
movq %r13, %rdi
movq %r12, 0x38(%rsp)
cltq
movq %rax, %rcx
subq %rbx, %rcx
movq %rax, %r11
movq 0xe0(%rsp), %rdx
subq %rdx, %r11
cmpq %rdx, %rcx
cmovleq %rbx, %r11
cmpq %rax, %r11
movq %r13, 0x18(%rsp)
jle 0x1c01c5
movq $-0x1, 0x28(%rsp)
movl $0x1, %esi
movq 0x30(%rsp), %r13
jmp 0x1c034c
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rbp
addq %r15, %rbp
addq %rax, %rbp
movl $0x1, %r15d
movq $-0x1, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x30(%rsp), %rbx
movq %rax, %r14
movl 0x8(%rbp), %eax
cmpl 0x4(%rdi), %eax
jbe 0x1c0343
movq %r11, %r12
movl 0xc(%rbp), %eax
movl 0x18(%rbp), %r13d
shrdl $0x1f, %eax, %r13d
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x98(%rsp), %r8
xorl %r9d, %r9d
callq 0x6ea22
cmpl %r13d, 0x44(%rsp)
jne 0x1c0255
movq %rbx, %r13
movq %r15, %rsi
movq %r12, %r11
movq 0x18(%rsp), %rdi
leaq -0x1(%r14), %rax
addq $-0x1c, %rbp
movq %rsi, %r15
movq %r13, %rbx
cmpq %r11, %r14
movq %rax, %r14
jg 0x1c01f3
jmp 0x1c034c
xorl $0x1, %r13d
leaq 0x78(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x60(%rsp), %rsi
movl 0x40(%rsp), %edx
movl %r13d, %ecx
movq 0x68(%rsp), %r8
movsd 0x70(%rsp), %xmm0
movq %rax, %r9
callq 0x1bcf0f
testq %rax, %rax
je 0x1c022c
cmpq $0x0, 0x78(%rsp)
movq %r12, %r11
movq 0x18(%rsp), %rdi
jne 0x1c02de
movq 0x118(%rsp), %rax
movq (%rax,%r14,8), %r13
addq 0x30(%rsp), %r13
movq 0x108(%rsp), %rax
movslq (%rax,%r14,4), %rsi
incq %rsi
cmpq %rbx, %r13
jg 0x1c02c2
jne 0x1c02e9
cmpq %rsi, %r15
jge 0x1c02e9
xorl %eax, %eax
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
setg %al
subq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %r14, 0x28(%rsp)
jmp 0x1c0319
movq %rbx, %r13
movq %r15, %rsi
jmp 0x1c023a
movq 0x100(%rsp), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, 0x38(%rsp)
jne 0x1c0313
movq 0x10(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jge 0x1c0343
incq %rax
movq %rax, 0x10(%rsp)
movq %rbx, %r13
movq %r15, %rsi
movq 0x110(%rsp), %rax
movq (%rax,%r14,8), %rax
testq %rax, %rax
js 0x1c023a
movq 0x100(%rsp), %rcx
movq 0x38(%rsp), %rdx
movl %edx, (%rcx,%rax,4)
jmp 0x1c023a
movq %r14, %rax
movq %rbx, %r13
movq %r15, %rsi
movq %rsi, 0x10(%rsp)
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
movq 0xf0(%rsp), %r15
movq 0x118(%rsp), %rbx
movq 0x108(%rsp), %r14
js 0x1c03a3
movq 0x18(%rsp), %rcx
movl 0x8(%rcx), %ecx
leaq (,%rbp,8), %rdx
addq %rbp, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rbp, %rdx
movl 0x8(%r15,%rdx), %edx
addq 0xe8(%rsp), %rdx
cmpq %rcx, %rdx
jge 0x1c0427
movq 0x38(%rsp), %r12
cmpq %r11, %r12
jle 0x1c04b7
movq 0x18(%rsp), %rcx
movl 0x8(%rcx), %ecx
movq $-0x1, %rbp
movq $-0x80000000, %rdi      # imm = 0x80000000
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %rsi
movq $-0x80000000, %r8       # imm = 0x80000000
movl (%rdx), %r9d
addq 0xe8(%rsp), %r9
cmpq %rcx, %r9
jl 0x1c0427
movq (%rbx,%rsi,8), %r9
cmpq %r9, %r8
jge 0x1c0403
movslq (%r14,%rsi,4), %rdi
movq %rsi, %rbp
movq %r9, %r8
jmp 0x1c0417
jne 0x1c0417
movslq (%r14,%rsi,4), %r10
cmpq %r10, %rdi
jge 0x1c0417
movq %rsi, %rbp
movq %r9, %r8
movq %r10, %rdi
leaq -0x1(%rsi), %r9
addq $-0x1c, %rdx
cmpq %r11, %rsi
movq %r9, %rsi
jg 0x1c03de
testq %rbp, %rbp
js 0x1c04ea
cmpq %rax, %rbp
jge 0x1c04ea
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %rax
addq %rbp, %rax
movq 0x18(%rsp), %rdi
movl 0x4(%rdi), %ecx
cmpl %ecx, 0x8(%r15,%rax)
movq 0x10(%rsp), %r8
jbe 0x1c05a6
leaq (%r15,%rax), %rsi
cmpl %ecx, 0x4(%rsi)
jae 0x1c05a6
movq %r11, 0x20(%rsp)
movl 0xc(%r15,%rax), %ecx
movl 0x18(%r15,%rax), %r12d
shrdl $0x1f, %ecx, %r12d
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x98(%rsp), %r8
xorl %r9d, %r9d
callq 0x6ea22
cmpl %r12d, 0x44(%rsp)
jne 0x1c0530
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
jmp 0x1c05a6
leaq (%rbx,%r12,8), %rax
movq %r13, (%rax)
movq 0x110(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rdx, (%rsi,%r12,8)
movq 0x10(%rsp), %r8
movl %r8d, (%r14,%r12,4)
movq %r12, 0x20(%rsp)
movq 0x100(%rsp), %r9
jmp 0x1c0607
movq 0x38(%rsp), %r12
leaq (%rbx,%r12,8), %rax
movq %r13, (%rax)
movq 0x110(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rcx, (%rsi,%r12,8)
movq 0x10(%rsp), %r8
movl %r8d, (%r14,%r12,4)
movq %r12, 0x20(%rsp)
testq %rbp, %rbp
movq 0x100(%rsp), %r9
movq 0x18(%rsp), %rdi
jns 0x1c05cf
jmp 0x1c0607
xorl $0x1, %r12d
leaq 0x78(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x60(%rsp), %rsi
movl 0x40(%rsp), %edx
movl %r12d, %ecx
movq 0x68(%rsp), %r8
movsd 0x70(%rsp), %xmm0
movq %rax, %r9
callq 0x1bcf0f
testq %rax, %rax
je 0x1c04a3
cmpq $0x0, 0x78(%rsp)
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
jne 0x1c05a6
movq 0x30(%rsp), %rdx
addq (%rbx,%rbp,8), %rdx
movslq (%r14,%rbp,4), %rax
incq %rax
movq %rdx, %rcx
cmpq %r13, %rdx
jg 0x1c059b
jne 0x1c05a6
cmpq %rax, %r8
jge 0x1c05a6
movq %rcx, %r13
movq %rax, %r8
movq %rbp, 0x28(%rsp)
movq 0x38(%rsp), %r12
leaq (%rbx,%r12,8), %rax
movq %r13, (%rax)
movq 0x110(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rcx, (%rsi,%r12,8)
movl %r8d, (%r14,%r12,4)
movq 0x100(%rsp), %r9
movl 0x8(%rdi), %ecx
leaq (,%rbp,8), %rdx
addq %rbp, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rbp, %rdx
movl 0x8(%r15,%rdx), %edx
addq 0xe8(%rsp), %rdx
cmpq %rcx, %rdx
jl 0x1c0602
movq (%rbx,%rbp,8), %rcx
cmpq (%rbx,%r12,8), %rcx
cmovlq %r12, %rbp
movq %rbp, 0x20(%rsp)
movq (%rax), %rax
movslq %r8d, %rcx
cmpq %rax, 0x58(%rsp)
jge 0x1c062d
movq %rax, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movq 0xf8(%rsp), %rdx
jmp 0x1c064d
movq 0xf8(%rsp), %rdx
jne 0x1c064d
cmpq %rcx, 0x50(%rsp)
jge 0x1c064d
movq %rax, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %r12, 0x48(%rsp)
incq %r12
incq 0x88(%rsp)
addq $0x1c, 0x90(%rsp)
cmpq %rdx, %r12
jne 0x1c00e2
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
movq 0x120(%rsp), %r8
js 0x1c0697
xorl %eax, %eax
movl %ecx, (%r9,%rax,4)
incq %rax
movq (%rsi,%rcx,8), %rcx
testq %rcx, %rcx
jns 0x1c067e
jmp 0x1c0699
xorl %eax, %eax
jmp 0x1c071e
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 0x1c071e
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 0x1c0709
movl %esi, 0x4(%r8)
movl (%rcx), %esi
cmpl 0x8(%r8), %esi
jbe 0x1c0715
movl %esi, 0x8(%r8)
addq $0x1c, %rcx
decq %rdx
jne 0x1c06d7
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq