// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: sort_hap_chain(hap_overlaps_list*)
void sort_hap_chain(hap_overlaps_list* all_ovlp)
{
    hap_overlaps *x = NULL, *p = NULL;
    uint32_t v, i, k, uId;    
    long long ovlp, xLen, pLen;
    kvec_t(hap_overlaps) pri; kv_init(pri);
    kvec_t(hap_overlaps) alt; kv_init(alt);

    for (v = 0; v < all_ovlp->num; v++)
    {
        uId = v;
        qsort(all_ovlp->x[uId].a.a, all_ovlp->x[uId].a.n, sizeof(hap_overlaps), cmp_chain_score);
        pri.n = alt.n = 0;        
        for (i = 0; i < all_ovlp->x[uId].a.n; i++)
        {
            x = &(all_ovlp->x[uId].a.a[i]);
            xLen = x->x_end_pos - x->x_beg_pos;
            for (k = 0; k < pri.n; k++)
            {
                p = &(pri.a[k]);
                pLen = p->x_end_pos - p->x_beg_pos;
                ovlp = get_ovlp_len(x->x_beg_pos, x->x_end_pos-1, p->x_beg_pos, p->x_end_pos-1);
                if(ovlp == 0) continue;
                if(ovlp >= (MIN(xLen, pLen))*0.5) break;
            }

            if(k < pri.n)
            {
                x->xUid = k;
                kv_push(hap_overlaps, alt, *x);
            }
            else
            {
                kv_push(hap_overlaps, pri, *x);
            } 
        }

    }

    kv_destroy(pri); kv_destroy(alt);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
xorl %ecx, %ecx
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x8(%rsp)
movq %rdi, (%rsp)
movq %r11, %r15
movl 0x8(%r14), %eax
cmpq %rax, %rcx
jae 0xd22f9
movq (%r14), %rax
movq %rcx, 0x18(%rsp)
imulq $0x18, %rcx, %rbx
movq (%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdi
pushq $0x40
popq %rdx
leaq -0x99(%rip), %rcx       # 0xd20d9
callq 0x23f0
movq %rbx, %rcx
movsd 0x1113c6(%rip), %xmm4   # 0x1e3548
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r15, %r11
movq %rcx, 0x20(%rsp)
movl %r12d, %r12d
movq (%r14), %rax
cmpq %r12, (%rax,%rcx)
jbe 0xd22ec
addq %rcx, %rax
movq 0x10(%rax), %rcx
movq %r12, %rdx
shlq $0x6, %rdx
leaq (%rcx,%rdx), %r15
movl 0x4(%rcx,%rdx), %eax
movl 0x8(%rcx,%rdx), %ecx
movl %ecx, %edx
subl %eax, %edx
decl %ecx
xorl %esi, %esi
movl %esi, %edi
cmpq %rdi, %rbp
jbe 0xd2215
shlq $0x6, %rdi
movl 0x4(%r13,%rdi), %r8d
movl 0x8(%r13,%rdi), %edi
leal -0x1(%rdi), %r9d
cmpl %r9d, %ecx
cmovbl %ecx, %r9d
cmpl %r8d, %eax
movl %r8d, %r10d
cmoval %eax, %r10d
subq %r10, %r9
js 0xd2211
incq %r9
subl %r8d, %edi
cvtsi2sd %r9, %xmm0
cmpl %edi, %edx
cmovbl %edx, %edi
cvtsi2sd %rdi, %xmm1
mulsd %xmm4, %xmm1
ucomisd %xmm1, %xmm0
jae 0xd2269
incl %esi
jmp 0xd21c3
movq 0x10(%rsp), %rax
cmpq %rax, %rbp
jne 0xd2256
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
movq %r14, %rsi
shlq $0x6, %rsi
movq %r13, %rdi
movq %r11, %r13
callq 0x2370
movq %r13, %r11
movsd 0x1112fe(%rip), %xmm4   # 0x1e3548
movq %rax, %r13
movq %r14, 0x10(%rsp)
movq (%rsp), %r14
leaq 0x1(%rbp), %rcx
shlq $0x6, %rbp
addq %r13, %rbp
movq %rbp, %rax
movq %rcx, %rbp
jmp 0xd22b9
movl %esi, 0x24(%r15)
movq 0x8(%rsp), %rax
cmpq %rax, %rbx
jne 0xd22a8
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
movq %r14, %rsi
shlq $0x6, %rsi
movq %r11, %rdi
callq 0x2370
movsd 0x1112ac(%rip), %xmm4   # 0x1e3548
movq %rax, %r11
movq %r14, 0x8(%rsp)
movq (%rsp), %r14
leaq 0x1(%rbx), %rcx
shlq $0x6, %rbx
addq %r11, %rbx
movq %rbx, %rax
movq %rcx, %rbx
movupd (%r15), %xmm0
movupd 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups 0x30(%r15), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movupd %xmm1, 0x10(%rax)
movupd %xmm0, (%rax)
incl %r12d
movq 0x20(%rsp), %rcx
jmp 0xd2191
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0xd2143
movq %r13, %rdi
callq 0x2220
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220