// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %r9, 0x88(%rsp)
movq %r8, 0x70(%rsp)
movsd %xmm0, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdi, 0x80(%rsp)
movq 0xe8(%rsp), %r12
testq %r12, %r12
jle 0x17596f
movq 0xe0(%rsp), %r13
movq %r12, %r14
negq %r14
leaq 0x1c(%r13), %rbx
pushq $0x1
popq %r15
xorl %eax, %eax
leaq (%r14,%r15), %rcx
cmpq $0x1, %rcx
je 0x1753f0
testq %rcx, %rcx
je 0x1753c3
movl 0x8(%rbx), %ecx
imulq $0x1c, %rax, %rdx
cmpl 0x8(%r13,%rdx), %ecx
jne 0x1753c3
movq %rax, %rcx
jmp 0x1753e4
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1753e4
imulq $0x1c, %rax, %rdi
addq %r13, %rdi
movq %rbx, %rsi
callq 0x1571fa
movq %r15, %rcx
incq %r15
addq $0x1c, %rbx
movq %rcx, %rax
jmp 0x1753a1
leaq (,%r12,4), %rdx
movq $0x0, 0x28(%rsp)
movq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
incq 0x70(%rsp)
leaq -0x14(%r13), %rax
movq %rax, 0x90(%rsp)
movq $-0x80000000, 0x40(%rsp) # imm = 0x80000000
pushq $-0x1
popq %rax
movq %rax, %rdi
xorl %ebp, %ebp
movq $-0x80000000, 0x38(%rsp) # imm = 0x80000000
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %r14
cmpq %r12, %rbp
je 0x1758c7
movq %rdi, 0x8(%rsp)
imulq $0x1c, %rbp, %r15
leaq (%r15,%r13), %rbx
movl 0xc(%r13,%r15), %eax
movl 0x18(%r13,%r15), %esi
shrdl $0x1f, %eax, %esi
movl %esi, 0x1c(%rsp)
xorl $0x1, %esi
movq 0x50(%rsp), %rdi
movl %esi, 0x18(%rsp)
callq 0x15d49a
movl 0x4(%r13,%r15), %ecx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x58(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x70(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movl %ebp, %edi
movq %r13, %rsi
callq 0x160717
testb $0x40, 0x1b(%r13,%r15)
movq %rbp, 0x48(%rsp)
jne 0x1754cf
xorl %esi, %esi
jmp 0x1754d5
movl 0x8(%rbx), %esi
subl 0x4(%rbx), %esi
movslq %eax, %r15
movq %r15, %rax
subq %r14, %rax
movq %r15, %rdx
movq 0xd0(%rsp), %rcx
subq %rcx, %rdx
cmpq %rcx, %rax
cmovleq %r14, %rdx
movq %rdx, 0x28(%rsp)
imulq $0x1c, %r15, %rbp
addq 0xe0(%rsp), %rbp
movq %rsi, 0x78(%rsp)
movq %rsi, %r14
pushq $0x1
popq %r12
pushq $-0x1
popq %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpq 0x28(%rsp), %r15
jl 0x175652
movl 0x8(%rbp), %eax
cmpl 0x4(%rbx), %eax
jbe 0x175652
movl 0xc(%rbp), %eax
movl 0x18(%rbp), %r13d
shrdl $0x1f, %eax, %r13d
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x80(%rsp), %r8
xorl %r9d, %r9d
callq 0x3d261
cmpl %r13d, 0x1c(%rsp)
jne 0x17557a
movq %r14, %rax
movq %r12, %rcx
decq %r15
addq $-0x1c, %rbp
movq %rax, %r14
movq %rcx, %r12
jmp 0x175521
xorl $0x1, %r13d
leaq 0x68(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x50(%rsp), %rsi
movl 0x18(%rsp), %edx
movl %r13d, %ecx
movq 0x58(%rsp), %r8
movsd 0x60(%rsp), %xmm0
movq %rax, %r9
callq 0x1728e0
testq %rax, %rax
je 0x175565
cmpq $0x0, 0x68(%rsp)
jne 0x175565
movq 0x108(%rsp), %rax
movq (%rax,%r15,8), %rax
addq 0x78(%rsp), %rax
movq 0xf8(%rsp), %rcx
movslq (%rcx,%r15,4), %rcx
incq %rcx
cmpq %r14, %rax
jg 0x1755df
jne 0x1755fb
cmpq %rcx, %r12
jge 0x1755fb
xorl %edx, %edx
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
setg %dl
subq %rdx, %rsi
movq %rsi, 0x20(%rsp)
movq %r15, 0x10(%rsp)
jmp 0x175628
movq 0xf0(%rsp), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, 0x48(%rsp)
jne 0x175622
movq 0x20(%rsp), %rax
cmpq 0x88(%rsp), %rax
jge 0x175652
incq 0x20(%rsp)
movq %r14, %rax
movq %r12, %rcx
movq 0x100(%rsp), %rdx
movq (%rdx,%r15,8), %rdx
testq %rdx, %rdx
js 0x17556b
movq 0xf0(%rsp), %rsi
movq 0x48(%rsp), %rdi
movl %edi, (%rsi,%rdx,4)
jmp 0x17556b
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
movq 0xe0(%rsp), %r13
movq 0x108(%rsp), %r9
movq 0xf8(%rsp), %r10
movq 0xd8(%rsp), %r11
movq 0x48(%rsp), %rbp
js 0x175695
movl 0x8(%rbx), %eax
imulq $0x1c, %rdi, %rcx
movl 0x8(%r13,%rcx), %ecx
addq %r11, %rcx
cmpq %rax, %rcx
jge 0x175713
movq $-0x80000000, %rcx      # imm = 0x80000000
movq %rbp, %rax
pushq $-0x1
popq %rdx
movq %rdx, 0x8(%rsp)
movq $-0x80000000, %rdx      # imm = 0x80000000
imulq $0x1c, %rax, %rsi
decq %rax
addq 0x90(%rsp), %rsi
leaq 0x1(%rax), %rdi
cmpq 0x28(%rsp), %rdi
jle 0x17570e
movl 0x8(%rbx), %edi
movl (%rsi), %r8d
addq %r11, %r8
cmpq %rdi, %r8
jl 0x17570e
movq (%r9,%rax,8), %rdi
cmpq %rdi, %rcx
jl 0x1756f3
jne 0x1756ea
movslq (%r10,%rax,4), %r8
cmpq %r8, %rdx
jl 0x175701
decq %rax
addq $-0x1c, %rsi
jmp 0x1756bd
movslq (%r10,%rax,4), %rdx
movq %rax, 0x8(%rsp)
movq %rdi, %rcx
jmp 0x1756ae
movq %rax, 0x8(%rsp)
movq %rdi, %rcx
movq %r8, %rdx
jmp 0x1756ae
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
js 0x175793
cmpq %r15, %rdi
jge 0x175793
imulq $0x1c, %rdi, %rax
movl 0x4(%rbx), %ecx
cmpl %ecx, 0x8(%r13,%rax)
jbe 0x175793
leaq (%rax,%r13), %rsi
cmpl %ecx, 0x4(%rsi)
jae 0x175793
movq %rdi, 0x8(%rsp)
movq %rbp, %r15
movl 0xc(%r13,%rax), %ecx
movl 0x18(%r13,%rax), %ebp
shrdl $0x1f, %ecx, %ebp
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x80(%rsp), %r8
xorl %r9d, %r9d
callq 0x3d261
cmpl %ebp, 0x1c(%rsp)
jne 0x175820
movq 0xe0(%rsp), %r13
movq 0x108(%rsp), %r9
movq 0xf8(%rsp), %r10
movq 0xd8(%rsp), %r11
movq %r15, %rbp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
movq %r14, (%r9,%rbp,8)
movq 0x100(%rsp), %rax
movq %rcx, (%rax,%rbp,8)
movl %r12d, (%r10,%rbp,4)
movq (%r9,%rbp,8), %rax
testq %rdi, %rdi
js 0x1757cf
movl 0x8(%rbx), %ecx
imulq $0x1c, %rdi, %rdx
movl 0x8(%r13,%rdx), %edx
addq %r11, %rdx
cmpq %rcx, %rdx
jl 0x1757d2
cmpq %rax, (%r9,%rdi,8)
jge 0x1757d2
movq %rbp, %rdi
cmpq %rax, 0x40(%rsp)
jge 0x1757f5
movslq %r12d, %rcx
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq %rbp, 0x30(%rsp)
movq 0xe8(%rsp), %r12
jmp 0x175818
jne 0x1757eb
movslq %r12d, %rcx
cmpq %rcx, 0x38(%rsp)
movq 0xe8(%rsp), %r12
jge 0x175818
movq %rax, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
incq %rbp
jmp 0x175440
xorl $0x1, %ebp
leaq 0x68(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x50(%rsp), %rsi
movl 0x18(%rsp), %edx
movl %ebp, %ecx
movq 0x58(%rsp), %r8
movsd 0x60(%rsp), %xmm0
movq %rax, %r9
callq 0x1728e0
testq %rax, %rax
je 0x17576b
cmpq $0x0, 0x68(%rsp)
movq 0xe0(%rsp), %r13
movq 0x108(%rsp), %r9
movq 0xf8(%rsp), %r10
movq 0xd8(%rsp), %r11
movq %r15, %rbp
movq 0x10(%rsp), %rcx
jne 0x1758bd
movq 0x8(%rsp), %rdi
movq 0x78(%rsp), %rsi
addq (%r9,%rdi,8), %rsi
movslq (%r10,%rdi,4), %rax
incq %rax
movq %rsi, %rdx
cmpq %r14, %rsi
jg 0x1758af
jne 0x175798
cmpq %rax, %r12
jge 0x175798
movq %rdx, %r14
movq %rax, %r12
movq %rdi, %rcx
jmp 0x175798
movq 0x8(%rsp), %rdi
jmp 0x175798
xorl %eax, %eax
movq 0x110(%rsp), %rdi
movq 0x30(%rsp), %rdx
testq %rdx, %rdx
js 0x1758f7
movq 0xf0(%rsp), %rcx
movl %edx, (%rcx,%rax,4)
incq %rax
movq 0x100(%rsp), %rcx
movq (%rcx,%rdx,8), %rdx
jmp 0x1758d6
movq 0xf0(%rsp), %rdx
movslq -0x4(%rdx,%rax,4), %rcx
imulq $0x1c, %rcx, %rcx
movl 0x4(%r13,%rcx), %ecx
movl %ecx, 0x4(%rdi)
movslq -0x4(%rdx,%rax,4), %rcx
imulq $0x1c, %rcx, %rcx
movl 0x8(%r13,%rcx), %ecx
movl %ecx, 0x8(%rdi)
leaq 0x8(%r13), %rcx
movq %rax, %rdx
subq $0x1, %rdx
jb 0x175971
movq 0xf0(%rsp), %rsi
movslq (%rsi,%rdx,4), %rsi
imulq $0x1c, %rsi, %rsi
movupd (%r13,%rsi), %xmm0
movups 0xc(%r13,%rsi), %xmm1
movupd %xmm0, -0x8(%rcx)
movups %xmm1, 0x4(%rcx)
movl -0x4(%rcx), %esi
cmpl 0x4(%rdi), %esi
jae 0x17595f
movl %esi, 0x4(%rdi)
movl (%rcx), %esi
cmpl 0x8(%rdi), %esi
jbe 0x175969
movl %esi, 0x8(%rdi)
addq $0x1c, %rcx
jmp 0x175928
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq