// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: filter_by_reliable_ovlp_mmhap_adv(unsigned int, kv_u_trans_t*, st_mt_t*, overlap_region_alloc*, ul_idx_t const*, double, unsigned long, unsigned long, ug_rid_cov_t*, unsigned long*)
void filter_by_reliable_ovlp_mmhap_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, ug_rid_cov_t *ccov, uint64_t *occ1)
{
	(*occ1) = 0;
	u_trans_t *a; uint64_t n, k, l, z, rr, r1, *fc; 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(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; fc = NULL;
	if(dedup_by_reliable_ovlp) {
		for (k = 0; k < n; k++) {
			if(a[k].del) continue;
			if(a[k].f == RC_0 || a[k].f == RC_1) break;
		}
		if(k < n) fc = gen_reliable_cov_arr(id, idx, ccov, sp);
	}

	for (k = rr = r1 = 0; k < ol->length; k++) {
		m = &(ol->list[k]);
		if((dedup_by_reliable_ovlp) && (m->x_pos_strand == 0) && (fc)) {
			if(is_above_cov(id, m, fc, ccov, sec_rate)) continue;
		} 
		
		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++;
		}
		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 $0xf8, %rsp
movsd %xmm0, 0x48(%rsp)
movq %rcx, %r13
movq %rdx, %r15
movq 0x140(%rsp), %rax
movq $0x0, (%rax)
movq 0x10(%rsi), %rax
movq %rax, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x28(%rsi), %rax
movl %edi, 0x24(%rsp)
movl %edi, %ecx
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rax
movl %eax, %ebx
shrq $0x20, %rax
movq %rax, 0x28(%rsp)
testq %r9, %r9
movq %r13, 0x8(%rsp)
je 0x1bcc56
movq 0x10(%r13), %rax
addq %rbx, %rax
cmpq %rax, 0x8(%r15)
jae 0x1bc9ba
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(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq $0x0, (%r15)
movq %rbx, 0x10(%rsp)
testq %rbx, %rbx
je 0x1bca65
movq 0x10(%rsp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbx
movq 0x28(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x30(%rsp), %rcx
leaq (%rax,%rcx), %r14
addq $0x28, %r14
xorl %r12d, %r12d
movzbl (%r14,%r12), %ebp
testb $-0x42, %bpl
jne 0x1bca5c
movl -0x14(%r14,%r12), %r13d
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1bca37
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)
shrl $0x6, %ebp
shlq $0x21, %r13
shlq $0x20, %rbp
orq %r13, %rbp
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rbp, (%rcx,%rax,8)
movq 0x8(%rsp), %r13
addq $0x30, %r12
cmpq %r12, %rbx
jne 0x1bc9f9
cmpq $0x0, (%r15)
movq 0x10(%rsp), %rbx
je 0x1bcc56
cmpq $0x0, 0x10(%r13)
je 0x1bcaf8
movl $0x1c, %ebx
xorl %r14d, %r14d
movl $0x2, %r12d
movq (%r13), %rax
movl -0xc(%rax,%rbx), %ebp
movl (%rax,%rbx), %r13d
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1bcabe
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
shlq $0x21, %rbp
shlq $0x20, %r13
orq %rbp, %r13
orq %r14, %r13
btsq $0x1f, %r13
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r13, (%rcx,%rax,8)
incq %r14
addq $0xa8, %rbx
movq 0x8(%rsp), %r13
cmpq 0x10(%r13), %r14
jb 0x1bca89
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
movq (%r15), %rax
testq %rax, %rax
movq 0x10(%rsp), %rbx
je 0x1bcc56
leaq 0x1(%rax), %rdx
cmpq $0x3, %rdx
movl $0x2, %ecx
cmovaeq %rdx, %rcx
movl $0x1, %esi
xorl %edx, %edx
xorl %edi, %edi
cmpq %rax, %rsi
je 0x1bcb4a
movq 0x10(%r15), %r8
movq (%r8,%rsi,8), %r9
xorq (%r8,%rdi,8), %r9
shrq $0x20, %r9
je 0x1bcba6
movq %rsi, %r8
subq %rdi, %r8
cmpq $0x2, %r8
jb 0x1bcba3
movq 0x10(%r15), %r8
testb $-0x80, 0x3(%r8,%rdi,8)
sete %r9b
cmpq %rdi, %rsi
seta %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x1bcba3
movq (%r8,%rdi,8), %r9
testl %r9d, %r9d
jns 0x1bcb9b
movq (%r13), %r10
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
imulq $0xa8, %r9, %r9
movl $0xffffffff, 0x10(%r10,%r9) # imm = 0xFFFFFFFF
incq %rdx
incq %rdi
cmpq %rdi, %rsi
jne 0x1bcb74
movq %rsi, %rdi
incq %rsi
cmpq %rcx, %rsi
jne 0x1bcb33
testq %rdx, %rdx
je 0x1bcc56
cmpq $0x0, 0x10(%r13)
je 0x1bcc42
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq (%rax), %r14
cmpl $-0x1, 0x10(%r14,%rbx)
je 0x1bcc2b
cmpq %r13, %r12
je 0x1bcc28
imulq $0xa8, %r13, %rbp
addq %r14, %rbp
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq %rbx, %r14
movl $0xa8, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
leaq 0x50(%rsp), %rsi
callq 0x2160
incq %r13
incq %r12
addq $0xa8, %rbx
movq 0x8(%rsp), %rax
cmpq 0x10(%rax), %r12
jb 0x1bcbcf
jmp 0x1bcc4a
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq %r13, 0x10(%rax)
movq %rax, %r13
movq 0x10(%rsp), %rbx
movq $0x0, (%r15)
movq $0x0, 0x18(%rsp)
cmpq $0x0, 0x130(%rsp)
je 0x1bccd1
testq %rbx, %rbx
je 0x1bccd1
shlq $0x4, %rbx
leaq (%rbx,%rbx,2), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq 0x30(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
movq $0x0, 0x18(%rsp)
xorl %edx, %edx
testb $-0x42, (%rcx,%rdx)
je 0x1bccb3
addq $0x30, %rdx
cmpq %rdx, %rax
jne 0x1bcca2
jmp 0x1bccd1
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq %r15, %rcx
callq 0x1bc736
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x10(%r13)
je 0x1bce2a
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq $0x0, 0x130(%rsp)
je 0x1bcd32
cmpq $0x0, 0x18(%rsp)
je 0x1bcd32
movq (%r13), %rsi
cmpl $0x0, 0xc(%rsi,%rbx)
jne 0x1bcd32
addq %rbx, %rsi
movq 0x40(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rcx, %r14
movq 0x138(%rsp), %rcx
movsd 0x48(%rsp), %xmm0
callq 0x1bc83d
movq %r14, %rcx
testq %rax, %rax
jne 0x1bce14
cmpq %r15, %rbp
je 0x1bcd94
movq (%r13), %r14
leaq (%r14,%rbx), %r12
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %rcx, 0x10(%rsp)
callq 0x2160
imulq $0xa8, %r15, %r13
addq %r13, %r14
movl $0xa8, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r13
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %r13
leaq 0x50(%rsp), %rsi
callq 0x2160
movq 0x10(%rsp), %rcx
movq (%r13), %r14
imulq $0xa8, %r15, %r12
cmpl $0x0, 0xc(%r14,%r12)
je 0x1bce11
leaq (%r14,%r12), %rax
addq $0xc, %rax
movl $0x0, (%rax)
cmpq %r15, %rcx
je 0x1bce0e
imulq $0xa8, %rcx, %r13
addq %r14, %r13
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
movq %rcx, 0x10(%rsp)
callq 0x2160
addq %r12, %r14
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %r13
movq %r14, %rsi
callq 0x2160
addq (%r13), %r12
movl $0xa8, %edx
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
callq 0x2160
movq 0x10(%rsp), %rcx
incq %rcx
incq %r15
incq %rbp
addq $0xa8, %rbx
cmpq 0x10(%r13), %rbp
jb 0x1bcce5
jmp 0x1bce2f
xorl %r15d, %r15d
xorl %ecx, %ecx
movq %r15, 0x10(%r13)
movq 0x140(%rsp), %rax
movq %rcx, (%rax)
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq