// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movl %r9d, %ebx
movsd %xmm0, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, (%rsp)
movq %rdx, %r15
movq 0x10(%rsi), %rax
movq %rax, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x28(%rsi), %rax
movl %edi, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq %rcx, 0x10(%rsp)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq $0x0, (%rdx)
andq %rax, %rsi
je 0x1ba17b
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $0x28, %r12
xorl %ebp, %ebp
movl %ebx, 0xc(%rsp)
movzbl (%r12), %r14d
testb $-0x42, %r14b
jne 0x1ba16f
movl -0x14(%r12), %r13d
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1ba14b
movq %rsi, %rbx
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rbx, %rsi
movl 0xc(%rsp), %ebx
movq %rax, 0x10(%r15)
shrl $0x6, %r14d
shlq $0x21, %r13
shlq $0x20, %r14
orq %rbp, %r13
orq %r14, %r13
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r13, (%rcx,%rax,8)
incq %rbp
addq $0x30, %r12
cmpq %rbp, %rsi
jne 0x1ba103
cmpq $0x0, (%r15)
je 0x1ba651
movq %rsi, 0x30(%rsp)
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1ba23d
xorl %r12d, %r12d
xorl %ebp, %ebp
testl %ebx, %ebx
je 0x1ba1b1
movq (%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0xc(%rax,%r12)
je 0x1ba225
movq (%rsp), %rax
movq (%rax), %rax
movl 0x10(%rax,%r12), %r13d
movl 0x1c(%rax,%r12), %r14d
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1ba1f0
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
shlq $0x21, %r13
shlq $0x20, %r14
orq %r13, %r14
orq %rbp, %r14
btsq $0x1f, %r14
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r14, (%rcx,%rax,8)
movq (%rsp), %rax
movq (%rax), %rax
movl $0x0, 0xc(%rax,%r12)
incq %rbp
addq $0xa8, %r12
movq (%rsp), %rax
cmpq 0x10(%rax), %rbp
jb 0x1ba19e
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
movq (%r15), %rax
testq %rax, %rax
movq 0x18(%rsp), %r14
je 0x1ba302
leaq 0x1(%rax), %rdx
cmpq $0x3, %rdx
movl $0x2, %ecx
cmovaeq %rdx, %rcx
movl $0x1, %edx
xorl %esi, %esi
cmpq %rax, %rdx
je 0x1ba292
movq 0x10(%r15), %rdi
movq (%rdi,%rdx,8), %r8
xorq (%rdi,%rsi,8), %r8
shrq $0x20, %r8
jne 0x1ba292
movq %rsi, %r8
jmp 0x1ba2f3
movq %rdx, %rdi
subq %rsi, %rdi
movq %rdx, %r8
cmpq $0x2, %rdi
jb 0x1ba2f3
movq 0x10(%r15), %rdi
testb $-0x80, 0x3(%rdi,%rsi,8)
sete %r8b
cmpq %rsi, %rdx
seta %r9b
andb %r8b, %r9b
movq %rdx, %r8
cmpb $0x1, %r9b
jne 0x1ba2f3
movq (%rdi,%rsi,8), %r8
testl %r8d, %r8d
jns 0x1ba2e8
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq (%rsp), %r9
movq (%r9), %r9
imulq $0xa8, %r8, %r8
movl $0x1, 0xc(%r9,%r8)
incq %rsi
cmpq %rsi, %rdx
jne 0x1ba2c1
movq %rdx, %r8
incq %rdx
movq %r8, %rsi
cmpq %rcx, %rdx
jne 0x1ba276
movq $0x0, (%r15)
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x1ba384
addq 0x10(%rsp), %r14
shlq $0x4, %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
movl $0x2, %ebp
testb $-0x42, 0x28(%r14,%r13)
jne 0x1ba37b
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1ba359
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movl (%r14,%r13), %eax
movl 0x4(%r14,%r13), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq (%r15), %rax
movq 0x10(%r15), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%r15)
movq %rcx, (%rdx,%rax,8)
addq $0x30, %r13
cmpq %r13, %r12
jne 0x1ba328
movq (%r15), %rax
cmpq $0x2, %rax
jb 0x1ba3fa
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
cmpq $0x0, (%r15)
je 0x1ba3f5
movq 0x10(%r15), %rcx
xorl %edx, %edx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
xorl %eax, %eax
movq (%rcx,%rdx,8), %r8
testq %rax, %rax
je 0x1ba3cc
movq %r8, %r9
shrq $0x20, %r9
movq -0x8(%rcx,%rax,8), %rdi
cmpl %edi, %r9d
jbe 0x1ba3dd
movq %r8, (%rcx,%rax,8)
incq %rax
incq %rdx
cmpq (%r15), %rdx
jb 0x1ba3b2
jmp 0x1ba3f7
movl %r8d, %r8d
movl %edi, %r9d
cmpl %r9d, %r8d
jbe 0x1ba3d3
andq %rsi, %rdi
orq %r8, %rdi
movq %rdi, -0x8(%rcx,%rax,8)
jmp 0x1ba3d3
xorl %eax, %eax
movq %rax, (%r15)
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1ba647
xorl %r13d, %r13d
movq 0x6ae2c(%rip), %xmm3    # 0x225240
movapd 0x6ae34(%rip), %xmm4    # 0x225250
xorl %ebx, %ebx
movq (%rsp), %rax
movq (%rax), %rbp
imulq $0xa8, %r13, %rax
leaq (%rax,%rbp), %r14
cmpl $0x0, 0xc(%rbp,%rax)
je 0x1ba4b2
movl $0x0, 0xc(%r14)
cmpq %r13, %rbx
je 0x1ba499
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x2160
imulq $0xa8, %rbx, %r12
addq %r12, %rbp
movl $0xa8, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2160
movq (%rsp), %rax
addq (%rax), %r12
movl $0xa8, %edx
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2160
movapd 0x6adbf(%rip), %xmm4    # 0x225250
movq 0x6ada7(%rip), %xmm3    # 0x225240
incq %rbx
incq %r13
movq (%rsp), %rax
cmpq 0x10(%rax), %r13
jb 0x1ba41e
jmp 0x1ba649
movq (%r15), %rax
testq %rax, %rax
setne %r8b
je 0x1ba599
movl 0x4(%r14), %ecx
movl 0x8(%r14), %edx
incl %edx
movq 0x10(%r15), %rsi
movq %rdx, %rdi
subq %rcx, %rdi
movq %rdi, %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 0x28(%rsp), %xmm0
movq (%rsi), %rdi
movl %edi, %r9d
movq %rdi, %r10
shrq $0x20, %r10
cmpq %rdx, %r9
cmovaeq %rdx, %r9
cmpq %rcx, %r10
cmovbeq %rcx, %r10
movl $0x0, %edi
subq %r10, %r9
cmovaeq %r9, %rdi
jbe 0x1ba52c
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
ucomisd %xmm0, %xmm1
ja 0x1ba590
leaq -0x1(%rax), %r8
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x1ba599
movq 0x8(%rsi,%r9,8), %r10
movl %r10d, %r11d
shrq $0x20, %r10
cmpq %rdx, %r11
cmovaeq %rdx, %r11
cmpq %rcx, %r10
cmovbeq %rcx, %r10
subq %r10, %r11
movl $0x0, %r10d
cmovaeq %r11, %r10
addq %r10, %rdi
incq %r9
movq %rdi, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
testq %rdi, %rdi
je 0x1ba533
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x1ba533
cmpq %rax, %r9
setb %r8b
testb %r8b, %r8b
jne 0x1ba49c
movl 0x10(%r14), %ecx
movl 0x1c(%r14), %edx
testl %edx, %edx
je 0x1ba5c9
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (%rcx,%rcx,4), %rsi
movl (%rax,%rsi,8), %esi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %esi
movl 0x18(%r14), %eax
notl %eax
addl %esi, %eax
jmp 0x1ba5cd
movl 0x14(%r14), %eax
testl %edx, %edx
je 0x1ba5f3
movq 0x20(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rsi
movl (%rdx,%rsi,8), %r8d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r8d
subl 0x14(%r14), %r8d
jmp 0x1ba5fa
movl 0x18(%r14), %r8d
incl %r8d
movq 0x38(%rsp), %r9
movq 0x28(%r9), %rdx
movq (%rdx,%rcx,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq 0x10(%r9), %rdi
movl %eax, %edx
movl %r8d, %ecx
movsd 0x28(%rsp), %xmm0
callq 0x1ba00d
testq %rax, %rax
movq 0x6ac0c(%rip), %xmm3    # 0x225240
movapd 0x6ac14(%rip), %xmm4    # 0x225250
jne 0x1ba49c
jmp 0x1ba437
xorl %ebx, %ebx
movq (%rsp), %rax
movq %rbx, 0x10(%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq