// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x0, 0x8(%rdi)
je 0x11790c
movq (%rdi), %rax
xorl %r14d, %r14d
movq $0x0, 0x10(%rsp)
movq $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
movq %rdi, 0x8(%rsp)
leaq (,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rbx
movq (%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdi
movl $0x40, %edx
leaq -0x9e(%rip), %rcx       # 0x1176d2
callq 0x23f0
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
cmpq $0x0, (%rax,%rbx)
movq %r14, 0x20(%rsp)
je 0x1178f6
xorl %ecx, %ecx
leaq (%r14,%r14,2), %r13
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
movsd 0x119e4d(%rip), %xmm4   # 0x2315f0
movq 0x10(%rax,%r13,8), %rdx
shlq $0x6, %rcx
leaq (%rdx,%rcx), %rbp
testq %rbx, %rbx
je 0x117821
movl 0x4(%rdx,%rcx), %eax
movl 0x8(%rdx,%rcx), %ecx
movl %ecx, %edx
subl %eax, %edx
decl %ecx
xorl %r9d, %r9d
xorl %esi, %esi
shlq $0x6, %r9
movl 0x4(%r12,%r9), %r8d
movl 0x8(%r12,%r9), %r11d
leal -0x1(%r11), %r9d
cmpl %r9d, %ecx
cmovbl %ecx, %r9d
cmpl %r8d, %eax
movl %r8d, %r10d
cmoval %eax, %r10d
subq %r10, %r9
js 0x117817
incq %r9
subl %r8d, %r11d
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
cmpl %r11d, %edx
cmovbl %edx, %r11d
xorps %xmm1, %xmm1
cvtsi2sd %r11, %xmm1
mulsd %xmm4, %xmm1
ucomisd %xmm1, %xmm0
jae 0x11786c
incl %esi
movq %rsi, %r9
cmpq %rsi, %rbx
ja 0x1177c8
cmpq 0x18(%rsp), %rbx
jne 0x117859
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x18(%rsp)
shlq $0x6, %rsi
movq %r12, %rdi
callq 0x2370
movsd 0x119d9f(%rip), %xmm4   # 0x2315f0
movq 0x8(%rsp), %rdi
movq %rax, %r12
leaq 0x1(%rbx), %rcx
shlq $0x6, %rbx
addq %r12, %rbx
movq %rbx, %rax
movq %rcx, %rbx
jmp 0x1178c0
movl %esi, 0x24(%rbp)
cmpq 0x10(%rsp), %r14
jne 0x1178a7
leaq (%r14,%r14), %rsi
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rsp)
shlq $0x6, %rsi
movq (%rsp), %rdi
callq 0x2370
movsd 0x119d50(%rip), %xmm4   # 0x2315f0
movq 0x8(%rsp), %rdi
jmp 0x1178ab
movq (%rsp), %rax
leaq 0x1(%r14), %rcx
shlq $0x6, %r14
movq %rax, (%rsp)
addq %rax, %r14
movq %r14, %rax
movq %rcx, %r14
movupd (%rbp), %xmm0
movupd 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups 0x30(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movupd %xmm1, 0x10(%rax)
movupd %xmm0, (%rax)
incl %r15d
movq (%rdi), %rax
movq %r15, %rcx
cmpq %r15, (%rax,%r13,8)
ja 0x1177a3
movq 0x20(%rsp), %r14
incq %r14
movl 0x8(%rdi), %ecx
cmpq %rcx, %r14
jb 0x11774f
jmp 0x117917
xorl %r12d, %r12d
movq $0x0, (%rsp)
movq %r12, %rdi
callq 0x2220
movq (%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220