// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movq %rdx, %r15
movq 0x130(%rsp), %rax
andq $0x0, (%rax)
movq 0x10(%rsi), %rax
movq %rax, 0x10(%rsp)
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, %ebx
shrq $0x20, %rax
movq %rax, (%rsp)
testq %r9, %r9
movq %r13, 0x8(%rsp)
je 0x17b320
movq 0x10(%r13), %rax
addq %rbx, %rax
cmpq %rax, 0x8(%r15)
jae 0x17b0f4
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)
andq $0x0, (%r15)
movq %rbx, 0x18(%rsp)
imulq $0x30, %rbx, %r14
imulq $0x30, (%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx), %r12
addq $0x28, %r12
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x17b17c
movzbl (%r12,%r13), %ebp
testb $-0x42, %bpl
jne 0x17b176
movl -0x14(%r12,%r13), %ebx
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x17b15d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
shrl $0x6, %ebp
shlq $0x21, %rbx
shlq $0x20, %rbp
orq %rbx, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rbp, (%rax,%rcx,8)
addq $0x30, %r13
jmp 0x17b116
cmpq $0x0, (%r15)
movq 0x8(%rsp), %r13
movq 0x18(%rsp), %rbx
je 0x17b320
pushq $0x1c
popq %r14
xorl %r12d, %r12d
cmpq 0x10(%r13), %r12
jae 0x17b20b
movq (%r13), %rax
movl -0xc(%rax,%r14), %ebx
movl (%rax,%r14), %r13d
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x17b1dc
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
shlq $0x21, %rbx
shlq $0x20, %r13
orq %rbx, %r13
orq %r12, %r13
btsq $0x1f, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %r13, (%rax,%rcx,8)
incq %r12
addq $0xa8, %r14
movq 0x8(%rsp), %r13
jmp 0x17b197
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq (%r15), %rax
pushq $0x1
popq %rcx
xorl %esi, %esi
xorl %edx, %edx
movq 0x18(%rsp), %rbx
cmpq %rax, %rcx
ja 0x17b290
je 0x17b243
movq 0x10(%r15), %rdi
movq (%rdi,%rcx,8), %r8
xorq (%rdi,%rsi,8), %r8
shrq $0x20, %r8
je 0x17b28b
movq %rcx, %rdi
subq %rsi, %rdi
cmpq $0x2, %rdi
jb 0x17b288
movq 0x10(%r15), %rdi
testb $-0x80, 0x3(%rdi,%rsi,8)
jne 0x17b288
cmpq %rcx, %rsi
jae 0x17b288
movq (%rdi,%rsi,8), %r8
testl %r8d, %r8d
jns 0x17b283
movq (%r13), %r9
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
imulq $0xa8, %r8, %r8
orl $-0x1, 0x10(%r9,%r8)
incq %rdx
incq %rsi
jmp 0x17b25a
movq %rcx, %rsi
incq %rcx
jmp 0x17b22a
testq %rdx, %rdx
je 0x17b320
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
cmpq 0x10(%rax), %r13
jae 0x17b314
movq (%rax), %r14
cmpl $-0x1, 0x10(%r14,%rbx)
je 0x17b308
cmpq %r12, %r13
je 0x17b305
imulq $0xa8, %r12, %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 %r14, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
incq %r12
incq %r13
addq $0xa8, %rbx
jmp 0x17b2a1
movq %r12, 0x10(%rax)
movq %rax, %r13
movq 0x18(%rsp), %rbx
andq $0x0, (%r15)
cmpq $0x0, 0x120(%rsp)
je 0x17b383
imulq $0x30, %rbx, %rax
addq $0x30, %rax
imulq $0x30, (%rsp), %rcx
movq 0x10(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
movq $0x0, (%rsp)
xorl %edx, %edx
leaq 0x30(%rdx), %rsi
cmpq %rsi, %rax
je 0x17b38b
testb $-0x42, (%rcx,%rdx)
movq %rsi, %rdx
jne 0x17b352
movl 0x24(%rsp), %edi
movq 0x28(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq %r15, %rcx
callq 0x17ae8d
movq %rax, (%rsp)
jmp 0x17b38b
movq $0x0, (%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %ebx, %ebx
cmpq 0x10(%r13), %rbx
jae 0x17b4eb
movq (%r13), %r12
cmpq $0x0, 0x120(%rsp)
je 0x17b3f5
cmpq $0x0, (%rsp)
je 0x17b3f5
cmpl $0x0, 0xc(%r12,%r15)
jne 0x17b3f5
movq %rcx, %r14
addq %r15, %r12
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movq (%rsp), %rdx
movq 0x128(%rsp), %rcx
movsd 0x38(%rsp), %xmm0
callq 0x17af94
testq %rax, %rax
je 0x17b3ee
movq %r14, %rcx
jmp 0x17b4dc
movq (%r13), %r12
movq %r14, %rcx
cmpq %rbp, %rbx
je 0x17b45d
leaq (%r12,%r15), %r14
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movq %r15, 0x10(%rsp)
movq %rcx, %r15
callq 0x2160
imulq $0xa8, %rbp, %r13
addq %r13, %r12
movl $0xa8, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r13
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %r13
leaq 0x40(%rsp), %rsi
callq 0x2160
movq %r15, %rcx
movq 0x10(%rsp), %r15
movq (%r13), %r12
imulq $0xa8, %rbp, %r14
cmpl $0x0, 0xc(%r12,%r14)
je 0x17b4d9
leaq (%r12,%r14), %rax
addq $0xc, %rax
andl $0x0, (%rax)
cmpq %rbp, %rcx
je 0x17b4d6
imulq $0xa8, %rcx, %r13
addq %r12, %r13
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %r15, 0x10(%rsp)
movq %rcx, %r15
callq 0x2160
addq %r14, %r12
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %r13
movq %r12, %rsi
callq 0x2160
addq (%r13), %r14
movl $0xa8, %edx
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2160
movq %r15, %rcx
movq 0x10(%rsp), %r15
incq %rcx
incq %rbp
incq %rbx
addq $0xa8, %r15
jmp 0x17b394
movq %rbp, 0x10(%r13)
movq 0x130(%rsp), %rax
movq %rcx, (%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq