// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movl %r8d, 0x34(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %rbx
pushq $0x40
popq %r13
subq 0x18(%rdi), %r13
movq 0x18(%rsi), %rbp
testq %rbp, %rbp
jne 0xfd8db
movq %rdi, %rsi
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
callq 0xfdcd5
movq 0x18(%rbx), %rbp
pushq $0x4
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x28(%rsp)
xorl %r11d, %r11d
xorl %edi, %edi
xorl %eax, %eax
xorl %r9d, %r9d
movq %r12, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
cmpq %rbp, %r11
jae 0xfdae3
movq 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rdx
movl (%rcx,%r11,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0xfdadb
movq 0x28(%rbx), %rcx
movq (%rcx,%r11,8), %rdx
movq %rdx, 0x10(%rsp)
shrq $0x1b, %rdx
andq $-0x20, %rdx
addq 0x10(%rbx), %rdx
movq %rdx, 0x8(%rsp)
xorl %r14d, %r14d
pushq $0x1
popq %r15
movq 0x18(%rsp), %rbp
movq %r11, 0x38(%rsp)
cmpl 0x10(%rsp), %r15d
ja 0xfdad2
movl %r14d, %edx
jne 0xfd984
shlq $0x5, %rdx
movq 0x8(%rsp), %rcx
movq 0x8(%rcx,%rdx), %rbx
addq %rbx, %rbx
movl %r13d, %ecx
shrq %cl, %rbx
jmp 0xfd9b6
movl %r15d, %ecx
shlq $0x5, %rcx
movq 0x8(%rsp), %r8
movq 0x8(%r8,%rcx), %rsi
addq %rsi, %rsi
movl %r13d, %ecx
shrq %cl, %rsi
shlq $0x5, %rdx
movq 0x8(%r8,%rdx), %rbx
addq %rbx, %rbx
shrq %cl, %rbx
cmpq %rbx, %rsi
je 0xfdaca
cmpq %r11, %rbx
je 0xfda24
movq (%rbp), %rcx
movq 0x80(%rbp), %rdx
movl (%rcx,%rbx,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0xfda24
movq 0x10(%r12), %rdx
imulq $0x30, %r11, %rsi
leaq (%rdx,%rsi), %rcx
movq (%rdx,%rsi), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rdx
jbe 0xfda2c
movq 0x10(%rcx), %r10
imulq $0x28, %rsi, %r8
cmpb $0x0, 0x18(%r10,%r8)
js 0xfda07
addq %r8, %r10
movl 0x8(%r10), %r8d
cmpq %r8, %rbx
je 0xfda0b
incl %esi
jmp 0xfd9e4
cmpq $-0x1, 0x10(%r10)
je 0xfda2c
movl %r15d, %ecx
subl %r14d, %ecx
movq 0x28(%rsp), %rdx
addl %ecx, (%rdx,%r11,4)
addl %ecx, (%rdx,%rbx,4)
movl %r15d, %r14d
jmp 0xfdaca
cmpq %rax, %r9
jne 0xfda59
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
imulq $0x30, %r12, %rsi
movq %r9, %r13
callq 0x2370
movq 0x38(%rsp), %r11
movq %r13, %r9
movq %rax, %rdi
movq %r12, %rax
leaq 0x1(%r9), %r12
imulq $0x30, %r9, %rcx
movl %r11d, 0x8(%rdi,%rcx)
movl %ebx, 0x14(%rdi,%rcx)
movl %r15d, %r13d
subl %r14d, %r13d
movl %r13d, 0x18(%rdi,%rcx)
cmpq %rax, %r12
jne 0xfdaa7
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
imulq $0x30, %r14, %rsi
movq %r9, %rbp
callq 0x2370
movq 0x38(%rsp), %r11
movq %rbp, %r9
movq 0x18(%rsp), %rbp
movq %rax, %rdi
movq %r14, %rax
addq $0x2, %r9
imulq $0x30, %r12, %rcx
movl %ebx, 0x8(%rdi,%rcx)
movl %r11d, 0x14(%rdi,%rcx)
movl %r13d, 0x18(%rdi,%rcx)
movl %r15d, %r14d
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
incl %r15d
jmp 0xfd95b
movq 0x40(%rsp), %rbx
movq 0x18(%rbx), %rbp
incq %r11
jmp 0xfd904
imulq $0x30, %r9, %rsi
addq %rdi, %rsi
movq %r9, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
callq 0xf0272
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
leaq 0x18(%rdi), %r14
pushq $0x1
popq %r15
xorl %r13d, %r13d
xorl %eax, %eax
movsd 0x20(%rsp), %xmm3
movl %r15d, %ebx
cmpq %rbx, %r8
jb 0xfdcb8
movl %eax, %ebp
je 0xfdb4a
imulq $0x30, %rbp, %rcx
movl 0x8(%rdi,%rcx), %esi
imulq $0x30, %rbx, %rdx
cmpl 0x8(%rdi,%rdx), %esi
jne 0xfdb4a
addq %rdi, %rcx
addq %rdi, %rdx
movl 0x14(%rcx), %ecx
cmpl 0x14(%rdx), %ecx
je 0xfdc3d
movl %r15d, %ecx
subl %eax, %ecx
cmpl $0x3, %ecx
jae 0xfdc45
imulq $0x30, %rbp, %rcx
leaq (%r14,%rcx), %rdx
xorl %eax, %eax
cmpq %rbx, %rbp
jae 0xfdb72
addl (%rdx), %eax
incq %rbp
addq $0x30, %rdx
jmp 0xfdb62
movl %r13d, %edx
imulq $0x30, %rdx, %rdx
leaq (%rdi,%rdx), %rbp
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 0x34(%rsp), %eax
jbe 0xfdbac
movl 0x8(%rbp), %esi
jmp 0xfdbe2
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movl 0x8(%rbp), %esi
movq 0x28(%rsp), %rcx
movl (%rcx,%rsi,4), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
jae 0xfdbe2
movl 0x14(%rbp), %eax
movl (%rcx,%rax,4), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
jb 0xfdc37
movl 0x14(%rbp), %edx
movq %r12, %rdi
xorl %ecx, %ecx
callq 0xfcabc
movq %rax, %rbx
cmpq $-0x1, 0x10(%rax)
jne 0xfdc76
movb 0x18(%rbx), %al
andb $-0x80, %al
incb %al
movb %al, 0x18(%rbx)
movl 0x8(%rbp), %edx
movl 0x14(%rbp), %esi
movq %r12, %rdi
xorl %ecx, %ecx
callq 0xfcabc
movq %rax, %rbx
cmpq $-0x1, 0x10(%rax)
jne 0xfdc97
movb 0x18(%rbx), %al
andb $-0x80, %al
incb %al
movb %al, 0x18(%rbx)
movsd 0x20(%rsp), %xmm3
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
incl %r13d
movl %r15d, %eax
incl %r15d
jmp 0xfdb16
movq 0x155394(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xeb31d(%rip), %rdi     # 0x1e8f73
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
movsd 0x20(%rsp), %xmm3
jmp 0xfdb58
movq 0x155363(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xefa07(%rip), %rdi     # 0x1ed68e
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0xfdbf9
movq 0x155342(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xef9e6(%rip), %rdi     # 0x1ed68e
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0xfdc1d
callq 0x2220
movq 0x28(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220