// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movsd %xmm0, 0x38(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq 0x130(%rsp), %rax
movq 0x120(%rsp), %rbx
movq $0x0, (%rax)
movq 0x10(%rsi), %r14
movq %rsi, 0x28(%rsp)
movq 0x28(%rsi), %rax
movl %edi, 0x24(%rsp)
movl %edi, %ecx
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %rax
movl %eax, %ebp
shrq $0x20, %rax
movq %rax, 0x18(%rsp)
testq %r9, %r9
movq %r15, 0x8(%rsp)
je 0x1c6a99
movq 0x10(%r15), %rax
addq %rbp, %rax
cmpq %rax, 0x8(%r12)
jae 0x1c67b0
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(%r12)
movq 0x10(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r12)
movq $0x0, (%r12)
testq %rbp, %rbp
je 0x1c6a69
movq %rbp, 0x10(%rsp)
movq %rbp, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbx
movq 0x18(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq %r14, (%rsp)
addq %rax, %r14
addq $0x28, %r14
xorl %r15d, %r15d
movzbl (%r14,%r15), %ebp
testb $-0x42, %bpl
jne 0x1c6850
movl -0x14(%r14,%r15), %r13d
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0x1c6836
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
shrl $0x6, %ebp
shlq $0x21, %r13
shlq $0x20, %rbp
orq %r13, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %rbp, (%rax,%rcx,8)
addq $0x30, %r15
cmpq %r15, %rbx
jne 0x1c67ec
movq (%r12), %rax
testq %rax, %rax
movq 0x8(%rsp), %r15
movq (%rsp), %r14
movq 0x120(%rsp), %rbx
movq 0x10(%rsp), %rbp
je 0x1c6a99
cmpq $0x0, 0x10(%r15)
movq 0x10(%r12), %rdi
je 0x1c6924
movl $0x1c, %ebx
xorl %r14d, %r14d
movq %r15, %rcx
movl $0x2, %r15d
movq (%rcx), %rax
movl -0xc(%rax,%rbx), %ebp
movl (%rax,%rbx), %r13d
movq (%r12), %rax
cmpq 0x8(%r12), %rax
jne 0x1c68d8
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r12)
movq (%r12), %rax
shlq $0x21, %rbp
shlq $0x20, %r13
orq %rbp, %r13
orq %r14, %r13
btsq $0x1f, %r13
leaq 0x1(%rax), %rcx
movq %rcx, (%r12)
movq %r13, (%rdi,%rax,8)
incq %r14
addq $0xa8, %rbx
movq 0x8(%rsp), %rcx
cmpq 0x10(%rcx), %r14
jb 0x1c689d
movq (%r12), %rax
movq %rcx, %r15
movq (%rsp), %r14
movq 0x120(%rsp), %rbx
movq 0x10(%rsp), %rbp
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq (%r12), %rax
testq %rax, %rax
je 0x1c6a99
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 0x1c696c
movq 0x10(%r12), %r8
movq (%r8,%rsi,8), %r9
xorq (%r8,%rdi,8), %r9
shrq $0x20, %r9
je 0x1c69c8
movq %rsi, %r8
subq %rdi, %r8
cmpq $0x2, %r8
jb 0x1c69c5
movq 0x10(%r12), %r8
testb $-0x80, 0x3(%r8,%rdi,8)
sete %r9b
cmpq %rdi, %rsi
seta %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x1c69c5
movq (%r8,%rdi,8), %r9
testl %r9d, %r9d
jns 0x1c69bd
movq (%r15), %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 0x1c6997
movq %rsi, %rdi
incq %rsi
cmpq %rcx, %rsi
jne 0x1c6954
testq %rdx, %rdx
je 0x1c6a99
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1c6a7e
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
movq (%rcx), %r14
cmpl $-0x1, 0x10(%r14,%rbx)
je 0x1c6a58
cmpq %r15, %r13
je 0x1c6a55
imulq $0xa8, %r15, %rbp
addq %r14, %rbp
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq %rbx, %r14
movl $0xa8, %edx
movq %rbp, %rdi
movq 0x10(%rsp), %rbp
movq %r14, %rsi
callq 0x2160
movq 0x8(%rsp), %r14
movq (%r14), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x10(%r14), %rax
incq %r15
incq %r13
addq $0xa8, %rbx
cmpq %rax, %r13
jb 0x1c69ee
jmp 0x1c6a81
testq %rbx, %rbx
setne 0x10(%rsp)
movq $0x0, (%rsp)
jmp 0x1c6b13
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq %r15, 0x10(%rax)
movq %rax, %r15
movq (%rsp), %r14
movq 0x120(%rsp), %rbx
movq $0x0, (%r12)
movq $0x0, (%rsp)
testq %rbx, %rbx
setne 0x10(%rsp)
je 0x1c6b13
testq %rbp, %rbp
je 0x1c6b13
shlq $0x4, %rbp
leaq (,%rbp,2), %rax
addq %rbp, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq %r14, %rcx
addq $0x28, %rcx
movq $0x0, (%rsp)
xorl %edx, %edx
testb $-0x42, (%rcx,%rdx)
je 0x1c6af6
addq $0x30, %rdx
cmpq %rdx, %rax
jne 0x1c6ae5
jmp 0x1c6b13
movl 0x24(%rsp), %edi
movq 0x28(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq %r12, %rcx
callq 0x1c6536
movq %rax, (%rsp)
cmpq $0x0, 0x10(%r15)
je 0x1c6c6a
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %r12d, %r12d
movq (%r15), %r13
cmpb $0x0, 0x10(%rsp)
je 0x1c6b78
cmpq $0x0, (%rsp)
je 0x1c6b78
cmpl $0x0, 0xc(%r13,%rbx)
jne 0x1c6b78
movq %rcx, %r14
addq %rbx, %r13
movq 0x30(%rsp), %rdi
movq %r13, %rsi
movq (%rsp), %rdx
movq 0x128(%rsp), %rcx
movsd 0x38(%rsp), %xmm0
callq 0x1c6642
testq %rax, %rax
je 0x1c6b72
movq %r14, %rcx
jmp 0x1c6c54
movq (%r15), %r13
movq %r14, %rcx
cmpq %r12, %rbp
je 0x1c6bd9
leaq (%rbx,%r13), %r14
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movq %rcx, 0x18(%rsp)
callq 0x2160
imulq $0xa8, %r12, %r15
addq %r15, %r13
movl $0xa8, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r15
movl $0xa8, %edx
movq %r15, %rdi
movq 0x8(%rsp), %r15
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x18(%rsp), %rcx
movq (%r15), %r13
imulq $0xa8, %r12, %r14
cmpl $0x0, 0xc(%r13,%r14)
je 0x1c6c51
leaq (%r14,%r13), %rax
addq $0xc, %rax
movl $0x0, (%rax)
cmpq %r12, %rcx
je 0x1c6c4e
imulq $0xa8, %rcx, %r15
addq %r13, %r15
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %rcx, 0x18(%rsp)
callq 0x2160
addq %r14, %r13
movl $0xa8, %edx
movq %r15, %rdi
movq 0x8(%rsp), %r15
movq %r13, %rsi
callq 0x2160
addq (%r15), %r14
movl $0xa8, %edx
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x18(%rsp), %rcx
incq %rcx
incq %r12
incq %rbp
addq $0xa8, %rbx
cmpq 0x10(%r15), %rbp
jb 0x1c6b27
jmp 0x1c6c6f
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %r12, 0x10(%r15)
movq 0x130(%rsp), %rax
movq %rcx, (%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq