// File: /workspace/github_cpp_makefile_star2_O3/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, 0x14(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r8, 0x28(%rsp)
movq %rdx, %r15
movq 0x10(%rsi), %rax
movq %rsi, 0x30(%rsp)
movq 0x28(%rsi), %rsi
movl %edi, %edx
movq (%rsi,%rdx,8), %rdx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, (%r15)
andq %rdx, %r12
je 0x1c43e6
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
leaq (%rax,%rdx), %rsi
movq %rsi, 0x20(%rsp)
leaq (%rdx,%rax), %rbx
addq $0x28, %rbx
xorl %r13d, %r13d
movq %rcx, 0x8(%rsp)
movzbl (%rbx), %r14d
testb $-0x42, %r14b
jne 0x1c3f5c
movl -0x14(%rbx), %ebp
movq (%r15), %rdi
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdi
jne 0x1c3f3f
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%r15)
movq (%r15), %rdi
shrl $0x6, %r14d
shlq $0x21, %rbp
shlq $0x20, %r14
orq %r13, %rbp
orq %r14, %rbp
leaq 0x1(%rdi), %rdx
movq %rdx, (%r15)
movq %rbp, (%rax,%rdi,8)
incq %r13
addq $0x30, %rbx
cmpq %r13, %r12
jne 0x1c3ef9
movq (%r15), %rdx
testq %rdx, %rdx
je 0x1c43e6
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x1c4025
movq (%rcx), %rdi
movl $0xc, %ebx
xorl %r13d, %r13d
movl 0x14(%rsp), %r8d
testl %r8d, %r8d
je 0x1c3f9c
cmpl $0x0, (%rdi,%rbx)
je 0x1c400f
movl 0x4(%rdi,%rbx), %ebp
movl 0x10(%rdi,%rbx), %r14d
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0x1c3fe6
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x14(%rsp), %r8d
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%r15)
movq (%r15), %rdx
movq (%rcx), %rdi
shlq $0x21, %rbp
shlq $0x20, %r14
orq %rbp, %r14
orq %r13, %r14
btsq $0x1f, %r14
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movq %r14, (%rax,%rdx,8)
movl $0x0, (%rdi,%rbx)
movq 0x10(%rcx), %rax
incq %r13
addq $0xa8, %rbx
cmpq %rax, %r13
jb 0x1c3f91
movq (%r15), %rdx
movq 0x10(%r15), %rdi
leaq (%rdi,%rdx,8), %rsi
callq 0x19dd99
movq 0x8(%rsp), %r11
movq (%r15), %rax
testq %rax, %rax
movq 0x20(%rsp), %r14
je 0x1c40e8
leaq 0x1(%rax), %rdx
cmpq $0x3, %rdx
movl $0x2, %ecx
cmovaeq %rdx, %rcx
movl $0x1, %edx
xorl %esi, %esi
cmpq %rax, %rdx
je 0x1c407c
movq 0x10(%r15), %rdi
movq (%rdi,%rdx,8), %r8
xorq (%rdi,%rsi,8), %r8
shrq $0x20, %r8
jne 0x1c407c
movq %rsi, %r8
jmp 0x1c40d9
movq %rdx, %rdi
subq %rsi, %rdi
movq %rdx, %r8
cmpq $0x2, %rdi
jb 0x1c40d9
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 0x1c40d9
movq (%rdi,%rsi,8), %r8
testl %r8d, %r8d
jns 0x1c40ce
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq (%r11), %r9
imulq $0xa8, %r8, %r8
movl $0x1, 0xc(%r9,%r8)
incq %rsi
cmpq %rsi, %rdx
jne 0x1c40ab
movq %rdx, %r8
incq %rdx
movq %r8, %rsi
cmpq %rcx, %rdx
jne 0x1c4060
movq $0x0, (%r15)
testq %r12, %r12
je 0x1c41e9
shlq $0x4, %r12
leaq (%r12,%r12,2), %rbx
xorl %r12d, %r12d
movl $0x2, %r13d
testb $-0x42, 0x28(%r14,%r12)
jne 0x1c4160
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x1c4145
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movl (%r14,%r12), %edx
movl 0x4(%r14,%r12), %esi
shlq $0x20, %rdx
orq %rdx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rsi, (%rax,%rcx,8)
addq $0x30, %r12
cmpq %r12, %rbx
jne 0x1c4109
movq (%r15), %rax
cmpq $0x2, %rax
jb 0x1c41e9
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
cmpq $0x0, (%r15)
je 0x1c41df
movq 0x10(%r15), %rcx
xorl %edx, %edx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
xorl %eax, %eax
movq 0x8(%rsp), %r11
movq (%rcx,%rdx,8), %r8
testq %rax, %rax
je 0x1c41b6
movq %r8, %r9
shrq $0x20, %r9
movq -0x8(%rcx,%rax,8), %rdi
cmpl %edi, %r9d
jbe 0x1c41c7
movq %r8, (%rcx,%rax,8)
incq %rax
incq %rdx
cmpq (%r15), %rdx
jb 0x1c419c
jmp 0x1c41e6
movl %r8d, %r8d
movl %edi, %r9d
cmpl %r9d, %r8d
jbe 0x1c41bd
andq %rsi, %rdi
orq %r8, %rdi
movq %rdi, -0x8(%rcx,%rax,8)
jmp 0x1c41bd
xorl %eax, %eax
movq 0x8(%rsp), %r11
movq %rax, (%r15)
movq 0x10(%r11), %r12
testq %r12, %r12
je 0x1c43e0
xorl %r13d, %r13d
movq 0x6d07f(%rip), %xmm3    # 0x231280
movapd 0x6d087(%rip), %xmm4    # 0x231290
xorl %ebx, %ebx
movq %r15, 0x38(%rsp)
movq (%r11), %rbp
imulq $0xa8, %r13, %rax
leaq (%rax,%rbp), %r14
cmpl $0x0, 0xc(%rbp,%rax)
je 0x1c42b5
movl $0x0, 0xc(%r14)
cmpq %r13, %rbx
je 0x1c42a1
movl $0xa8, %edx
movq %rbx, %r15
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2160
imulq $0xa8, %r15, %r12
addq %r12, %rbp
movl $0xa8, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r12
movl $0xa8, %edx
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rbx
movq 0x38(%rsp), %r15
callq 0x2160
movapd 0x6d000(%rip), %xmm4    # 0x231290
movq 0x6cfe8(%rip), %xmm3    # 0x231280
movq 0x8(%rsp), %r11
movq 0x10(%r11), %r12
incq %rbx
incq %r13
cmpq %r12, %r13
jb 0x1c4210
jmp 0x1c43e2
movq (%r15), %rax
testq %rax, %rax
je 0x1c4352
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 0x18(%rsp), %xmm0
xorl %r8d, %r8d
xorl %edi, %edi
movq %r8, %r9
movq (%rsi,%rdi,8), %r8
movl %r8d, %r10d
shrq $0x20, %r8
cmpq %rdx, %r10
cmovaeq %rdx, %r10
cmpq %rcx, %r8
cmovbeq %rcx, %r8
subq %r8, %r10
movl $0x0, %r8d
cmovaeq %r10, %r8
addq %r9, %r8
movq %r8, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
je 0x1c434a
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
ja 0x1c42a4
incq %rdi
cmpq %rdi, %rax
jne 0x1c42f9
movl 0x10(%r14), %edx
cmpl $0x0, 0x1c(%r14)
je 0x1c4385
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (%rdx,%rdx,4), %rcx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl 0x18(%r14), %ecx
notl %ecx
addl %eax, %ecx
subl 0x14(%r14), %eax
jmp 0x1c438f
movl 0x14(%r14), %ecx
movl 0x18(%r14), %eax
incl %eax
movq 0x30(%rsp), %r8
movq 0x28(%r8), %rsi
movq (%rsi,%rdx,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdi
shlq $0x4, %rdi
addq 0x10(%r8), %rdi
movl %ecx, %edx
movl %eax, %ecx
movsd 0x18(%rsp), %xmm0
callq 0x1c3def
testq %rax, %rax
movq 0x8(%rsp), %r11
movq 0x6ceb3(%rip), %xmm3    # 0x231280
movapd 0x6cebb(%rip), %xmm4    # 0x231290
jne 0x1c42a4
jmp 0x1c4229
xorl %ebx, %ebx
movq %rbx, 0x10(%r11)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq