// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: filter_by_reliable_ovlp_adv(unsigned int, kv_u_trans_t*, st_mt_t*, overlap_region_alloc*, ul_idx_t const*, double, unsigned long, unsigned long, unsigned long*)
void filter_by_reliable_ovlp_adv(uint32_t id, kv_u_trans_t *idx, st_mt_t *sp, overlap_region_alloc* ol, const ul_idx_t *udb, double sec_rate, uint64_t avoid_dup_aln, 
uint64_t dedup_by_reliable_ovlp, uint64_t *occ1)
{
	(*occ1) = 0;
	u_trans_t *a; uint64_t n, k, l, s, e, s0, e0, z, ov, rr, r1, spn; overlap_region *m, t;
	a = u_trans_a(*idx, id); n = u_trans_n(*idx, id);
	if(avoid_dup_aln) {
		kv_resize(uint64_t, *sp, (ol->length)+n);
		for (k = sp->n = 0; k < n; k++) {
			if(a[k].del) continue;
			// if(id == 160) {
			// 	fprintf(stderr, "[rid::%u]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tnw::%f\tf::%u\n", id, 
			// 		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, a[k].nw, a[k].f);
			// }
			if(a[k].f == RC_0 || a[k].f == RC_1) {
				z = a[k].tn; z <<= 1; z |= a[k].rev; z <<= 32; 
				kv_push(uint64_t, *sp, z);
			}
		}
		if(sp->n > 0) {
			for (k = 0; k < ol->length; k++) {
				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);
			}

			radix_sort_gfa64(sp->a, sp->a + sp->n); 
			for (k = 1, l = 0, rr = 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)))) {///overlap within bck
						for (z = l; z < k; z++) {
							if(sp->a[z]&((uint64_t)0x80000000)) {
								ol->list[(uint32_t)(sp->a[z]-((uint64_t)0x80000000))].y_id = ((uint32_t)-1);
								rr++;
							}
						}
					}
					l = k;
				}
			}

			if(rr > 0) {
				for (k = rr = 0; k < ol->length; k++) {
					if(ol->list[k].y_id == ((uint32_t)-1)) continue;
					if(rr != k) {
						t = ol->list[rr];
						ol->list[rr] = ol->list[k];
						ol->list[k] = t;
					}
					rr++;
				}
				ol->length = rr;
			}
		}
	}

	sp->n = 0;
	if(dedup_by_reliable_ovlp) {
		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 = r1 = 0, spn = sp->n; k < ol->length; k++) {
		m = &(ol->list[k]);
		// if(id == 160) {
		// 	fprintf(stderr, "[M::%s] utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\txstr::%u\n", __func__, 
		// 		ol->list[k].x_id+1, "lc"[udb->ug->u.a[ol->list[k].x_id].circ], udb->ug->u.a[ol->list[k].x_id].len, 
		// 		ol->list[k].x_pos_s, ol->list[k].x_pos_e+1, "+-"[ol->list[k].y_pos_strand],
		// 		ol->list[k].y_id+1, "lc"[udb->ug->u.a[ol->list[k].y_id].circ], udb->ug->u.a[ol->list[k].y_id].len, 
		// 		ol->list[k].y_pos_s, ol->list[k].y_pos_e+1, ol->list[k].x_pos_strand);
		// }
		if((dedup_by_reliable_ovlp) && (m->x_pos_strand == 0)) {
			s = m->x_pos_s; e = m->x_pos_e + 1; l = 0; 
			for (z = 0; z < spn; 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;
			}
			///filter by the overlaps of id/m->x_id
			if(z < spn) continue;
			///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)) {
				continue;
			}
		} 
		// else {
		// 	if(id == 7) {
		// 		fprintf(stderr, "[M::%s] utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
		// 		ol->list[k].x_id+1, "lc"[udb->ug->u.a[ol->list[k].x_id].circ], udb->ug->u.a[ol->list[k].x_id].len, 
		// 		ol->list[k].x_pos_s, ol->list[k].x_pos_e+1, "+-"[ol->list[k].y_pos_strand],
		// 		ol->list[k].y_id+1, "lc"[udb->ug->u.a[ol->list[k].y_id].circ], udb->ug->u.a[ol->list[k].y_id].len, 
		// 		ol->list[k].y_pos_s, ol->list[k].y_pos_e+1);
		// 	}
		// }
		if(rr != k) {
			t = ol->list[k];
            ol->list[k] = ol->list[rr];
            ol->list[rr] = t;
		}
		if(ol->list[rr].x_pos_strand) {
			ol->list[rr].x_pos_strand = 0;
			if(r1 != rr) {
				t = ol->list[r1];
            	ol->list[r1] = ol->list[rr];
            	ol->list[rr] = t;
			}
			r1++;
		}
		// 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; (*occ1) = r1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm0, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq 0x128(%rsp), %rax
andq $0x0, (%rax)
movq 0x10(%rsi), %r12
movq %rsi, 0x38(%rsp)
movq 0x28(%rsi), %rax
movl %edi, %esi
movq (%rax,%rsi,8), %rax
movl %eax, %ebx
shrq $0x20, %rax
imulq $0x30, %rax, %rax
movq %rax, 0x18(%rsp)
testq %r9, %r9
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
je 0x17922c
movq 0x10(%rcx), %rax
addq %rbx, %rax
cmpq %rax, 0x8(%rdx)
jae 0x178fef
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rdx)
movq 0x10(%rdx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rdx)
andq $0x0, (%rdx)
movq %rbx, 0x20(%rsp)
imulq $0x30, %rbx, %rbx
movq 0x18(%rsp), %rax
leaq (%rax,%r12), %r14
addq $0x28, %r14
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x17907d
movzbl (%r14,%r15), %r13d
testb $-0x42, %r13b
jne 0x179077
movl -0x14(%r14,%r15), %ebp
movq (%rdx), %rdi
movq 0x10(%rdx), %rax
cmpq 0x8(%rdx), %rdi
jne 0x17905d
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rdx)
movq (%rdx), %rdi
shrl $0x6, %r13d
shlq $0x21, %rbp
shlq $0x20, %r13
orq %rbp, %r13
leaq 0x1(%rdi), %rsi
movq %rsi, (%rdx)
movq %r13, (%rax,%rdi,8)
addq $0x30, %r15
jmp 0x17900c
cmpq $0x0, (%rdx)
movq 0x20(%rsp), %rbx
je 0x17922c
pushq $0x1c
popq %rbx
xorl %r14d, %r14d
cmpq 0x10(%rcx), %r14
jae 0x17910a
movq (%rcx), %rax
movl -0xc(%rax,%rbx), %r13d
movl (%rax,%rbx), %r15d
movq (%rdx), %rdi
movq 0x10(%rdx), %rax
cmpq 0x8(%rdx), %rdi
jne 0x1790e0
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rdx)
movq (%rdx), %rdi
shlq $0x21, %r13
shlq $0x20, %r15
orq %r13, %r15
orq %r14, %r15
btsq $0x1f, %r15
leaq 0x1(%rdi), %rsi
movq %rsi, (%rdx)
movq %r15, (%rax,%rdi,8)
incq %r14
addq $0xa8, %rbx
jmp 0x179092
movq (%rdx), %rax
movq 0x10(%rdx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rdx), %rax
pushq $0x1
popq %r11
xorl %r10d, %r10d
xorl %esi, %esi
movq 0x20(%rsp), %rbx
cmpq %rax, %r11
ja 0x17919a
je 0x17914e
movq 0x10(%rdx), %rdi
movq (%rdi,%r11,8), %r8
xorq (%rdi,%rsi,8), %r8
shrq $0x20, %r8
je 0x179195
movq %r11, %rdi
subq %rsi, %rdi
cmpq $0x2, %rdi
jb 0x179192
movq 0x10(%rdx), %rdi
testb $-0x80, 0x3(%rdi,%rsi,8)
jne 0x179192
cmpq %r11, %rsi
jae 0x179192
movq (%rdi,%rsi,8), %r8
testl %r8d, %r8d
jns 0x17918d
movq (%rcx), %r9
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
imulq $0xa8, %r8, %r8
orl $-0x1, 0x10(%r9,%r8)
incq %r10
incq %rsi
jmp 0x179165
movq %r11, %rsi
incq %r11
jmp 0x179135
testq %r10, %r10
je 0x17922c
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq 0x10(%rcx), %r14
jae 0x179223
movq (%rcx), %r13
cmpl $-0x1, 0x10(%r13,%rbx)
je 0x179217
cmpq %r15, %r14
je 0x179214
imulq $0xa8, %r15, %rbp
addq %r13, %rbp
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq %rbx, %r13
movl $0xa8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
incq %r15
incq %r14
addq $0xa8, %rbx
jmp 0x1791ab
movq %r15, 0x10(%rcx)
movq 0x20(%rsp), %rbx
andq $0x0, (%rdx)
cmpq $0x0, 0x120(%rsp)
je 0x17932b
addq 0x18(%rsp), %r12
imulq $0x30, %rbx, %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x1792b5
testb $-0x42, 0x28(%r12,%r14)
jne 0x1792af
movq (%rdx), %rdi
movq 0x10(%rdx), %rax
cmpq 0x8(%rdx), %rdi
jne 0x179294
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rdx)
movq (%rdx), %rdi
movl (%r12,%r14), %r8d
movl 0x4(%r12,%r14), %esi
shlq $0x20, %r8
orq %r8, %rsi
leaq 0x1(%rdi), %r8
movq %r8, (%rdx)
movq %rsi, (%rax,%rdi,8)
addq $0x30, %r14
jmp 0x17924b
movq (%rdx), %r13
cmpq $0x2, %r13
jb 0x179333
movq 0x10(%rdx), %rdi
leaq (%rdi,%r13,8), %rsi
callq 0x156b0d
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
xorl %r13d, %r13d
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
xorl %r9d, %r9d
cmpq (%rdx), %r9
jae 0x179330
movq 0x10(%rdx), %r10
movq (%r10,%r9,8), %rdi
testq %r13, %r13
je 0x179308
movq %rdi, %r8
shrq $0x20, %r8
movq -0x8(%r10,%r13,8), %rsi
cmpl %esi, %r8d
jbe 0x179314
movq %rdi, (%r10,%r13,8)
incq %r13
incq %r9
jmp 0x1792e5
movl %edi, %edi
movl %esi, %r8d
cmpl %r8d, %edi
jbe 0x17930f
andq %rax, %rsi
orq %rdi, %rsi
movq %rsi, -0x8(%r10,%r13,8)
jmp 0x17930f
xorl %r13d, %r13d
jmp 0x179333
movq %r13, (%rdx)
xorl %ebx, %ebx
movq 0x69f33(%rip), %xmm3    # 0x1e3270
movapd 0x69f3b(%rip), %xmm4    # 0x1e3280
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r13, 0x18(%rsp)
cmpq 0x10(%rcx), %r15
jae 0x1795b1
movq (%rcx), %rbp
imulq $0xa8, %r15, %r14
addq %rbp, %r14
cmpq $0x0, 0x120(%rsp)
je 0x17937d
cmpl $0x0, 0xc(%r14)
je 0x17948f
cmpq %r15, %rbx
je 0x1793f0
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x2160
movq %r12, %r13
imulq $0xa8, %rbx, %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 %r13, %r12
movq 0x18(%rsp), %r13
leaq 0x40(%rsp), %rsi
callq 0x2160
movapd 0x69ea5(%rip), %xmm4    # 0x1e3280
movq 0x69e8d(%rip), %xmm3    # 0x1e3270
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rcx), %rbp
imulq $0xa8, %rbx, %r14
cmpl $0x0, 0xc(%rbp,%r14)
je 0x179484
leaq (%r14,%rbp), %rax
addq $0xc, %rax
andl $0x0, (%rax)
cmpq %rbx, %r12
je 0x179481
movq %r12, %r13
imulq $0xa8, %r12, %r12
addq %rbp, %r12
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
addq %r14, %rbp
movl $0xa8, %edx
movq %r12, %rdi
movq %r13, %r12
movq 0x18(%rsp), %r13
movq %rbp, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r14
movl $0xa8, %edx
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2160
movapd 0x69e11(%rip), %xmm4    # 0x1e3280
movq 0x69df9(%rip), %xmm3    # 0x1e3270
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
incq %r12
incq %rbx
incq %r15
jmp 0x179350
movl 0x4(%r14), %eax
movl 0x8(%r14), %r9d
incl %r9d
movq %r9, %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 0x28(%rsp), %xmm0
xorl %r10d, %r10d
xorl %esi, %esi
cmpq %r10, %r13
je 0x179522
movq 0x10(%rdx), %rdi
movq (%rdi,%r10,8), %rdi
movl %edi, %r8d
cmpq %r9, %r8
jb 0x1794dc
movq %r9, %r8
shrq $0x20, %rdi
cmpq %rax, %rdi
cmovbeq %rax, %rdi
subq %rdi, %r8
movl $0x0, %edi
cmovaeq %r8, %rdi
addq %rdi, %rsi
incq %r10
movq %rsi, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
testq %rsi, %rsi
je 0x1794c4
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x1794c4
jmp 0x179487
movl 0x10(%r14), %ecx
cmpl $0x0, 0x1c(%r14)
je 0x179555
movq 0x30(%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 0x17955f
movl 0x14(%r14), %edx
movl 0x18(%r14), %eax
incl %eax
movq 0x38(%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 0x28(%rsp), %xmm0
callq 0x1789dd
testq %rax, %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq 0x69cd2(%rip), %xmm3    # 0x1e3270
movapd 0x69cda(%rip), %xmm4    # 0x1e3280
jne 0x179487
jmp 0x17937d
movq %rbx, 0x10(%rcx)
movq 0x128(%rsp), %rax
movq %r12, (%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq