// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rsi, 0x50(%rsp)
movq (%rdi), %r13
movq %rdi, 0x18(%rsp)
movq 0x8(%rdi), %rax
movq %rax, %r15
sarq %r15
testq %r15, %r15
jle 0x1c2dfa
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
addq %r13, %rdx
addq $-0x1c, %rdx
movl $0x1, %esi
movq %r13, %r8
movq %r15, %rdi
movq (%r8), %r9
movl 0x8(%r8), %r10d
movl 0xc(%r8), %r11d
movl 0xc(%r8), %ebx
movl %ebx, 0x2c(%rsp)
movq 0x10(%r8), %rbx
movq %rbx, 0x30(%rsp)
movl 0x18(%r8), %ebx
movl %ebx, 0x38(%rsp)
movq %r9, 0x20(%rsp)
movl %r10d, 0x28(%rsp)
movl %r11d, 0x2c(%rsp)
movupd (%rdx), %xmm0
movupd 0xc(%rdx), %xmm1
movupd %xmm1, 0xc(%r8)
movupd %xmm0, (%r8)
movl 0x2c(%rsp), %r9d
movl %r9d, 0xc(%rdx)
movq 0x30(%rsp), %r9
movq %r9, 0x10(%rdx)
movl 0x38(%rsp), %r9d
movl %r9d, 0x18(%rdx)
movq 0x20(%rsp), %r9
movq %r9, (%rdx)
movl 0x28(%rsp), %r9d
movl %r9d, 0x8(%rdx)
movl 0x2c(%rsp), %r9d
movl %r9d, 0xc(%rdx)
movl %esi, 0xc(%rdx)
movl %esi, 0xc(%r8)
addq $0x1c, %r8
addq $-0x1c, %rdx
decq %rdi
jne 0x1c2d64
jmp 0x1c2dfd
xorl %r15d, %r15d
testb $0x1, %al
je 0x1c2e15
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r15, %rdx
movl $0x1, 0xc(%r13,%rdx)
testq %rax, %rax
jle 0x1c342c
movq %rcx, 0x48(%rsp)
movq %r14, 0x10(%rsp)
leaq -0x10(%r13), %rcx
movq %rcx, 0x60(%rsp)
leaq 0x28(%r13), %rcx
movq %rcx, 0x58(%rsp)
movsd 0x7cd1e(%rip), %xmm0    # 0x23fb60
movsd 0x6e766(%rip), %xmm1    # 0x2315b0
xorl %r8d, %r8d
movq %r13, 0x8(%rsp)
leaq (%r8,%r8,8), %rcx
leaq (%rcx,%rcx,2), %r12
leaq (%r12,%r8), %rcx
cmpl $0x0, 0xc(%r13,%rcx)
je 0x1c2fe0
leaq (%rcx,%r13), %r9
movl 0x4(%r13,%rcx), %r10d
movl 0x8(%r13,%rcx), %r11d
movl %r11d, %ebp
subl %r10d, %ebp
testq %r8, %r8
je 0x1c2f2b
leaq 0x1(%r8), %r15
movq 0x60(%rsp), %rcx
leaq (%rcx,%r8), %rbx
addq %r12, %rbx
movl -0x8(%rbx), %r14d
movl -0x4(%rbx), %edx
cmpl %edx, %r11d
movl %edx, %edi
cmovbl %r11d, %edi
cmpl %r14d, %r10d
movl %r14d, %ecx
cmoval %r10d, %ecx
subl %ecx, %edi
jbe 0x1c2f1a
movl -0xc(%rbx), %ecx
movl (%r9), %esi
cmpl %esi, %ecx
movl %esi, %r13d
cmovbl %ecx, %r13d
cmoval %ecx, %esi
subl %r14d, %edx
cmpl %ebp, %edx
cmovbel %ebp, %edx
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x1c2f2b
cmpl $0x9, %edi
jb 0x1c2f1a
ucomisd %xmm3, %xmm2
jbe 0x1c2f1a
movl %edi, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl %edx, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1c2f1a
xorl %ecx, %ecx
movl %ecx, (%rbx)
movl %ecx, 0xc(%r9)
decq %r15
addq $-0x1c, %rbx
cmpq $0x1, %r15
jg 0x1c2e97
leaq 0x1(%r8), %r15
cmpq %rax, %r15
jge 0x1c2fd9
addq 0x58(%rsp), %r8
addq %r12, %r8
movq %r15, %rbx
movl -0x8(%r8), %ecx
movl -0x4(%r8), %edx
cmpl %edx, %r11d
movl %edx, %edi
cmovbl %r11d, %edi
cmpl %ecx, %r10d
movl %ecx, %esi
cmoval %r10d, %esi
subl %esi, %edi
jbe 0x1c2fc9
movl -0xc(%r8), %esi
movl (%r9), %r14d
cmpl %r14d, %esi
movl %r14d, %r12d
cmovbl %esi, %r12d
cmoval %esi, %r14d
subl %ecx, %edx
cmpl %ebp, %edx
cmovbel %ebp, %edx
xorps %xmm2, %xmm2
cvtsi2sd %r12, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r14, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x1c2fd9
cmpl $0x9, %edi
jb 0x1c2fc9
ucomisd %xmm3, %xmm2
jbe 0x1c2fc9
movl %edi, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl %edx, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1c2fc9
xorl %ecx, %ecx
movl %ecx, (%r8)
movl %ecx, 0xc(%r9)
incq %rbx
addq $0x1c, %r8
cmpq %rax, %rbx
jl 0x1c2f43
movq 0x8(%rsp), %r13
jmp 0x1c2fe6
incq %r8
movq %r8, %r15
movq %r15, %r8
cmpq %rax, %r15
jne 0x1c2e52
testq %rax, %rax
movq 0x10(%rsp), %r14
jle 0x1c342c
xorl %ecx, %ecx
movq %r13, %rdx
cmpl $0x0, 0xc(%rdx)
je 0x1c3030
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
incq %rcx
movupd (%rdx), %xmm0
movupd 0xc(%rdx), %xmm1
movupd %xmm1, 0xc(%r13,%rsi)
movupd %xmm0, (%r13,%rsi)
addq $0x1c, %rdx
decq %rax
jne 0x1c3005
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rax)
testq %rcx, %rcx
jle 0x1c342c
leaq 0x8(%r13), %rax
xorl %ebp, %ebp
movsd 0x6dbb7(%rip), %xmm0    # 0x230c10
xorl %edx, %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
testq %rbp, %rbp
jle 0x1c30dd
movq 0x8(%rsp), %rdi
movl 0x4(%rdi,%rsi), %r8d
movl 0x8(%rdi,%rsi), %r9d
movl %r9d, %edi
subl %r8d, %edi
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
mulsd %xmm0, %xmm1
movq %rax, %rdi
movq %rbp, %r10
movl -0x4(%rdi), %ebx
movl (%rdi), %r11d
cmpl %r11d, %r9d
movl %r11d, %r15d
cmovbl %r9d, %r15d
cmpl %ebx, %r8d
movl %ebx, %r14d
cmoval %r8d, %r14d
subl %r14d, %r15d
jbe 0x1c30d4
subl %ebx, %r11d
xorps %xmm2, %xmm2
cvtsi2sd %r15, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r11, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x1c3110
ucomisd %xmm1, %xmm2
ja 0x1c3110
addq $0x1c, %rdi
decq %r10
jne 0x1c3092
movq 0x8(%rsp), %r8
addq %r8, %rsi
leaq (,%rbp,8), %rdi
addq %rbp, %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rbp, %rdi
incq %rbp
movupd (%rsi), %xmm1
movupd 0xc(%rsi), %xmm2
movupd %xmm2, 0xc(%r8,%rdi)
movupd %xmm1, (%r8,%rdi)
incq %rdx
cmpq %rcx, %rdx
jne 0x1c305b
movq 0x18(%rsp), %rax
movq %rbp, 0x8(%rax)
movq 0x10(%rsp), %r14
movq %rbp, (%r14)
cmpq %rbp, 0x8(%r14)
jae 0x1c3185
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 %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
testq %rbp, %rbp
movq 0x48(%rsp), %rbx
jle 0x1c343a
movq 0x10(%r14), %rdi
movq 0x8(%rsp), %rax
addq $0x10, %rax
xorl %ecx, %ecx
movl (%rax), %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%rcx,8)
incq %rcx
addq $0x1c, %rax
cmpq %rcx, %rbp
jne 0x1c31a2
movq (%r14), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
xorl %r12d, %r12d
testq %rbp, %rbp
jle 0x1c3290
movl $0x80000000, %eax       # imm = 0x80000000
xorl %ecx, %ecx
movq 0x10(%r14), %rdx
movl (%rdx,%rcx,8), %esi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movq 0x8(%rsp), %rdi
movl 0x10(%rdi,%rdx), %esi
cmpl 0x14(%rdi,%rdx), %esi
jae 0x1c3284
addq %rdi, %rdx
leaq (%r12,%r12,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r12, %rdi
leaq (%rsi,%rsi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rsi, %r8
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x50(%rsp), %r9
movupd (%r9,%r8), %xmm0
movupd 0xc(%r9,%r8), %xmm1
movupd %xmm1, 0xc(%r9,%rdi)
movupd %xmm0, (%r9,%rdi)
orb $-0x80, 0xf(%r9,%rdi)
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movl 0x18(%r9,%rdi), %r11d
andl %eax, %r11d
cmpq $0x1, %rbp
leal 0x40000000(%r11,%r10), %r10d
movl %r12d, %r11d
cmovel %ebx, %r11d
movl %r10d, 0x18(%r9,%rdi)
movl %r11d, (%r9,%rdi)
movq %r12, %r10
incq %r12
incq %rsi
movl 0x14(%rdx), %r11d
addq $0x1c, %r9
cmpq %r11, %rsi
jb 0x1c3223
incq %rcx
cmpq %rbp, %rcx
jne 0x1c31da
cmpq $0x1, %rbp
je 0x1c344d
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rsi
movq 0x50(%rsp), %r14
addq %r14, %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x19e2b4
testq %r12, %r12
jle 0x1c332e
movq %r12, %rbx
negq %rbx
leaq 0x1c(%r14), %r13
movl $0x1, %r15d
xorl %edi, %edi
movq %rbx, %rax
addq %r15, %rax
je 0x1c32ef
movl 0x8(%r13), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r14,%rcx), %eax
jne 0x1c32ef
movq %rdi, %rax
jmp 0x1c3317
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1c3317
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r14, %rdi
addq %rax, %rdi
movq %r13, %rsi
callq 0x19e55c
movq %r15, %rax
leaq (%rbx,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r13
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1c32cc
movq 0x10(%rsp), %rbx
movq %r12, (%rbx)
cmpq %r12, 0x8(%rbx)
jae 0x1c338f
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 %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
testq %r12, %r12
movq 0x48(%rsp), %r11
jle 0x1c344d
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movq %r14, %rdx
movl (%rdx), %esi
movq %rcx, (%rax,%rsi,8)
incq %rcx
addq $0x1c, %rdx
cmpq %rcx, %r12
jne 0x1c33a6
testq %r12, %r12
jle 0x1c344d
movq 0x10(%rbx), %rsi
xorl %eax, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq (%rsi,%rax,8), %rdi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r9
addq %rdi, %r9
movl 0x18(%r14,%r9), %r8d
movq %r8, %r10
andq %rcx, %r10
cmpq %rcx, %r10
je 0x1c3413
leaq (%r14,%r9), %rdi
addq $0x18, %rdi
movl (%rsi,%r10,8), %esi
andl %edx, %esi
andl $0xc0000000, %r8d       # imm = 0xC0000000
orl %esi, %r8d
movl %r8d, (%rdi)
movq 0x10(%rbx), %rsi
movq (%rsi,%rax,8), %rdi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
movl %r11d, (%r14,%r8)
incq %rax
cmpq %rax, %r12
jne 0x1c33d1
jmp 0x1c344d
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, (%r14)
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
xorl %r12d, %r12d
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq