// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x18(%rsp)
movl %r8d, 0xc(%rsp)
movq %rcx, %r14
movq %rdx, 0x28(%rsp)
movq %rsi, %rbx
movl $0x40, %r15d
subq 0x18(%rdi), %r15
cmpq $0x0, 0x18(%rsi)
jne 0x13f37c
movq %rdi, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x13f81d
movq 0x18(%rbx), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x2120
movq %rax, 0x20(%rsp)
testq %r12, %r12
je 0x13f5c4
xorl %ebp, %ebp
xorl %eax, %eax
xorl %edi, %edi
xorl %r10d, %r10d
movq %r14, 0x50(%rsp)
movq %rbx, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movq (%r14), %rcx
movq 0x80(%r14), %rdx
movl (%rcx,%r10,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0x13f5b0
movq 0x28(%rbx), %rcx
movq (%rcx,%r10,8), %r11
testl %r11d, %r11d
je 0x13f5b0
movq %r11, %rcx
shrq $0x1b, %rcx
andq $-0x20, %rcx
addq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rsp)
xorl %ebx, %ebx
movl $0x1, %r13d
movq %r10, 0x38(%rsp)
movq %r11, 0x30(%rsp)
movl %ebx, %edx
cmpl %r11d, %r13d
je 0x13f443
movl %r13d, %ecx
shlq $0x5, %rcx
movq 0x10(%rsp), %r8
movq 0x8(%r8,%rcx), %rsi
addq %rsi, %rsi
movl %r15d, %ecx
shrq %cl, %rsi
movq %rdx, %rcx
shlq $0x5, %rcx
movq 0x8(%r8,%rcx), %r8
addq %r8, %r8
movl %r15d, %ecx
shrq %cl, %r8
cmpq %r8, %rsi
je 0x13f5a4
shlq $0x5, %rdx
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rdx), %r12
addq %r12, %r12
movl %r15d, %ecx
shrq %cl, %r12
cmpq %r10, %r12
je 0x13f5a1
movq (%r14), %rcx
movq 0x80(%r14), %rdx
movl (%rcx,%r12,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0x13f5a1
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %rdx
leaq (%r10,%r10,2), %rsi
shlq $0x4, %rsi
movq (%rdx,%rsi), %rcx
testq %rcx, %rcx
je 0x13f4cd
addq %rsi, %rdx
movq 0x10(%rdx), %rdx
movl $0x1, %esi
xorl %r8d, %r8d
leaq (%r8,%r8,4), %r8
cmpb $0x0, 0x18(%rdx,%r8,8)
js 0x13f4c3
leaq (%rdx,%r8,8), %r9
movl 0x8(%r9), %r8d
cmpq %r8, %r12
je 0x13f584
movl %esi, %r8d
incl %esi
cmpq %r8, %rcx
ja 0x13f4a6
cmpq %rax, %rbp
jne 0x13f4fd
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x38(%rsp), %r10
movq %rax, %rdi
movq %r14, %rax
leaq 0x1(%rbp), %r15
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
shlq $0x4, %rcx
movl %r10d, 0x8(%rdi,%rcx)
movl %r12d, 0x14(%rdi,%rcx)
movl %r13d, %r14d
subl %ebx, %r14d
movl %r14d, 0x18(%rdi,%rcx)
cmpq %rax, %r15
jne 0x13f555
leaq (%rax,%rax), %rbx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbx
movq %rbx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x38(%rsp), %r10
movq %rax, %rdi
movq %rbx, %rax
movq 0x30(%rsp), %r11
addq $0x2, %rbp
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movl %r12d, 0x8(%rdi,%rcx)
movl %r10d, 0x14(%rdi,%rcx)
movl %r14d, 0x18(%rdi,%rcx)
movl %r13d, %ebx
movq 0x50(%rsp), %r14
movq 0x48(%rsp), %r15
jmp 0x13f5a4
cmpq $-0x1, 0x10(%r9)
je 0x13f4cd
movl %r13d, %ecx
subl %ebx, %ecx
movq 0x20(%rsp), %rdx
addl %ecx, (%rdx,%r10,4)
addl %ecx, (%rdx,%r12,4)
movl %r13d, %ebx
incl %r13d
cmpl %r11d, %r13d
jbe 0x13f404
incq %r10
movq 0x40(%rsp), %rbx
cmpq 0x18(%rbx), %r10
jb 0x13f3b3
jmp 0x13f5c8
xorl %edi, %edi
xorl %ebp, %ebp
movq %rbp, 0x10(%rsp)
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movq %rdi, %r13
callq 0x130888
movq %r13, %rdi
movq 0x10(%rsp), %r8
testq %r8, %r8
movsd 0x18(%rsp), %xmm3
movl 0xc(%rsp), %esi
je 0x13f800
leaq 0x18(%rdi), %rbx
xorl %r12d, %r12d
movl $0x1, %r14d
movl $0x1, %eax
xorl %ebp, %ebp
movl %r12d, %r15d
cmpq %rax, %r8
je 0x13f64a
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movl 0x8(%rdi,%rcx), %edx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
cmpl 0x8(%rdi,%rax), %edx
jne 0x13f64a
addq %rdi, %rcx
addq %rdi, %rax
movl 0x14(%rcx), %ecx
cmpl 0x14(%rax), %ecx
je 0x13f79e
movl %r14d, %eax
subl %r12d, %eax
cmpl $0x3, %eax
jae 0x13f7af
xorl %eax, %eax
cmpl %r12d, %r14d
jbe 0x13f67e
movl %r14d, %ecx
leaq (%r15,%r15,2), %rdx
shlq $0x4, %rdx
addq %rbx, %rdx
subq %r15, %rcx
xorl %eax, %eax
addl (%rdx), %eax
addq $0x30, %rdx
decq %rcx
jne 0x13f673
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movl %ebp, %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
leaq (%rdi,%rdx), %r15
movupd (%rdi,%rcx), %xmm0
movupd 0x10(%rdi,%rcx), %xmm1
movups 0x20(%rdi,%rcx), %xmm2
movupd %xmm1, 0x10(%rdi,%rdx)
movups %xmm2, 0x20(%rdi,%rdx)
movupd %xmm0, (%rdi,%rdx)
movl %eax, 0x18(%rdi,%rdx)
cmpl %esi, %eax
ja 0x13f706
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl 0x8(%r15), %eax
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
jae 0x13f706
movl 0x14(%r15), %eax
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
jb 0x13f799
movl 0x8(%r15), %ecx
movl 0x14(%r15), %eax
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq 0x10(%rdx,%rcx), %r12
addq $0x18, %r12
cmpb $0x0, (%r12)
js 0x13f736
cmpl %eax, -0x10(%r12)
je 0x13f73c
addq $0x28, %r12
jmp 0x13f728
cmpq $-0x1, -0x8(%r12)
jne 0x13f7cb
movb (%r12), %al
andb $-0x80, %al
incb %al
movb %al, (%r12)
movl 0x8(%r15), %eax
movl 0x14(%r15), %ecx
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq 0x10(%rdx,%rcx), %r15
addq $0x18, %r15
cmpb $0x0, (%r15)
js 0x13f782
cmpl %eax, -0x10(%r15)
je 0x13f788
addq $0x28, %r15
jmp 0x13f776
cmpq $-0x1, -0x8(%r15)
jne 0x13f7e7
movb (%r15), %al
andb $-0x80, %al
incb %al
movb %al, (%r15)
incl %ebp
movl %r14d, %r12d
incl %r14d
movq %r14, %rax
cmpq %r14, %r8
jae 0x13f616
jmp 0x13f800
callq 0x1cb18
movq %r13, %rdi
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %esi
movsd 0x18(%rsp), %xmm3
jmp 0x13f659
callq 0x1cb34
movq %r13, %rdi
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %esi
movsd 0x18(%rsp), %xmm3
jmp 0x13f748
callq 0x1cb50
movq %r13, %rdi
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %esi
movsd 0x18(%rsp), %xmm3
jmp 0x13f78f
callq 0x2220
movq 0x20(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220