// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: select_clean_chain(kv_ul_ov_t*, ul_ov_t*, long, long, asg64_v*)
int64_t select_clean_chain(kv_ul_ov_t *idx, ul_ov_t *res_a, int64_t res_n, int64_t ulid_local, asg64_v *b64)
{
	ul_ov_t kp, *m, *p, *idx_a = idx->a; uint64_t om, ovlp, min_sc, max_sc, ok, z;
	int64_t k, i, idx_n = idx->n, mm, n_mchain; 
	for (k = 0, mm = idx_n>>1; k < mm; k++) {
        kp = idx_a[k]; idx_a[k] = idx_a[idx_n-k-1]; idx_a[idx_n-k-1] = kp;
		idx_a[k].tn = idx_a[idx_n-k-1].tn = 1;
    }
	if(idx_n&1) idx_a[k].tn = 1;
	
	for (k = 0; k < idx_n; k++) {//filter too close chains
        m = &(idx_a[k]); om = m->qe - m->qs; ///current chain
        // fprintf(stderr, "k::%ld[M::%s::sc->%u] q::[%u, %u), set::%u\n", k, __func__, m->qn, m->qs, m->qe, m->tn);
        if(m->tn == 0) continue;
        for (i = k-1; i >= 0; i--) {
            p = &(idx_a[i]);
            ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
            if(ovlp == 0) continue;
            min_sc = MIN(p->qn, m->qn); max_sc = MAX(p->qn, m->qn);
            ok = p->qe - p->qs; ok = MAX(ok, om);
            if(min_sc < (max_sc*0.98)) break;
            if((ovlp > GC_OFFSET_POS) && (min_sc > (max_sc*0.98)) && (ovlp > (ok*0.8))) {
                // fprintf(stderr, "k::%ld[M::%s::i->%ld] min_sc::%ld, max_sc::%ld\n", 
                // k, __func__, i, min_sc, max_sc);
                m->tn = p->tn = 0;
            }
        }

        for (i = k+1; i < idx_n; i++) {
            p = &(idx_a[i]);
            ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
            if(ovlp == 0) continue;
            min_sc = MIN(p->qn, m->qn); max_sc = MAX(p->qn, m->qn);
            ok = p->qe - p->qs; ok = MAX(ok, om);
            if(min_sc < (max_sc*0.98)) break;
            if((ovlp > GC_OFFSET_POS) && (min_sc > (max_sc*0.98)) && (ovlp > (ok*0.8))) {
                // fprintf(stderr, "k::%ld[M::%s::i->%ld] min_sc::%ld, max_sc::%ld\n", 
                // k, __func__, i, min_sc, max_sc);
                m->tn = p->tn = 0;
            }
        }
    }
	
    for (k = i = 0; k < idx_n; k++) {
        m = &(idx_a[k]); if(m->tn == 0) continue;
        idx_a[i++] = idx_a[k];
    }
    // fprintf(stderr, "[M::%s::] gb_n0::%ld, gb_n::%ld\n", __func__, gb_n, i);
    idx->n = idx_n = i;
    for (k = n_mchain = 0; k < idx_n; k++) {
        m = &(idx_a[k]); om = m->qe - m->qs;
        for (i = 0; i < n_mchain; i++) {
            p = &(idx_a[i]);
            ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
            if(ovlp == 0) continue;
            ok = p->qe - p->qs;
            if((ovlp > ok*0.1) || (ovlp > om*0.1)) break;
        }
        if(i < n_mchain) continue;
        idx_a[n_mchain++] = idx_a[k];
    }
    idx->n = idx_n = n_mchain;

	b64->n = idx->n; kv_resize(uint64_t, *b64, b64->n);
	for (k = 0; k < idx_n; k++) {
		om = idx_a[k].ts; om <<= 32; om |= k; b64->a[k] = om;
	}
	radix_sort_gfa64(b64->a, b64->a + b64->n);
	for (k = res_n = 0; k < idx_n; k++) {
		m = &(idx_a[(uint32_t)(b64->a[k])]); 
		for (z = m->ts, ok = SEC_MODE; z < m->te; z++) {
			res_a[res_n] = res_a[z]; res_a[res_n].el = 1; 
			res_a[res_n].tn |= ((uint32_t)(0x80000000));
			res_a[res_n].sec = ok; 
			res_a[res_n].qn = ((idx_n<=1)?ulid_local:res_n);
			ok = res_n; res_n++;
		}
	}

	if(idx_n > 1) {
		radix_sort_ul_ov_srt_qe(res_a, res_a + res_n);
		for (i = 1, k = 0; i <= res_n; i++) {
			if (i == res_n || res_a[i].qe != res_a[k].qe) {
				if(i-k>1) radix_sort_ul_ov_srt_qs(res_a+k, res_a+i);
				k = i;
			}
		}
		b64->n = res_n; kv_resize(uint64_t, *b64, b64->n);
		for (i = 0; i < res_n; i++) b64->a[res_a[i].qn] = i;
		for (i = 0; i < res_n; i++) {
			if(res_a[b64->a[i]].sec != SEC_MODE) {
				res_a[b64->a[i]].sec = b64->a[res_a[b64->a[i]].sec];
			}
			res_a[b64->a[i]].qn = ulid_local;
		}
	}
	return res_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, %rax
movq (%rdi), %rdi
movq %rax, 0x28(%rsp)
movq 0x8(%rax), %rax
movq %rax, %rdx
sarq %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
cmovgq %rdx, %rcx
imulq $0x1c, %rax, %rdx
addq %rdi, %rdx
addq $-0x1c, %rdx
pushq $0x1
popq %rsi
movq %rdi, (%rsp)
movq %rcx, %r8
subq $0x1, %r8
jb 0x177b17
movups (%rdi), %xmm0
movups 0xc(%rdi), %xmm1
movups %xmm1, 0x4c(%rsp)
movaps %xmm0, 0x40(%rsp)
movups (%rdx), %xmm0
movups 0xc(%rdx), %xmm1
movups %xmm1, 0xc(%rdi)
movups %xmm0, (%rdi)
movups 0x4c(%rsp), %xmm0
movups %xmm0, 0xc(%rdx)
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%rdx)
movl %esi, 0xc(%rdx)
movl %esi, 0xc(%rdi)
addq $-0x1c, %rdx
addq $0x1c, %rdi
jmp 0x177ad1
testb $0x1, %al
je 0x177b2b
imulq $0x1c, %rcx, %rcx
movq (%rsp), %rdx
movl $0x1, 0xc(%rdx,%rcx)
xorl %esi, %esi
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
movq (%rsp), %rdx
leaq -0x10(%rdx), %rdi
movq %rdi, 0x30(%rsp)
addq $0x28, %rdx
movq %rdx, 0x20(%rsp)
pushq $0x1
popq %rdx
movq %rdx, 0x18(%rsp)
movsd 0x79ed1(%rip), %xmm0    # 0x1f1a30
movsd 0x6b999(%rip), %xmm1    # 0x1e3500
cmpq %rcx, %rsi
je 0x177cf1
movq %rsi, 0x38(%rsp)
imulq $0x1c, %rsi, %rsi
movq (%rsp), %rdx
cmpl $0x0, 0xc(%rdx,%rsi)
je 0x177cd3
movq (%rsp), %rdx
leaq (%rdx,%rsi), %r10
movl 0x4(%rdx,%rsi), %r11d
movl 0x8(%rdx,%rsi), %ebp
movl %ebp, %r12d
subl %r11d, %r12d
movq 0x30(%rsp), %r9
movq 0x38(%rsp), %r15
testq %r15, %r15
jle 0x177c3a
movl -0x8(%r9), %ebx
movl -0x4(%r9), %esi
cmpl %esi, %ebp
movl %esi, %edi
cmovbl %ebp, %edi
cmpl %ebx, %r11d
movl %ebx, %r8d
cmoval %r11d, %r8d
subl %r8d, %edi
jbe 0x177c2e
movl -0xc(%r9), %r8d
movl (%r10), %r13d
cmpl %r13d, %r8d
movl %r13d, %edx
cmovbl %r8d, %edx
cmoval %r8d, %r13d
subl %ebx, %esi
cmpl %r12d, %esi
cmovbel %r12d, %esi
cvtsi2sd %rdx, %xmm2
cvtsi2sd %r13, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x177c3a
cmpl $0x9, %edi
jb 0x177c2e
ucomisd %xmm3, %xmm2
jbe 0x177c2e
movl %edi, %edx
cvtsi2sd %rdx, %xmm2
movl %esi, %edx
cvtsi2sd %rdx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x177c2e
andl $0x0, (%r9)
andl $0x0, 0xc(%r10)
addq $-0x1c, %r9
decq %r15
jmp 0x177ba9
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r9
cmpq %rax, %r9
jge 0x177cd3
movl -0x8(%r15), %r8d
movl -0x4(%r15), %esi
cmpl %esi, %ebp
movl %esi, %edi
cmovbl %ebp, %edi
cmpl %r8d, %r11d
movl %r8d, %edx
cmoval %r11d, %edx
subl %edx, %edi
jbe 0x177cc7
movl -0xc(%r15), %edx
movl (%r10), %ebx
cmpl %ebx, %edx
movl %ebx, %r13d
cmovbl %edx, %r13d
cmoval %edx, %ebx
subl %r8d, %esi
cmpl %r12d, %esi
cmovbel %r12d, %esi
cvtsi2sd %r13, %xmm2
cvtsi2sd %rbx, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x177cd3
cmpl $0x9, %edi
jb 0x177cc7
ucomisd %xmm3, %xmm2
jbe 0x177cc7
movl %edi, %edx
cvtsi2sd %rdx, %xmm2
movl %esi, %edx
cvtsi2sd %rdx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x177cc7
andl $0x0, (%r15)
andl $0x0, 0xc(%r10)
incq %r9
addq $0x1c, %r15
jmp 0x177c44
movq 0x38(%rsp), %rsi
incq %rsi
addq $0x1c, 0x30(%rsp)
incq 0x18(%rsp)
addq $0x1c, 0x20(%rsp)
jmp 0x177b67
xorl %eax, %eax
movq (%rsp), %rdx
movq (%rsp), %rdi
subq $0x1, %rcx
jb 0x177d28
cmpl $0x0, 0xc(%rdx)
je 0x177d22
imulq $0x1c, %rax, %rsi
incq %rax
movupd (%rdx), %xmm0
movupd 0xc(%rdx), %xmm1
movupd %xmm1, 0xc(%rdi,%rsi)
movupd %xmm0, (%rdi,%rsi)
addq $0x1c, %rdx
jmp 0x177cfb
movq 0x28(%rsp), %rcx
movq %rax, 0x8(%rcx)
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
movq (%rsp), %rdx
addq $0x8, %rdx
movsd 0x6aea6(%rip), %xmm0    # 0x1e2bf0
xorl %ebp, %ebp
cmpq %rax, %rcx
je 0x177df9
imulq $0x1c, %rcx, %rdi
movq (%rsp), %r9
leaq (%r9,%rdi), %rsi
movl 0x4(%r9,%rdi), %r8d
movl 0x8(%r9,%rdi), %r9d
movl %r9d, %edi
subl %r8d, %edi
cvtsi2sd %rdi, %xmm1
mulsd %xmm0, %xmm1
testq %rbp, %rbp
movl $0x0, %edi
cmovgq %rbp, %rdi
movq %rdx, %r10
subq $0x1, %rdi
jb 0x177dd2
movl -0x4(%r10), %ebx
movl (%r10), %r11d
cmpl %r11d, %r9d
movl %r11d, %r15d
cmovbl %r9d, %r15d
cmpl %ebx, %r8d
movl %ebx, %r12d
cmoval %r8d, %r12d
subl %r12d, %r15d
jbe 0x177dcc
subl %ebx, %r11d
cvtsi2sd %r15, %xmm2
cvtsi2sd %r11, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x177df1
ucomisd %xmm1, %xmm2
ja 0x177df1
addq $0x1c, %r10
jmp 0x177d89
imulq $0x1c, %rbp, %rdi
incq %rbp
movupd (%rsi), %xmm1
movupd 0xc(%rsi), %xmm2
movq (%rsp), %rsi
movupd %xmm2, 0xc(%rsi,%rdi)
movupd %xmm1, (%rsi,%rdi)
incq %rcx
jmp 0x177d4c
movq 0x28(%rsp), %rax
movq %rbp, 0x8(%rax)
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
cmpq %rbp, 0x8(%rax)
jae 0x177e67
leaq -0x1(%rbp), %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 0x8(%rsp), %rbx
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
xorl %eax, %eax
testq %rbp, %rbp
movl $0x0, %r15d
cmovgq %rbp, %r15
movq (%rsp), %rcx
addq $0x10, %rcx
cmpq %rax, %r15
je 0x177ea2
movl (%rcx), %edx
shlq $0x20, %rdx
orq %rax, %rdx
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rsi
movq %rdx, (%rsi,%rax,8)
incq %rax
addq $0x1c, %rcx
jmp 0x177e7e
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
xorl %eax, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
xorl %r12d, %r12d
cmpq %r15, %rax
je 0x177f69
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl (%rdx,%rax,8), %edx
imulq $0x1c, %rdx, %rsi
movq (%rsp), %rdi
leaq (%rdi,%rsi), %rdx
movl 0x10(%rdi,%rsi), %esi
imulq $0x1c, %rsi, %rdi
imulq $0x1c, %r12, %r8
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x10(%rsp), %r9
movl 0x14(%rdx), %r11d
cmpq %r11, %rsi
jae 0x177f61
movupd (%r9,%rdi), %xmm0
movupd 0xc(%r9,%rdi), %xmm1
movupd %xmm1, 0xc(%r9,%r8)
movupd %xmm0, (%r9,%r8)
orb $-0x80, 0xf(%r9,%r8)
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movl 0x18(%r9,%r8), %r11d
andl %ecx, %r11d
cmpq $0x2, %rbp
leal 0x40000000(%r11,%r10), %r10d
movl %r10d, 0x18(%r9,%r8)
leaq 0x1(%r12), %r11
movq %r12, %r10
movl %r12d, %ebx
cmovll %r14d, %ebx
movl %ebx, (%r9,%r8)
incq %rsi
addq $0x1c, %r9
movq %r11, %r12
jmp 0x177ef9
incq %rax
jmp 0x177ec1
cmpq $0x2, %rbp
jl 0x1780f1
imulq $0x1c, %r12, %rsi
movq 0x10(%rsp), %rbx
addq %rbx, %rsi
movq %rbx, %rdi
callq 0x156f5c
movq %rbx, %rsi
xorl %eax, %eax
testq %r12, %r12
movl $0x0, %ebp
cmovgq %r12, %rbp
movq %rbp, %rbx
negq %rbx
leaq 0x1c(%rsi), %r13
pushq $0x1
popq %r15
leaq (%rbx,%r15), %rcx
cmpq $0x1, %rcx
je 0x177ffa
cmpq %r15, %r12
je 0x177fc8
movl 0x8(%r13), %ecx
imulq $0x1c, %rax, %rdx
cmpl 0x8(%rsi,%rdx), %ecx
jne 0x177fc8
movq %rax, %rcx
jmp 0x177fee
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x177fee
imulq $0x1c, %rax, %rdi
addq %rsi, %rdi
movq %r13, %rsi
callq 0x1571fa
movq 0x10(%rsp), %rsi
movq %r15, %rcx
incq %r15
addq $0x1c, %r13
movq %rcx, %rax
jmp 0x177fa6
movq 0x8(%rsp), %rax
movq %r12, (%rax)
cmpq %r12, 0x8(%rax)
jae 0x178065
leaq -0x1(%r12), %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 0x8(%rsp), %rbx
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x10(%rsp), %rsi
movq %rax, 0x10(%rbx)
xorl %eax, %eax
movq %rsi, %rcx
cmpq %rax, %rbp
je 0x178087
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl (%rcx), %esi
movq %rax, (%rdx,%rsi,8)
incq %rax
addq $0x1c, %rcx
jmp 0x17806a
xorl %eax, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq 0x10(%rsp), %r11
cmpq %rax, %rbp
je 0x1780f1
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rdi
movq (%rdi,%rax,8), %r8
imulq $0x1c, %r8, %r9
movl 0x18(%r11,%r9), %esi
movq %rsi, %r10
andq %rcx, %r10
cmpq %rcx, %r10
je 0x1780e4
leaq (%r11,%r9), %r8
addq $0x18, %r8
movl (%rdi,%r10,8), %edi
andl %edx, %edi
andl $0xc0000000, %esi       # imm = 0xC0000000
orl %edi, %esi
movl %esi, (%r8)
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rsi
movq (%rsi,%rax,8), %r8
imulq $0x1c, %r8, %rsi
movl %r14d, (%r11,%rsi)
incq %rax
jmp 0x178098
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq