// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: rm_dup_aln(u_trans_t*, unsigned long, asg64_v*, double)
void rm_dup_aln(u_trans_t *u, uint64_t u_n, asg64_v *b, double dup_cut)
{
	if(u_n <= 0) return;
	uint64_t k, m, s, e, dp, old_dp, z, zs, ze, l, os, oe, ovq, cut; u_trans_t t;
	b->n = 0;
	for (k = 0; k < u_n; k++) {
		s = u[k].qs; e = u[k].qe; u[k].occ = 0;
		kv_push(uint64_t, *b, (s<<1)); 
		kv_push(uint64_t, *b, (e<<1|1));
	}
	radix_sort_gfa64(b->a, b->a + b->n);

	for (k = dp = old_dp = m = 0, s = e = (uint64_t)-1; k < b->n; k++) {
		old_dp = dp;
        //if a[j] is qe
        if (b->a[k]&1) --dp;
        else ++dp;

		if (old_dp < 2 && dp >= 2) {///old_dp < dp, b.a[k] is qs
            s = b->a[k]>>1;
        } else if (old_dp >= 2 && dp < 2) {///old_dp > min_dp, b.a[k] is qe
            e = b->a[k]>>1;
			if(e > s) b->a[m++] = (s<<32)|e;///non-unique region
        }
	}

	b->n = m;
	for (k = m = 0; k < u_n; k++) {
		l = 0; s = u[k].qs; e = u[k].qe; 
		cut = (e - s)*dup_cut; 
		if(!(u[k].f)) {//unreliable
			for (z = 0; z < b->n; z++) {
				zs = (b->a[z]>>32); ze = ((uint32_t)b->a[z]);
				if(zs >= e) break;
				if(ze <= s) continue;
				os = MAX(s, zs); oe = MIN(e, ze);
				ovq = ((oe > os)? (oe - os):0);
				l += ovq;
				if(l > cut) break;
			}
		}
		if(((l <= 0) || (l <= cut)) && (l < (e - s))) {
			if(m != k) { t = u[m]; u[m] = u[k]; u[k] = t;}
			m++;
		}
	}

	for (k = m; k < u_n; k++) u[k].del = 1;///delete 

	for (k = b->n = 0; k < m; k++) {
		dp = u[k].nw; dp <<= 32; dp |= k; kv_push(uint64_t, *b, dp);
	}
	radix_sort_gfa64(b->a, b->a + b->n); old_dp = 0;
	for (k = 0; k < b->n; k++) {
		assert(!u[((uint32_t)b->a[k])].del);
		for (z = 0; z < b->n; z++) {
			if(k == z) continue;
			if(u[((uint32_t)b->a[z])].del) continue;
			if(u[((uint32_t)b->a[k])].qs >= u[((uint32_t)b->a[z])].qs && u[((uint32_t)b->a[k])].qe <= u[((uint32_t)b->a[z])].qe) break;
		}
		if(z < b->n) {
			u[((uint32_t)b->a[k])].del = 1; old_dp++;
		}
	}

	if(old_dp) {
		for (k = m = 0; k < u_n; k++) {
			if(u[k].del) continue;
			if(m != k) { t = u[m]; u[m] = u[k]; u[k] = t;}
			m++;
		}
	}


	///for debug is there is any contained overlaps in u[0, m)
	for (k = 0; k < m; k++) {
		for (z = 0; z < m; z++) {
			if(k == z) continue;
			if(u[k].qs >= u[z].qs && u[k].qe <= u[z].qe) break;
		}
		assert(z >= m);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %xmm0, 0x10(%rsp)
testq %rsi, %rsi
je 0x1da45b
movq %rdx, %r15
movq %rdi, %rbx
movq $0x0, (%rdx)
movq 0x10(%rdx), %rdi
movq %rbx, %r13
movq %rsi, (%rsp)
movq %rsi, %rbp
movl (%r13), %r12d
movl 0x4(%r13), %r14d
movl $0x0, 0x18(%r13)
movq (%r15), %rax
cmpq 0x8(%r15), %rax
jne 0x1d9f91
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rax
addq %r12, %r12
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movq %r12, (%rdi,%rax,8)
movq (%r15), %rax
cmpq 0x8(%r15), %rax
jne 0x1d9fcf
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rax
leaq 0x1(,%r14,2), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rcx, (%rdi,%rax,8)
addq $0x30, %r13
decq %rbp
jne 0x1d9f51
movq (%r15), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq (%r15), %rsi
testq %rsi, %rsi
je 0x1da091
movq 0x10(%r15), %rax
movq $-0x1, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r8, %r10
movq (%rax,%rdx,8), %r9
movl %r9d, %r8d
andl $0x1, %r8d
negq %r8
orq $0x1, %r8
addq %r10, %r8
cmpq $0x2, %r10
setb %r11b
cmpq $0x2, %r8
setae %bpl
andb %r11b, %bpl
cmpb $0x1, %bpl
jne 0x1da052
shrq %r9
movq %r9, %rdi
jmp 0x1da087
cmpq $0x2, %r10
setae %r10b
cmpq $0x2, %r8
setb %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x1da087
shrq %r9
cmpq %rdi, %r9
jbe 0x1da087
movq %rdi, %rsi
shlq $0x20, %rsi
orq %rsi, %r9
movq %r9, (%rax,%rcx,8)
incq %rcx
movq (%r15), %rsi
incq %rdx
cmpq %rsi, %rdx
jb 0x1da019
jmp 0x1da093
xorl %ecx, %ecx
movq %rcx, (%r15)
xorl %eax, %eax
movq 0x571e0(%rip), %xmm0    # 0x231280
movapd 0x571e8(%rip), %xmm1    # 0x231290
movsd 0x57258(%rip), %xmm5    # 0x231308
xorl %r12d, %r12d
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
movl (%rbx,%rcx), %esi
movl 0x4(%rbx,%rcx), %edi
movq %rdi, %rdx
subq %rsi, %rdx
movq %rdx, %xmm2
punpckldq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
subpd %xmm1, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
mulsd 0x10(%rsp), %xmm3
cvttsd2si %xmm3, %r9
movq %r9, %r10
sarq $0x3f, %r10
subsd %xmm5, %xmm3
cvttsd2si %xmm3, %r8
andq %r10, %r8
orq %r9, %r8
testb $0x3f, 0x28(%rbx,%rcx)
je 0x1da1a1
xorl %r9d, %r9d
testq %r9, %r9
sete %sil
cmpq %r8, %r9
setbe %dil
orb %sil, %dil
cmpb $0x1, %dil
jne 0x1da205
cmpq %rdx, %r9
movq (%rsp), %r14
jae 0x1da209
cmpq %rax, %r12
je 0x1da19c
addq %rbx, %rcx
leaq (%r12,%r12,2), %rdx
shlq $0x4, %rdx
movups (%rbx,%rdx), %xmm2
movups 0x10(%rbx,%rdx), %xmm3
movups 0x20(%rbx,%rdx), %xmm4
movaps %xmm4, 0x40(%rsp)
movaps %xmm3, 0x30(%rsp)
movaps %xmm2, 0x20(%rsp)
movups (%rcx), %xmm2
movups 0x10(%rcx), %xmm3
movups 0x20(%rcx), %xmm4
movups %xmm4, 0x20(%rbx,%rdx)
movups %xmm3, 0x10(%rbx,%rdx)
movups %xmm2, (%rbx,%rdx)
movapd 0x20(%rsp), %xmm2
movapd 0x30(%rsp), %xmm3
movaps 0x40(%rsp), %xmm4
movups %xmm4, 0x20(%rcx)
movupd %xmm3, 0x10(%rcx)
movupd %xmm2, (%rcx)
incq %r12
jmp 0x1da209
movq (%r15), %r10
testq %r10, %r10
je 0x1da10d
movq 0x10(%r15), %r11
xorl %r9d, %r9d
xorl %r14d, %r14d
movq (%r11,%r14,8), %rbp
movq %rbp, %r13
shrq $0x20, %r13
cmpq %rdi, %r13
jae 0x1da110
movl %ebp, %ebp
cmpl %esi, %ebp
jbe 0x1da1f8
cmpq %rsi, %r13
cmovbeq %rsi, %r13
cmpq %rdi, %rbp
cmovaeq %rdi, %rbp
subq %r13, %rbp
movl $0x0, %r13d
cmovaeq %rbp, %r13
addq %r13, %r9
cmpq %r8, %r9
ja 0x1da110
incq %r14
cmpq %r14, %r10
jne 0x1da1b7
jmp 0x1da110
movq (%rsp), %r14
incq %rax
cmpq %r14, %rax
jne 0x1da0b3
cmpq %r14, %r12
jae 0x1da252
leaq (%r12,%r12,2), %rax
shlq $0x4, %rax
addq %rbx, %rax
addq $0x28, %rax
movq %r14, %rcx
orb $-0x80, (%rax)
decq %rcx
addq $0x30, %rax
cmpq %rcx, %r12
jne 0x1da22c
movq $0x0, (%r15)
testq %r12, %r12
jne 0x1da259
movq 0x10(%r15), %rdi
xorl %eax, %eax
jmp 0x1da2f9
movq $0x0, (%r15)
movq 0x10(%r15), %rdi
leaq 0x20(%rbx), %r13
xorl %ebp, %ebp
movsd (%r13), %xmm1
movapd %xmm1, %xmm0
subsd %xmm5, %xmm0
movq (%r15), %rax
cmpq 0x8(%r15), %rax
jne 0x1da2bf
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movapd %xmm0, 0x10(%rsp)
movapd %xmm1, (%rsp)
callq 0x2370
movapd (%rsp), %xmm1
movapd 0x10(%rsp), %xmm0
movsd 0x57053(%rip), %xmm5    # 0x231308
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rax
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
cvttsd2si %xmm0, %rsi
andl %edx, %esi
orl %esi, %ecx
shlq $0x20, %rcx
orq %rbp, %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rcx, (%rdi,%rax,8)
incq %rbp
addq $0x30, %r13
cmpq %rbp, %r12
jne 0x1da263
movq (%r15), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq (%r15), %rcx
testq %rcx, %rcx
je 0x1da415
xorl %eax, %eax
xorl %edx, %edx
movq 0x10(%r15), %rsi
movl (%rsi,%rax,8), %edi
leaq (%rdi,%rdi,2), %r8
shlq $0x4, %r8
movb 0x28(%rbx,%r8), %dil
testb %dil, %dil
js 0x1da489
leaq (%rbx,%r8), %r9
addq $0x28, %r9
addq %rbx, %r8
xorl %r10d, %r10d
cmpq %r10, %rax
je 0x1da36b
movl (%rsi,%r10,8), %r11d
leaq (%r11,%r11,2), %r11
shlq $0x4, %r11
cmpb $0x0, 0x28(%rbx,%r11)
js 0x1da36b
addq %rbx, %r11
movl (%r8), %ebp
cmpl (%r11), %ebp
jb 0x1da36b
movl 0x4(%r8), %ebp
cmpl 0x4(%r11), %ebp
jbe 0x1da37d
incq %r10
cmpq %r10, %rcx
jne 0x1da33d
incq %rax
cmpq %rcx, %rax
jb 0x1da312
jmp 0x1da38c
orb $-0x80, %dil
movb %dil, (%r9)
incq %rdx
movq (%r15), %rcx
jmp 0x1da373
testq %rdx, %rdx
je 0x1da415
xorl %eax, %eax
movq %rbx, %rcx
xorl %r12d, %r12d
cmpb $0x0, 0x28(%rcx)
js 0x1da409
cmpq %r12, %rax
je 0x1da406
leaq (%r12,%r12,2), %rdx
shlq $0x4, %rdx
movups (%rbx,%rdx), %xmm0
movups 0x10(%rbx,%rdx), %xmm1
movups 0x20(%rbx,%rdx), %xmm2
movaps %xmm2, 0x40(%rsp)
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rbx,%rdx)
movups %xmm1, 0x10(%rbx,%rdx)
movups %xmm0, (%rbx,%rdx)
movdqa 0x20(%rsp), %xmm0
movapd 0x30(%rsp), %xmm1
movapd 0x40(%rsp), %xmm2
movupd %xmm2, 0x20(%rcx)
movupd %xmm1, 0x10(%rcx)
movdqu %xmm0, (%rcx)
incq %r12
incq %rax
addq $0x30, %rcx
cmpq %rax, %r14
jne 0x1da39d
testq %r12, %r12
je 0x1da45b
leaq 0x4(%rbx), %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
addq %rbx, %rdx
movq %rax, %rsi
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x1da447
movl (%rdx), %r8d
cmpl -0x4(%rsi), %r8d
jb 0x1da447
movl 0x4(%rdx), %r8d
cmpl (%rsi), %r8d
jbe 0x1da46a
incq %rdi
addq $0x30, %rsi
cmpq %rdi, %r12
jne 0x1da430
incq %rcx
cmpq %r12, %rcx
jne 0x1da420
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6924e(%rip), %rdi     # 0x2436bf
leaq 0x657e3(%rip), %rsi     # 0x23fc5b
leaq 0x69206(%rip), %rcx     # 0x243685
movl $0x449b, %edx           # imm = 0x449B
callq 0x2110
leaq 0x691d9(%rip), %rdi     # 0x243669
leaq 0x657c4(%rip), %rsi     # 0x23fc5b
leaq 0x691e7(%rip), %rcx     # 0x243685
movl $0x4481, %edx           # imm = 0x4481
callq 0x2110