// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, %r13
movq (%rdi), %rax
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %rbx
pushq $-0x1
popq %rdx
movl %ebx, %ecx
shrq %cl, %rdx
movq %rdx, 0x38(%rsp)
movq (%rax), %rdi
pushq $0x8
popq %rsi
callq 0x2120
movq %rax, 0x20(%rsp)
xorl %edi, %edi
cmpq $0x0, (%r13)
movl $0x0, %r15d
movq %r13, 0x18(%rsp)
je 0x1af74
pushq $0x3f
popq %rax
subq %rbx, %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edi, %edi
xorl %r9d, %r9d
movq %r14, %r15
movq 0x10(%r13), %rax
movq (%rax,%r14,8), %rdx
movq 0x18(%rax,%r14,8), %rbx
shrq $0x20, %rbx
leaq (%rdx,%rdx), %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %r8
subb 0x18(%r8), %cl
shrq %cl, %rax
movq 0x30(%r8), %rcx
andq %rdx, %rcx
movq %rcx, %r14
subq %rbx, %r14
decq %rbx
incq %r14
testq %rdx, %rdx
movl $0x0, %edx
cmovsq %rdx, %rbx
cmovnsq %rcx, %r14
addq %rcx, %rbx
testq %rbx, %rbx
cmovleq %rdx, %rbx
testq %r14, %r14
cmovleq %rdx, %r14
movl %eax, %r12d
movq 0x10(%rsp), %rcx
shlq %cl, %r12
cmpq %rsi, %r15
movq %r9, 0x8(%rsp)
jne 0x1ae55
leaq (%rsi,%rsi), %rbp
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rbp, %rsi
movl %r14d, %eax
addq %r12, %rax
addq %rax, %rax
movq %rax, (%rdi,%r15,8)
leaq 0x1(%r15), %r14
cmpq %rsi, %r14
jne 0x1ae7e
leaq (%rsi,%rsi), %rbp
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %rbp, %rsi
incq %rbx
movl %ebx, %eax
addq %r12, %rax
leaq 0x1(,%rax,2), %rax
movq %rax, 0x8(%rdi,%r15,8)
movq 0x10(%r13), %rdx
movq 0x8(%rdx,%r15,8), %r9
leaq (%r9,%r9), %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %r8
subb 0x18(%r8), %cl
shrq %cl, %rax
movl 0x18(%rdx,%r15,8), %ebx
movq 0x30(%r8), %rcx
andq %r9, %rcx
movq %rcx, %rbp
subq %rbx, %rbp
decq %rbx
incq %rbp
testq %r9, %r9
movl $0x0, %edx
cmovsq %rdx, %rbx
cmovnsq %rcx, %rbp
addq %rcx, %rbx
testq %rbx, %rbx
cmovleq %rdx, %rbx
testq %rbp, %rbp
cmovleq %rdx, %rbp
movl %eax, %r12d
movq 0x10(%rsp), %rcx
shlq %cl, %r12
incq %r14
cmpq %rsi, %r14
jne 0x1af13
movq %r15, %r13
leaq (%rsi,%rsi), %r15
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %r15
movl %ebp, %eax
addq %r12, %rax
addq %rax, %rax
movq %rax, 0x10(%rdi,%r15,8)
incq %r14
cmpq %rsi, %r14
jne 0x1af41
movq %r15, %r13
leaq (%rsi,%rsi), %r15
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %r15
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %r9
incq %rbx
movl %ebx, %eax
addq %r12, %rax
leaq 0x1(,%rax,2), %rax
movq %rax, 0x18(%rdi,%r15,8)
incq %r9
incq %r14
cmpq (%r13), %r9
jb 0x1adc5
addq $0x4, %r15
leaq (%rdi,%r15,8), %rsi
movq %rdi, 0x10(%rsp)
callq 0x13180d
testq %r15, %r15
jne 0x1af8f
xorl %r11d, %r11d
jmp 0x1b0f2
pushq $0x1
popq %rbp
movq %r15, %rdx
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r11d, %r11d
xorl %r14d, %r14d
movq 0x10(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x38(%rsp), %r10
movq %rdx, 0x40(%rsp)
cmpq %rdx, %rbp
je 0x1afde
movq (%r8,%rbp,8), %rdx
movq 0x28(%rsp), %rcx
movb 0x18(%rcx), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %rdx
movq (%r8,%r14,8), %rsi
shrq %cl, %rsi
cmpq %rsi, %rdx
je 0x1b0dd
movq (%r8,%r14,8), %rsi
movq 0x28(%rsp), %rcx
movb 0x18(%rcx), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %rsi
movq 0x20(%rsp), %rcx
movq %rsi, 0x8(%rsp)
movq %r15, (%rcx,%rsi,8)
cmpq %r14, %rbp
je 0x1b0bc
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r12, %rcx
movq (%r8,%r14,8), %rdx
movl %edx, %r12d
andl $0x1, %r12d
negq %r12
orq $0x1, %r12
addq %rcx, %r12
cmpq %r9, %rcx
setge %sil
cmpq %r9, %r12
setl %dil
orb %sil, %dil
je 0x1b0a7
cmpq %r9, %r12
setl %dl
cmpq %r9, %rcx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x1b0b0
cmpq %rax, %r15
jne 0x1b087
leaq (%rax,%rax), %r13
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r13
movq %r13, %rsi
shlq $0x4, %rsi
movq %r11, %rdi
callq 0x2370
movq 0x38(%rsp), %r10
movq 0x30(%rsp), %r9
movq 0x10(%rsp), %r8
movq %rax, %r11
movq %r13, %rax
movq 0x18(%rsp), %r13
leaq 0x1(%r15), %rcx
shlq $0x4, %r15
movq %rbx, (%r11,%r15)
movq (%r8,%r14,8), %rdx
andq %r10, %rdx
shrq %rdx
movq %rdx, 0x8(%r11,%r15)
movq %rcx, %r15
jmp 0x1b0b0
andq %r10, %rdx
shrq %rdx
movq %rdx, %rbx
incq %r14
cmpq %r14, %rbp
jne 0x1b00d
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rdi
movq (%rsi,%rdi,8), %rcx
movl %r15d, %edx
subl %ecx, %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rsi,%rdi,8)
movq %rbp, %r14
leaq 0x1(%rbp), %rcx
movq 0x40(%rsp), %rdx
cmpq %rdx, %rbp
movq %rcx, %rbp
jne 0x1afb4
movq (%r13), %rcx
testq %rcx, %rcx
movq %r11, 0x30(%rsp)
jne 0x1b160
movq $0x0, 0x8(%rsp)
movq 0x26eed0(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x2153b4(%rip), %rsi    # 0x2304ce
leaq 0x2153e5(%rip), %rdx    # 0x230506
movq 0x8(%rsp), %rbx
movq %rbx, %r8
xorl %eax, %eax
callq 0x22b0
movq %rbx, (%r13)
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x8(%r11), %rax
xorl %edx, %edx
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
movq 0x10(%r13), %r8
movq %rdi, %r9
shlq $0x5, %r9
movq (%r8,%r9), %rbp
movq 0x18(%r8,%r9), %r11
movq %r11, %rbx
shrq $0x20, %rbx
leaq (,%rbp,2), %r14
movq 0x28(%rsp), %rsi
movq 0x18(%rsi), %r15
movq %r15, %rcx
negq %rcx
shrq %cl, %r14
movq 0x30(%rsi), %r10
movq %r10, %rcx
andq %rbp, %rcx
movq %rcx, %r12
subq %rbx, %r12
decq %rbx
incq %r12
testq %rbp, %rbp
cmovsq %rdx, %rbx
cmovnsq %rcx, %r12
addq %rcx, %rbx
testq %rbx, %rbx
cmovleq %rdx, %rbx
testq %r12, %r12
cmovleq %rdx, %r12
movl %r14d, %ecx
movq 0x20(%rsp), %rsi
movq (%rsi,%rcx,8), %rbp
movq %rbp, %r14
shrq $0x20, %r14
je 0x1b288
addq %r8, %r9
pushq $0x40
popq %rcx
subq %r15, %rcx
incl %ebx
movl %ebp, %r15d
movl %r12d, %r12d
shlq $0x4, %r14
shlq $0x4, %r15
addq %rax, %r15
xorl %ebp, %ebp
movq -0x8(%r15,%rbp), %r13
movq (%r15,%rbp), %rsi
cmpq %rbx, %rsi
cmovaeq %rbx, %rsi
cmpq %r12, %r13
cmovbeq %r12, %r13
cmpq %r13, %rsi
ja 0x1b236
addq $0x10, %rbp
cmpq %rbp, %r14
jne 0x1b20f
jmp 0x1b283
movq 0x8(%r9), %rsi
leaq (%rsi,%rsi), %rbx
shrq %cl, %rbx
andq %rsi, %r10
movl %r11d, %ecx
movq %r10, %r11
subq %rcx, %r11
decq %rcx
incq %r11
testq %rsi, %rsi
cmovsq %rdx, %rcx
cmovnsq %r10, %r11
addq %r10, %rcx
testq %rcx, %rcx
cmovleq %rdx, %rcx
testq %r11, %r11
cmovleq %rdx, %r11
movl %ebx, %esi
movq 0x20(%rsp), %r10
movq (%r10,%rsi,8), %rbx
movq %rbx, %r10
shrq $0x20, %r10
jne 0x1b29d
movq 0x18(%rsp), %r13
incq %rdi
movq (%r13), %rcx
cmpq %rcx, %rdi
jb 0x1b171
jmp 0x1b109
incl %ecx
movl %ebx, %ebx
movl %r11d, %r11d
shlq $0x4, %r10
shlq $0x4, %rbx
addq %rax, %rbx
xorl %r14d, %r14d
movq 0x18(%rsp), %r13
movq -0x8(%rbx,%r14), %rsi
movq (%rbx,%r14), %r15
cmpq %rcx, %r15
cmovaeq %rcx, %r15
cmpq %r11, %rsi
cmovbeq %r11, %rsi
cmpq %rsi, %r15
ja 0x1b2de
addq $0x10, %r14
cmpq %r14, %r10
jne 0x1b2b7
jmp 0x1b288
movq 0x8(%rsp), %rsi
movq %rsi, %rcx
shlq $0x5, %rcx
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups %xmm1, 0x10(%r8,%rcx)
movups %xmm0, (%r8,%rcx)
incq %rsi
movq %rsi, 0x8(%rsp)
jmp 0x1b288