// File: chhylp123[P]hifiasm/hic.cpp
// Function: filter_disconnect_edges(ha_ug_index*, kvec_pe_hit*, hc_links*, bubble_type*, unsigned int, double)
void filter_disconnect_edges(ha_ug_index* idx, kvec_pe_hit *hits, hc_links *link, bubble_type *bub, uint32_t thres, double rate)
{
    uint32_t k, l, i, m, h_occ, *occ = NULL;
    uint64_t shif = 64 - idx->uID_bits, qn, tn, u_dis;
    pe_hit *h_a = NULL;
    hc_linkeage *t = NULL;
    u_trans_t *p = NULL;
    hc_edge *e = NULL;
    kv_u_trans_t k_trans; 
    kv_init(k_trans);
    
    if(hits->idx.n == 0) idx_hc_links(hits, idx, bub);
    CALLOC(occ, hits->idx.n);
    for (qn = 0; qn < hits->idx.n; qn++)
    {
        if(IF_HOM(qn, *bub)) continue;
        h_a = hits->a.a + (hits->idx.a[qn]>>32);
        h_occ = (uint32_t)(hits->idx.a[qn]);

        for (k = 1, l = 0; k <= h_occ; ++k) ///same qn
        {
            if (k == h_occ || ((h_a[k].e<<1)>>shif) != ((h_a[l].e<<1)>>shif)) //same qn and tn
            {
                tn = ((h_a[l].e<<1)>>shif);
                if(!IF_HOM(tn, *bub) && tn != qn)
                {
                    t = &(link->a.a[qn]);
                    for (i = 0, u_dis = (uint64_t)-1; i < t->e.n; i++)
                    {
                        if(t->e.a[i].del || t->e.a[i].uID != tn) continue;
                        u_dis = (t->e.a[i].dis ==(uint64_t)-1? (uint64_t)-1 : t->e.a[i].dis>>3);
                        break;
                    }
                    
                    if(u_dis == (uint64_t)-1)
                    {
                        kv_pushp(u_trans_t, k_trans, &p);
                        p->qn = qn; p->tn = tn; p->occ = (k-l);
                        kv_pushp(u_trans_t, k_trans, &p);
                        p->qn = tn; p->tn = qn; p->occ = (k-l);
                    }
                    else
                    {
                        occ[qn] += (k-l); occ[tn] += (k-l);
                    }
                }
                l = k;
            }
        }
    }


    radix_sort_u_trans_m(k_trans.a, k_trans.a + k_trans.n);

    for (k = 1, l = 0, m = 0; k <= k_trans.n; ++k)
    {
        if (k == k_trans.n || k_trans.a[l].qn != k_trans.a[k].qn || k_trans.a[l].tn != k_trans.a[k].tn) //same qn and tn
        {
            if(k - l > 2) fprintf(stderr, "ERROR-3\n");
            for (i = l, h_occ = 0; i < k; i++)
            {
                h_occ += k_trans.a[i].occ;
            }

            k_trans.a[m] = k_trans.a[l];
            // k_trans.a[m].occ = ((uint32_t)-1) - h_occ;
            k_trans.a[m].occ = h_occ;

            if(h_occ > thres || h_occ >= (occ[k_trans.a[m].qn]*rate) || h_occ >= (occ[k_trans.a[m].tn]*rate))
            {
                e = get_hc_edge(link, k_trans.a[m].qn, k_trans.a[m].tn, 0);
                if(e->dis != (uint64_t)-1) fprintf(stderr, "ERROR-3-0\n");
                e->is_cc = 1;

                e = get_hc_edge(link, k_trans.a[m].tn, k_trans.a[m].qn, 0);
                if(e->dis != (uint64_t)-1) fprintf(stderr, "ERROR-3-0\n");
                e->is_cc = 1;
            }
            m++;
            l = k;
        }
    }
    k_trans.n = m;

    kv_destroy(k_trans); free(occ);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movsd %xmm0, -0x30(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rcx
movl $0x40, %eax
subq 0x18(%rcx), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x1d24d0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1d2bc0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x2140
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x1d28ca
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jbe 0x1d2527
jmp 0x1d28b9
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x44(%rbp)
movl $0x1, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
ja 0x1d28b7
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
je 0x1d25c7
movq -0x78(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x58(%rbp), %rcx
shrq %cl, %rax
movq -0x78(%rbp), %rcx
movl -0x38(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rdx
shlq %rdx
movq -0x58(%rbp), %rcx
shrq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
je 0x1d28a7
movq -0x78(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x58(%rbp), %rcx
shrq %cl, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
ja 0x1d28a1
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x1d28a1
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x3c(%rbp)
movq $-0x1, -0x70(%rbp)
movl -0x3c(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1d26ec
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
jne 0x1d2685
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x68(%rbp), %rax
je 0x1d2687
jmp 0x1d26de
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $-0x1, 0x10(%rax)
jne 0x1d26b0
movq $-0x1, %rax
movq %rax, -0xc8(%rbp)
jmp 0x1d26d1
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
shrq $0x3, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x1d26ec
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1d263e
cmpq $-0x1, -0x70(%rbp)
jne 0x1d2877
jmp 0x1d26f9
movq -0xc0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jne 0x1d275d
cmpq $0x0, -0xb8(%rbp)
je 0x1d2726
movq -0xb8(%rbp), %rax
shlq %rax
movq %rax, -0xd0(%rbp)
jmp 0x1d2734
movl $0x2, %eax
movq %rax, -0xd0(%rbp)
jmp 0x1d2734
movq -0xd0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
imulq $0x30, -0xb8(%rbp), %rsi
callq 0x2360
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xc0(%rbp)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x34(%rbp), %ecx
subl -0x38(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0xc0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jne 0x1d281b
cmpq $0x0, -0xb8(%rbp)
je 0x1d27e4
movq -0xb8(%rbp), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x1d27f2
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x1d27f2
movq -0xd8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
imulq $0x30, -0xb8(%rbp), %rsi
callq 0x2360
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xc0(%rbp)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x34(%rbp), %ecx
subl -0x38(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x1d289f
movl -0x34(%rbp), %edx
subl -0x38(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %edx
subl -0x38(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x1d28a1
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x1d28a9
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1d256f
jmp 0x1d28b9
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1d24ee
movq -0xb0(%rbp), %rdi
movq -0xb0(%rbp), %rsi
imulq $0x30, -0xc0(%rbp), %rax
addq %rax, %rsi
callq 0x1b8260
movl $0x1, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x34(%rbp), %eax
cmpq -0xc0(%rbp), %rax
ja 0x1d2b98
movl -0x34(%rbp), %eax
cmpq -0xc0(%rbp), %rax
je 0x1d2971
movq -0xb0(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0xb0(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x1d2971
movq -0xb0(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0xb0(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
je 0x1d2b88
movl -0x34(%rbp), %eax
subl -0x38(%rbp), %eax
cmpl $0x2, %eax
jbe 0x1d2994
movq 0x1f165d(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1b52d1(%rip), %rsi    # 0x387c5e
movb $0x0, %al
callq 0x22a0
movl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1d29ce
movq -0xb0(%rbp), %rax
movl -0x3c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1d29a1
movq -0xb0(%rbp), %rsi
movl -0x38(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movq -0xb0(%rbp), %rdi
movl -0x40(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x30, %edx
callq 0x2180
movl -0x44(%rbp), %ecx
movq -0xb0(%rbp), %rax
movl -0x40(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movl -0x44(%rbp), %eax
cmpl -0x24(%rbp), %eax
ja 0x1d2a87
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm0
movq -0x50(%rbp), %rax
movq -0xb0(%rbp), %rcx
movl -0x40(%rbp), %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movl 0x8(%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm1
mulsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x1d2a87
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm0
movq -0x50(%rbp), %rax
movq -0xb0(%rbp), %rcx
movl -0x40(%rbp), %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movl 0x14(%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm1
mulsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1d2b79
movq -0x18(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %esi
movq -0xb0(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1d09c0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq $-0x1, 0x10(%rax)
je 0x1d2aed
movq 0x1f1504(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1b98d5(%rip), %rsi    # 0x38c3bb
movb $0x0, %al
callq 0x22a0
movq -0x90(%rbp), %rax
movb 0x18(%rax), %cl
andb $-0x80, %cl
orb $0x1, %cl
movb %cl, 0x18(%rax)
movq -0x18(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, %esi
movq -0xb0(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1d09c0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq $-0x1, 0x10(%rax)
je 0x1d2b66
movq 0x1f148b(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1b985c(%rip), %rsi    # 0x38c3bb
movb $0x0, %al
callq 0x22a0
movq -0x90(%rbp), %rax
movb 0x18(%rax), %cl
andb $-0x80, %cl
orb $0x1, %cl
movb %cl, 0x18(%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x1d2b8a
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1d28fd
movl -0x40(%rbp), %eax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x2210
movq -0x50(%rbp), %rdi
callq 0x2210
addq $0xe0, %rsp
popq %rbp
retq