// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
cmpl $0x0, 0x8(%rdi)
je 0x111de4
movq %rdi, %r13
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r11d, %r11d
xorl %r12d, %r12d
movq %rdi, 0x20(%rsp)
movq %r11, %rbp
movq (%r13), %rax
leaq (,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rbx
movq (%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdi
movl $0x40, %edx
leaq -0xa0(%rip), %rcx       # 0x111b7e
callq 0x23f0
movq (%r13), %rax
cmpq $0x0, (%rax,%rbx)
movq %r14, 0x28(%rsp)
je 0x111dca
xorl %ecx, %ecx
leaq (%r14,%r14,2), %r14
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movsd 0x11389d(%rip), %xmm4   # 0x2254f0
movq %rbp, %r11
movq %r14, 0x30(%rsp)
movq 0x10(%rax,%r14,8), %rdx
shlq $0x6, %rcx
leaq (%rdx,%rcx), %rbp
testq %rbx, %rbx
je 0x111cd6
movl 0x4(%rdx,%rcx), %eax
movl 0x8(%rdx,%rcx), %ecx
movl %ecx, %edx
subl %eax, %edx
decl %ecx
xorl %edi, %edi
xorl %esi, %esi
shlq $0x6, %rdi
movl 0x4(%r12,%rdi), %r8d
movl 0x8(%r12,%rdi), %edi
leal -0x1(%rdi), %r9d
cmpq %r9, %rcx
cmovlq %rcx, %r9
cmpq %r8, %rax
movq %r8, %r10
cmovgq %rax, %r10
subq %r10, %r9
js 0x111ccc
incq %r9
subl %r8d, %edi
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
cmpl %edi, %edx
cmovbl %edx, %edi
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
mulsd %xmm4, %xmm1
ucomisd %xmm1, %xmm0
jae 0x111d20
incl %esi
movq %rsi, %rdi
cmpq %rsi, %rbx
ja 0x111c7f
cmpq %r11, %rbx
jne 0x111d0d
leaq (%r11,%r11), %r13
testq %r11, %r11
movl $0x2, %eax
cmoveq %rax, %r13
movq %r13, %rsi
shlq $0x6, %rsi
movq %r12, %rdi
callq 0x2370
movsd 0x1137ee(%rip), %xmm4   # 0x2254f0
movq %rax, %r12
movq %r13, %r11
movq 0x20(%rsp), %r13
leaq 0x1(%rbx), %rcx
shlq $0x6, %rbx
addq %r12, %rbx
movq %rbx, %rax
movq %rcx, %rbx
jmp 0x111d91
movl %esi, 0x24(%rbp)
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0x111d6f
leaq (%rax,%rax), %r13
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r13
movq %r13, %rsi
shlq $0x6, %rsi
movq 0x8(%rsp), %rdi
movq %r11, %r14
callq 0x2370
movq %r14, %r11
movq 0x30(%rsp), %r14
movsd 0x11378d(%rip), %xmm4   # 0x2254f0
movq %r13, 0x18(%rsp)
movq 0x20(%rsp), %r13
jmp 0x111d74
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rsi
leaq 0x1(%rsi), %rcx
shlq $0x6, %rsi
movq %rax, 0x8(%rsp)
addq %rax, %rsi
movq %rsi, %rax
movq %rcx, 0x10(%rsp)
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 (%r13), %rax
movq %r15, %rcx
cmpq %r15, (%rax,%r14,8)
ja 0x111c5b
jmp 0x111dcd
movq %rbp, %r11
movq 0x28(%rsp), %r14
incq %r14
movl 0x8(%r13), %eax
cmpq %rax, %r14
jb 0x111bf6
jmp 0x111df0
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq %r12, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220