// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: filter_by_reliable_ovlp(unsigned int, kv_u_trans_t*, st_mt_t*, overlap_region_alloc*, ul_idx_t const*, double, unsigned int)
void filter_by_reliable_ovlp(uint32_t id, kv_u_trans_t *idx, st_mt_t *sp, overlap_region_alloc* ol, const ul_idx_t *udb, double sec_rate, uint32_t pre_filter)
{
	u_trans_t *a; uint64_t n, k, l, s, e, s0, e0, z, ov, rr; overlap_region *m, t;
	a = u_trans_a(*idx, id); n = u_trans_n(*idx, id);
	for (k = sp->n = 0; k < n; k++) {
		if(a[k].del) continue;
		if(a[k].f == RC_0 || a[k].f == RC_1) {
			z = a[k].tn; z <<= 1; z |= a[k].rev; z <<= 32; z |= k;
			kv_push(uint64_t, *sp, z);
			// fprintf(stderr, "***utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", 
			// 	a[k].qn+1, "lc"[udb->ug->u.a[a[k].qn].circ], udb->ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
			// 	a[k].tn+1, "lc"[udb->ug->u.a[a[k].tn].circ], udb->ug->u.a[a[k].tn].len, a[k].ts, a[k].te);
		}
	}
	if(!sp->n) return;
	for (k = 0; k < ol->length; k++) {
		if((!pre_filter) || (ol->list[k].x_pos_strand)) {///corresponding to the overlaps within idx
			z = ol->list[k].y_id; z <<= 1; z |= ol->list[k].y_pos_strand; 
			z <<= 32; z |= k; z |= ((uint64_t)0x80000000);
			kv_push(uint64_t, *sp, z);
			ol->list[k].x_pos_strand = 0;
		}
	}
	radix_sort_gfa64(sp->a, sp->a + sp->n);
	for (k = 1, l = 0; k <= sp->n; k++) {
		if(k == sp->n || (sp->a[l]>>32)!=(sp->a[k]>>32)) {
			if((k - l > 1) && (!(sp->a[l]&((uint64_t)0x80000000)))) {
				for (z = l; z < k; z++) {
					if(!(sp->a[z]&((uint64_t)0x80000000))) continue;
					rr = (uint32_t)(sp->a[z]-((uint64_t)0x80000000));
					ol->list[rr].x_pos_strand = 1;
				}
			}
			l = k;
		}
	}

	for (k = sp->n = 0; k < n; k++) {
		if(a[k].del) continue;
		if(a[k].f == RC_0 || a[k].f == RC_1) {
			kv_push(uint64_t, *sp, (((uint64_t)a[k].qs)<<32)|((uint64_t)a[k].qe));
		}
	}
	if(sp->n > 1) {
		radix_sort_gfa64(sp->a, sp->a + sp->n);
		for (k = z = 0; k < sp->n; k++) {
			s = sp->a[k]>>32; e = (uint32_t)sp->a[k];
			if(z > 0 && s <= ((uint32_t)sp->a[z-1])) {
				if(e > ((uint32_t)sp->a[z-1])) {
					sp->a[z-1] >>= 32; sp->a[z-1] <<= 32; sp->a[z-1] |= e;
				}
			} else {
				sp->a[z++] = sp->a[k];
			}
		}
		sp->n = z;
	}

	for (k = rr = 0; k < ol->length; k++) {
		m = &(ol->list[k]);
		if(m->x_pos_strand == 0) {
			s = m->x_pos_s; e = m->x_pos_e + 1; l = 0; 
			for (z = 0; z < sp->n; z++) {
				s0 = sp->a[z]>>32; e0 = (uint32_t)sp->a[z];
				ov = ((MIN(e, e0) > MAX(s, s0))? (MIN(e, e0) - MAX(s, s0)):0);
				l += ov;
				if((l) && (l > ((e-s)*sec_rate))) break;
			}
			if(z < sp->n) {
				// fprintf(stderr, ">[M::%s] utg%.6u%c -> utg%.6u%c\n", __func__, id+1, "lc"[udb->ug->u.a[id].circ], 
				// m->y_id+1, "lc"[udb->ug->u.a[m->y_id].circ]);
				continue;///filter by the overlaps of id/m->x_id
			}
			///filter by the overlaps of m->y_id
			if(filter_by_reliable_ovlp0(u_trans_a(*idx, m->y_id), u_trans_n(*idx, m->y_id), 
			m->y_pos_strand?udb->ug->u.a[m->y_id].len-m->y_pos_e-1:m->y_pos_s, 
			m->y_pos_strand?udb->ug->u.a[m->y_id].len-m->y_pos_s:m->y_pos_e+1, sec_rate)) {
				// fprintf(stderr, "<[M::%s] utg%.6u%c -> utg%.6u%c\n", __func__, id+1, "lc"[udb->ug->u.a[id].circ], 
				// m->y_id+1, "lc"[udb->ug->u.a[m->y_id].circ]);
				continue;
			}
		}
		m->x_pos_strand = 0;
		if(rr != k) {
			t = ol->list[k];
            ol->list[k] = ol->list[rr];
            ol->list[rr] = t;
		}
		// fprintf(stderr, ">>>utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", 
		// 		ol->list[rr].x_id+1, "lc"[udb->ug->u.a[ol->list[rr].x_id].circ], udb->ug->u.a[ol->list[rr].x_id].len, 
		// 		ol->list[rr].x_pos_s, ol->list[rr].x_pos_e+1, "+-"[ol->list[rr].y_pos_strand],
		// 		ol->list[rr].y_id+1, "lc"[udb->ug->u.a[ol->list[rr].y_id].circ], udb->ug->u.a[ol->list[rr].y_id].len, 
		// 		ol->list[rr].y_pos_s, ol->list[rr].y_pos_e+1);
		rr++;
	}
	// if(id == 1576) {
		// fprintf(stderr, "[M::%s] utg%.6ul, ol->length0::%lu, ol->length::%lu\n", __func__, id+1, ol->length, rr);
	// }
	ol->length = rr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, 0x4(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
movq 0x10(%rsi), %rax
movq %rsi, 0x28(%rsp)
movq 0x28(%rsi), %rcx
movl %edi, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
imulq $0x30, %rcx, %rcx
andq $0x0, (%r15)
leaq (%rax,%rcx), %rdx
movq %rdx, 0x18(%rsp)
leaq (%rax,%rcx), %r12
addq $0x28, %r12
movq %rsi, %rax
xorl %ebp, %ebp
movq %rsi, 0x8(%rsp)
cmpq %rbp, %rax
je 0x178b3e
movzbl (%r12), %r14d
testb $-0x42, %r14b
jne 0x178b35
movl -0x14(%r12), %r13d
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x178b13
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
shrl $0x6, %r14d
shlq $0x21, %r13
shlq $0x20, %r14
orq %rbp, %r13
orq %r14, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %r13, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
incq %rbp
addq $0x30, %r12
jmp 0x178acc
cmpq $0x0, (%r15)
movl 0x4(%rsp), %edi
je 0x178f18
pushq $0xc
popq %r12
xorl %ebp, %ebp
cmpq 0x10(%rbx), %rbp
jae 0x178bdf
movq (%rbx), %rcx
testl %edi, %edi
je 0x178b6a
cmpl $0x0, (%rcx,%r12)
je 0x178bd0
movl 0x4(%rcx,%r12), %r13d
movl 0x10(%rcx,%r12), %r14d
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0x178bad
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %edi
movq %rax, 0x10(%r15)
movq (%r15), %rdx
movq (%rbx), %rcx
shlq $0x21, %r13
shlq $0x20, %r14
orq %r13, %r14
orq %rbp, %r14
btsq $0x1f, %r14
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movq %r14, (%rax,%rdx,8)
andl $0x0, (%rcx,%r12)
incq %rbp
addq $0xa8, %r12
jmp 0x178b52
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq (%r15), %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
movq 0x18(%rsp), %r14
cmpq %rax, %rcx
ja 0x178c6d
je 0x178c1a
movq 0x10(%r15), %rsi
movq (%rsi,%rcx,8), %rdi
xorq (%rsi,%rdx,8), %rdi
shrq $0x20, %rdi
jne 0x178c1a
movq %rdx, %rdi
jmp 0x178c65
movq %rcx, %rsi
subq %rdx, %rsi
movq %rcx, %rdi
cmpq $0x2, %rsi
jb 0x178c65
movq 0x10(%r15), %rsi
testb $-0x80, 0x3(%rsi,%rdx,8)
movq %rcx, %rdi
jne 0x178c65
cmpq %rcx, %rdx
jae 0x178c62
movq (%rsi,%rdx,8), %rdi
testl %edi, %edi
jns 0x178c5d
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq (%rbx), %r8
imulq $0xa8, %rdi, %rdi
movl $0x1, 0xc(%r8,%rdi)
incq %rdx
jmp 0x178c37
movq %rcx, %rdi
incq %rcx
movq %rdi, %rdx
jmp 0x178bfc
andq $0x0, (%r15)
imulq $0x30, 0x8(%rsp), %r12
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x178cda
testb $-0x42, 0x28(%r14,%r13)
jne 0x178cd4
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x178cb9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movl (%r14,%r13), %edx
movl 0x4(%r14,%r13), %esi
shlq $0x20, %rdx
orq %rdx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rsi, (%rax,%rcx,8)
addq $0x30, %r13
jmp 0x178c7a
movq (%r15), %rax
cmpq $0x2, %rax
jb 0x178d48
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
xorl %eax, %eax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
xorl %edx, %edx
cmpq (%r15), %rdx
jae 0x178d45
movq 0x10(%r15), %rsi
movq (%rsi,%rdx,8), %r8
testq %rax, %rax
je 0x178d21
movq %r8, %r9
shrq $0x20, %r9
movq -0x8(%rsi,%rax,8), %rdi
cmpl %edi, %r9d
jbe 0x178d2d
movq %r8, (%rsi,%rax,8)
incq %rax
incq %rdx
jmp 0x178cfe
movl %r8d, %r8d
movl %edi, %r9d
cmpl %r9d, %r8d
jbe 0x178d28
andq %rcx, %rdi
orq %r8, %rdi
movq %rdi, -0x8(%rsi,%rax,8)
jmp 0x178d28
movq %rax, (%r15)
xorl %r12d, %r12d
movq 0x6a51d(%rip), %xmm3    # 0x1e3270
movapd 0x6a525(%rip), %xmm4    # 0x1e3280
xorl %r13d, %r13d
cmpq 0x10(%rbx), %r13
jae 0x178f14
movq (%rbx), %rbp
imulq $0xa8, %r13, %rax
leaq (%rax,%rbp), %r14
cmpl $0x0, 0xc(%rbp,%rax)
je 0x178df7
andl $0x0, 0xc(%r14)
cmpq %r13, %r12
je 0x178def
movl $0xa8, %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x2160
movq %r13, 0x8(%rsp)
movq %rbx, %r13
movq %r12, %rbx
imulq $0xa8, %r12, %r12
addq %r12, %rbp
movl $0xa8, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2160
addq (%r13), %r12
movl $0xa8, %edx
movq %r12, %rdi
movq %rbx, %r12
movq %r13, %rbx
movq 0x8(%rsp), %r13
leaq 0x30(%rsp), %rsi
callq 0x2160
movapd 0x6a499(%rip), %xmm4    # 0x1e3280
movq 0x6a481(%rip), %xmm3    # 0x1e3270
incq %r12
jmp 0x178f0c
movl 0x4(%r14), %eax
movl 0x8(%r14), %ecx
incl %ecx
movq (%r15), %rdx
movq %rcx, %rsi
subq %rax, %rsi
movq %rsi, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x10(%rsp), %xmm0
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0x178e8c
movq 0x10(%r15), %r8
movq (%r8,%rsi,8), %r8
movl %r8d, %r9d
cmpq %rcx, %r9
jb 0x178e45
movq %rcx, %r9
shrq $0x20, %r8
cmpq %rax, %r8
cmovbeq %rax, %r8
subq %r8, %r9
movl $0x0, %r8d
cmovaeq %r9, %r8
addq %r8, %rdi
incq %rsi
movq %rdi, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
testq %rdi, %rdi
je 0x178e2d
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x178e2d
jmp 0x178f0c
movl 0x10(%r14), %ecx
cmpl $0x0, 0x1c(%r14)
je 0x178ebf
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rcx, %rdx
movl (%rax,%rdx), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movl 0x18(%r14), %edx
notl %edx
addl %eax, %edx
subl 0x14(%r14), %eax
jmp 0x178ec9
movl 0x14(%r14), %edx
movl 0x18(%r14), %eax
incl %eax
movq 0x28(%rsp), %r8
movq 0x28(%r8), %rsi
movq (%rsi,%rcx,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
imulq $0x30, %rcx, %rdi
addq 0x10(%r8), %rdi
movl %edx, %edx
movl %eax, %ecx
movsd 0x10(%rsp), %xmm0
callq 0x1789dd
testq %rax, %rax
movq 0x6a372(%rip), %xmm3    # 0x1e3270
movapd 0x6a37a(%rip), %xmm4    # 0x1e3280
je 0x178d7d
incq %r13
jmp 0x178d5e
movq %r12, 0x10(%rbx)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq