// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x182fbf
movl -0x1c(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movl $0x40, %edx
leaq -0x181(%rip), %rcx      # 0x182bd0
callq 0x23e0
movq $0x0, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
movl -0x28(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x182faf
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq -0x58(%rbp), %rax
jae 0x182e94
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
callq 0x182c20
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x182e3b
jmp 0x182e86
cvtsi2sdq -0x30(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jg 0x182e5a
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x182e62
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movsd -0x78(%rbp), %xmm0
movq -0x80(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x1ff3f8(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x182e84
jmp 0x182e94
jmp 0x182e86
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x182dc8
movl -0x24(%rbp), %eax
cmpq -0x58(%rbp), %rax
jae 0x182f24
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x182efa
cmpq $0x0, -0x68(%rbp)
je 0x182ecc
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0x182eda
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0x182eda
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq $0x6, %rsi
callq 0x2360
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x70(%rbp)
shlq $0x6, %rax
addq %rax, %rdi
movl $0x40, %edx
callq 0x2180
jmp 0x182f9f
jmp 0x182f26
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x182f75
cmpq $0x0, -0x50(%rbp)
je 0x182f47
movq -0x50(%rbp), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x182f55
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x182f55
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
shlq $0x6, %rsi
callq 0x2360
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
shlq $0x6, %rax
addq %rax, %rdi
movl $0x40, %edx
callq 0x2180
jmp 0x182f9f
jmp 0x182fa1
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x182d6d
jmp 0x182fb1
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x182d06
movq -0x48(%rbp), %rdi
callq 0x2210
movq -0x60(%rbp), %rdi
callq 0x2210
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)