// File: chhylp123[P]hifiasm/hic.cpp
// Function: flter_by_cov(ha_ug_index*, kvec_pe_hit*, long)
void flter_by_cov(ha_ug_index* idx, kvec_pe_hit *hits, int64_t min_dp)
{
    uint64_t i, m, k, l, pos_bits = 64 - idx->uID_bits - 1, uid, pM = ((uint64_t)-1)>>idx->uID_bits, *a = NULL, a_n;
    uint32_t s_uid, s_beg, s_end, e_uid, e_beg, e_end;
    int64_t dp, old_dp, start = 0;
    f_chain_t *t = NULL;
    kvec_t(uint64_t) b; kv_init(b);
    kvec_t(f_chain_t) x; kv_init(x);
    uint64_t *index = NULL; CALLOC(index, idx->ug->u.n);
    for (i = 0; i < hits->a.n; i++)
    {
        interpr_hit(idx, hits->a.a[i].s, hits->a.a[i].len>>32, &s_uid, &s_beg, &s_end);
        m = s_uid; m <<= pos_bits; m += s_beg; m <<= 1; kv_push(uint64_t, b, m);
        m = s_uid; m <<= pos_bits; m += s_end; m <<= 1; m += 1; kv_push(uint64_t, b, m);


        interpr_hit(idx, hits->a.a[i].e, (uint32_t)hits->a.a[i].len, &e_uid, &e_beg, &e_end);
        m = e_uid; m <<= pos_bits; m += e_beg; m <<= 1; kv_push(uint64_t, b, m);
        m = e_uid; m <<= pos_bits; m += e_end; m <<= 1; m += 1; kv_push(uint64_t, b, m);
    }
    radix_sort_hc64(b.a, b.a + b.n);
    for (k = 1, l = 0; k <= b.n; ++k) 
    {
        if (k == b.n || (b.a[k]>>(64 - idx->uID_bits)) != (b.a[l]>>(64 - idx->uID_bits)))
        {
            uid = (b.a[l]>>(64 - idx->uID_bits));
            a = b.a + l; a_n = k - l; index[uid] = x.n;
            for (i = 0, dp = 0, start = 0; i < a_n; ++i) 
            {
                old_dp = dp;
                ///if a[j] is qe
                if (a[i]&1) --dp;
                else ++dp;
    
                if (old_dp < min_dp && dp >= min_dp) ///old_dp < dp, b.a[j] is qs
                { 
                    ///case 2, a[j] is qs
                    start = (a[i]&pM)>>1;
                } 
                else if (old_dp >= min_dp && dp < min_dp) ///old_dp > min_dp, b.a[j] is qe
                {
                    kv_pushp(f_chain_t, x, &t);
                    t->s = start; t->e = (a[i]&pM)>>1;
                }
            }

            index[uid] |= ((uint64_t)(x.n - index[uid]))<<32;
            l = k;
        }
    }
    



    for (i = m = 0; i < hits->a.n; i++)
    {
        interpr_hit(idx, hits->a.a[i].s, hits->a.a[i].len>>32, &s_uid, &s_beg, &s_end);
        if(!check_ovlp(x.a+((uint32_t)index[s_uid]), index[s_uid]>>32, s_beg, s_end)) continue;

        interpr_hit(idx, hits->a.a[i].e, (uint32_t)hits->a.a[i].len, &e_uid, &e_beg, &e_end);
        if(!check_ovlp(x.a+((uint32_t)index[e_uid]), index[e_uid]>>32, e_beg, e_end)) continue;
        hits->a.a[m] = hits->a.a[i];
        m++;
    }

    fprintf(stderr, "[M::%s::] # old Hi-C pairs: %lu, # new Hi-C pairs: %lu\n", 
                                                                            __func__, hits->a.n, m);
    hits->a.n = m;

    kv_destroy(b); kv_destroy(x); free(index);
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rcx
movl $0x40, %eax
subq 0x18(%rcx), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq $-0x1, %rax
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, -0xd0(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2071f5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
leaq -0x64(%rbp), %rcx
leaq -0x68(%rbp), %r8
leaq -0x6c(%rbp), %r9
callq 0x201880
movl -0x64(%rbp), %eax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
shlq %cl, %rax
movq %rax, -0x28(%rbp)
movl -0x68(%rbp), %eax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shlq %rax
movq %rax, -0x28(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x206f3c
cmpq $0x0, -0xa8(%rbp)
je 0x206f02
movq -0xa8(%rbp), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x206f10
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x206f10
movq -0xd8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb0(%rbp)
movq %rdx, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
shlq %cl, %rax
movq %rax, -0x28(%rbp)
movl -0x6c(%rbp), %eax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shlq %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x206fff
cmpq $0x0, -0xa8(%rbp)
je 0x206fc5
movq -0xa8(%rbp), %rax
shlq %rax
movq %rax, -0xe0(%rbp)
jmp 0x206fd3
movl $0x2, %eax
movq %rax, -0xe0(%rbp)
jmp 0x206fd3
movq -0xe0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb0(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq -0x70(%rbp), %rcx
leaq -0x74(%rbp), %r8
leaq -0x78(%rbp), %r9
callq 0x201880
movl -0x70(%rbp), %eax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
shlq %cl, %rax
movq %rax, -0x28(%rbp)
movl -0x74(%rbp), %eax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shlq %rax
movq %rax, -0x28(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x2070fb
cmpq $0x0, -0xa8(%rbp)
je 0x2070c1
movq -0xa8(%rbp), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0x2070cf
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0x2070cf
movq -0xe8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb0(%rbp)
movq %rdx, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
shlq %cl, %rax
movq %rax, -0x28(%rbp)
movl -0x78(%rbp), %eax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shlq %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x2071be
cmpq $0x0, -0xa8(%rbp)
je 0x207184
movq -0xa8(%rbp), %rax
shlq %rax
movq %rax, -0xf0(%rbp)
jmp 0x207192
movl $0x2, %eax
movq %rax, -0xf0(%rbp)
jmp 0x207192
movq -0xf0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb0(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x2071e4
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x206e50
movq -0xa0(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xb0(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x1ba0f0
movq $0x1, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0xb0(%rbp), %rax
ja 0x2074c1
movq -0x30(%rbp), %rax
cmpq -0xb0(%rbp), %rax
je 0x20728e
movq -0xa0(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq -0xa0(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
movq -0x8(%rbp), %rsi
movl $0x40, %ecx
subq 0x18(%rsi), %rcx
shrq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
je 0x2074ae
movq -0xa0(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x48(%rbp)
movq -0xa0(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xc8(%rbp), %rdx
movq -0xd0(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x207479
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x207341
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x20734d
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x207383
movq -0x80(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x207383
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq -0x50(%rbp), %rax
shrq %rax
movq %rax, -0x90(%rbp)
jmp 0x207466
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x207464
movq -0x80(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x207464
jmp 0x2073a4
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0x20740b
cmpq $0x0, -0xc0(%rbp)
je 0x2073d1
movq -0xc0(%rbp), %rax
shlq %rax
movq %rax, -0xf8(%rbp)
jmp 0x2073df
movl $0x2, %eax
movq %rax, -0xf8(%rbp)
jmp 0x2073df
movq -0xf8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
shlq $0x4, %rsi
callq 0x2360
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq -0xc8(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
andq -0x50(%rbp), %rcx
shrq %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x207466
jmp 0x207468
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x207304
movq -0xc8(%rbp), %rdx
movq -0xd0(%rbp), %rax
movq -0x48(%rbp), %rcx
subq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq -0xd0(%rbp), %rax
movq -0x48(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x2074b0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x207226
movq $0x0, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x207661
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
leaq -0x64(%rbp), %rcx
leaq -0x68(%rbp), %r8
leaq -0x6c(%rbp), %r9
callq 0x201880
movq -0xb8(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
shlq $0x4, %rax
addq %rax, %rdi
movq -0xd0(%rbp), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movl -0x68(%rbp), %eax
movl %eax, %edx
movl -0x6c(%rbp), %eax
movl %eax, %ecx
callq 0x206bf0
cmpq $0x0, %rax
jne 0x207574
jmp 0x207650
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq -0x70(%rbp), %rcx
leaq -0x74(%rbp), %r8
leaq -0x78(%rbp), %r9
callq 0x201880
movq -0xb8(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
shlq $0x4, %rax
addq %rax, %rdi
movq -0xd0(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movl -0x74(%rbp), %eax
movl %eax, %edx
movl -0x78(%rbp), %eax
movl %eax, %ecx
callq 0x206bf0
cmpq $0x0, %rax
jne 0x207600
jmp 0x207650
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2074d1
movq 0x1bc978(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x28(%rbp), %r8
leaq 0x185cbe(%rip), %rsi    # 0x38d33b
leaq 0x185cef(%rip), %rdx    # 0x38d373
movb $0x0, %al
callq 0x22a0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0xa0(%rbp), %rdi
callq 0x2210
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0xd0(%rbp), %rdi
callq 0x2210
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)