// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, 0x60(%rsp)
movq (%rdi), %r15
movq %rdi, 0x38(%rsp)
movq 0x8(%rdi), %rax
movq %rax, %rcx
sarq %rcx
testq %rcx, %rcx
jle 0x1b9091
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
addq %r15, %rdx
addq $-0x1c, %rdx
movl $0x1, %ebp
movq %r15, %r14
movq %rcx, %r9
movq (%r14), %rdi
movl 0x8(%r14), %r10d
movl 0xc(%r14), %r11d
movl 0xc(%r14), %ebx
movl %ebx, 0x1c(%rsp)
movq 0x10(%r14), %rbx
movq %rbx, 0x20(%rsp)
movl 0x18(%r14), %ebx
movl %ebx, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movl %r11d, 0x1c(%rsp)
movupd (%rdx), %xmm0
movupd 0xc(%rdx), %xmm1
movupd %xmm1, 0xc(%r14)
movupd %xmm0, (%r14)
movl 0x1c(%rsp), %edi
movl %edi, 0xc(%rdx)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rdx)
movl 0x28(%rsp), %edi
movl %edi, 0x18(%rdx)
movq 0x10(%rsp), %rdi
movq %rdi, (%rdx)
movl 0x18(%rsp), %edi
movl %edi, 0x8(%rdx)
movl 0x1c(%rsp), %edi
movl %edi, 0xc(%rdx)
movl %ebp, 0xc(%rdx)
movl %ebp, 0xc(%r14)
addq $0x1c, %r14
addq $-0x1c, %rdx
decq %r9
jne 0x1b9003
jmp 0x1b9093
xorl %ecx, %ecx
movq %rsi, 0x40(%rsp)
movq %r8, 0x8(%rsp)
testb $0x1, %al
je 0x1b90b5
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl $0x1, 0xc(%r15,%rdx)
testq %rax, %rax
movq %r15, (%rsp)
jle 0x1b929f
leaq -0x10(%r15), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x28(%r15), %rcx
movq %rcx, 0x48(%rsp)
movl $0x1, %ecx
movq %rcx, 0x50(%rsp)
xorl %r8d, %r8d
movsd 0x7a7a7(%rip), %xmm0    # 0x233890
movsd 0x6c3bf(%rip), %xmm1    # 0x2254b0
xorl %r9d, %r9d
leaq (%r9,%r9,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r9, %rdx
cmpl $0x0, 0xc(%r15,%rdx)
je 0x1b927e
movq (%rsp), %rcx
leaq (%rcx,%rdx), %r10
movl 0x4(%rcx,%rdx), %r11d
movl 0x8(%rcx,%rdx), %ebp
movl %ebp, %r12d
subl %r11d, %r12d
testq %r9, %r9
je 0x1b91d2
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rdx
movl -0x8(%r15), %edi
movl -0x4(%r15), %r13d
cmpl %r13d, %ebp
movl %r13d, %r14d
cmovbl %ebp, %r14d
cmpl %edi, %r11d
movl %edi, %ebx
cmoval %r11d, %ebx
subl %ebx, %r14d
cmovbl %r8d, %r14d
jbe 0x1b91c1
movl -0xc(%r15), %ebx
movl (%r10), %ecx
cmpl %ecx, %ebx
movl %ecx, %esi
cmovbl %ebx, %esi
cmoval %ebx, %ecx
subl %edi, %r13d
cmpl %r12d, %r13d
cmovbel %r12d, %r13d
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x1b91d2
cmpl $0x9, %r14d
jb 0x1b91c1
ucomisd %xmm3, %xmm2
jbe 0x1b91c1
movl %r14d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl %r13d, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1b91c1
movl %r8d, (%r15)
movl %r8d, 0xc(%r10)
decq %rdx
addq $-0x1c, %r15
cmpq $0x1, %rdx
jg 0x1b9135
leaq 0x1(%r9), %r15
cmpq %rax, %r15
jge 0x1b927e
movq 0x48(%rsp), %rdx
movl -0x8(%rdx), %ebx
movl -0x4(%rdx), %r13d
cmpl %r13d, %ebp
movl %r13d, %r14d
cmovbl %ebp, %r14d
cmpl %ebx, %r11d
movl %ebx, %ecx
cmoval %r11d, %ecx
subl %ecx, %r14d
cmovbl %r8d, %r14d
jbe 0x1b926e
movl -0xc(%rdx), %ecx
movl (%r10), %esi
cmpl %esi, %ecx
movl %esi, %edi
cmovbl %ecx, %edi
cmoval %ecx, %esi
subl %ebx, %r13d
cmpl %r12d, %r13d
cmovbel %r12d, %r13d
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x1b927e
cmpl $0x9, %r14d
jb 0x1b926e
ucomisd %xmm3, %xmm2
jbe 0x1b926e
movl %r14d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl %r13d, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1b926e
movl %r8d, (%rdx)
movl %r8d, 0xc(%r10)
incq %r15
addq $0x1c, %rdx
cmpq %rax, %r15
jl 0x1b91e4
incq %r9
incq 0x50(%rsp)
addq $0x1c, 0x58(%rsp)
addq $0x1c, 0x48(%rsp)
cmpq %rax, %r9
movq (%rsp), %r15
jne 0x1b90f4
xorl %ecx, %ecx
testq %rax, %rax
jle 0x1b92e3
movq %r15, %rdx
movq 0x8(%rsp), %r13
cmpl $0x0, 0xc(%rdx)
je 0x1b92d8
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(%r15,%rsi)
movupd %xmm0, (%r15,%rsi)
addq $0x1c, %rdx
decq %rax
jne 0x1b92ae
jmp 0x1b92e8
movq 0x8(%rsp), %r13
movq 0x38(%rsp), %rax
movq %rcx, 0x8(%rax)
xorl %eax, %eax
testq %rcx, %rcx
jle 0x1b93da
leaq 0x8(%r15), %rdx
movsd 0x6b8c8(%rip), %xmm0    # 0x224bd0
xorl %ebp, %ebp
xorl %esi, %esi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %r8
addq %rsi, %r8
testq %rbp, %rbp
jle 0x1b9392
movq (%rsp), %rdi
movl 0x4(%rdi,%r8), %r9d
movl 0x8(%rdi,%r8), %r10d
movl %r10d, %edi
subl %r9d, %edi
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
mulsd %xmm0, %xmm1
movq %rdx, %r11
movq %rbp, %rbx
movl -0x4(%r11), %r14d
movl (%r11), %edi
cmpl %edi, %r10d
movl %edi, %r15d
cmovbl %r10d, %r15d
cmpl %r14d, %r9d
movl %r14d, %r12d
cmoval %r9d, %r12d
subl %r12d, %r15d
jbe 0x1b9389
cmovbl %eax, %r15d
subl %r14d, %edi
xorps %xmm2, %xmm2
cvtsi2sd %r15, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x1b93c8
ucomisd %xmm1, %xmm2
ja 0x1b93c8
addq $0x1c, %r11
decq %rbx
jne 0x1b9342
movq (%rsp), %r15
addq %r15, %r8
leaq (,%rbp,8), %rdi
addq %rbp, %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rbp, %rdi
incq %rbp
movupd (%r8), %xmm1
movupd 0xc(%r8), %xmm2
movupd %xmm2, 0xc(%r15,%rdi)
movupd %xmm1, (%r15,%rdi)
jmp 0x1b93cc
movq (%rsp), %r15
incq %rsi
cmpq %rcx, %rsi
jne 0x1b930c
jmp 0x1b93dc
xorl %ebp, %ebp
movq 0x38(%rsp), %rax
movq %rbp, 0x8(%rax)
movq %rbp, (%r13)
cmpq %rbp, 0x8(%r13)
jae 0x1b9441
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(%r13)
movq 0x10(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r13)
testq %rbp, %rbp
movq 0x60(%rsp), %r14
jle 0x1b946e
movq 0x10(%r13), %rax
leaq 0x10(%r15), %rcx
xorl %edx, %edx
movl (%rcx), %esi
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x1c, %rcx
cmpq %rdx, %rbp
jne 0x1b9455
movq (%r13), %rax
movq 0x10(%r13), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
xorl %r12d, %r12d
testq %rbp, %rbp
jle 0x1b954b
movl $0x40000000, %eax       # imm = 0x40000000
xorl %ecx, %ecx
movq 0x10(%r13), %rdx
movl (%rdx,%rcx,8), %esi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movl 0x10(%r15,%rdx), %esi
cmpl 0x14(%r15,%rdx), %esi
jae 0x1b953f
addq %r15, %rdx
leaq (%r12,%r12,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r12, %rdi
addq $0x18, %rdi
leaq (%rsi,%rsi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rsi, %r8
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x40(%rsp), %r9
movupd (%r9,%r8), %xmm0
movupd 0xc(%r9,%r8), %xmm1
movupd %xmm1, -0xc(%r9,%rdi)
movupd %xmm0, -0x18(%r9,%rdi)
movl (%r9,%rdi), %r11d
orb $-0x80, -0x9(%r9,%rdi)
orl %eax, %r11d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
andl $0xc0000000, %r11d      # imm = 0xC0000000
orl %r10d, %r11d
cmpq $0x1, %rbp
movl %r12d, %r10d
cmovel %r14d, %r10d
movl %r11d, (%r9,%rdi)
movl %r10d, -0x18(%r9,%rdi)
movq %r12, %r10
incq %r12
incq %rsi
movl 0x14(%rdx), %r11d
addq $0x1c, %r9
cmpq %r11, %rsi
jb 0x1b94dc
incq %rcx
cmpq %rbp, %rcx
jne 0x1b9492
cmpq $0x2, %rbp
movq 0x40(%rsp), %rbp
jl 0x1b96d5
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rbp, %rsi
addq %r12, %rsi
movq %rbp, %rdi
callq 0x195511
testq %r12, %r12
jle 0x1b95e8
movq %r12, %rbx
negq %rbx
leaq 0x1c(%rbp), %r13
movl $0x1, %r15d
xorl %edi, %edi
movq %rbx, %rax
addq %r15, %rax
je 0x1b95a9
movl 0x8(%r13), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%rbp,%rcx), %eax
jne 0x1b95a9
movq %rdi, %rax
jmp 0x1b95d1
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1b95d1
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbp, %rdi
addq %rax, %rdi
movq %r13, %rsi
callq 0x1957bd
movq %r15, %rax
leaq (%rbx,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r13
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1b9587
movq 0x8(%rsp), %rbx
movq %r12, (%rbx)
cmpq %r12, 0x8(%rbx)
jae 0x1b9649
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
jle 0x1b9669
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movq %rbp, %rdx
movl (%rdx), %esi
movq %rcx, (%rax,%rsi,8)
incq %rcx
addq $0x1c, %rdx
cmpq %rcx, %r12
jne 0x1b9657
testq %r12, %r12
jle 0x1b96d5
xorl %eax, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq 0x10(%rbx), %rdi
movq (%rdi,%rax,8), %rsi
leaq (%rsi,%rsi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rsi, %r8
movl 0x18(%rbp,%r8), %esi
movq %rsi, %r9
andq %rcx, %r9
cmpq %rcx, %r9
je 0x1b96b5
addq %rbp, %r8
addq $0x18, %r8
movl (%rdi,%r9,8), %edi
andl %edx, %edi
andl $0xc0000000, %esi       # imm = 0xC0000000
orl %edi, %esi
movl %esi, (%r8)
movq 0x10(%rbx), %rsi
movq (%rsi,%rax,8), %rsi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
movl %r14d, (%rbp,%rdi)
incq %rax
cmpq %rax, %r12
jne 0x1b967a
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq