// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %rsi, %r13
movq (%rdi), %rax
movq %rdi, 0x30(%rsp)
movq 0x18(%rdi), %r12
pushq $-0x1
popq %rdx
movl %r12d, %ecx
shrq %cl, %rdx
movq %rdx, 0x38(%rsp)
movq (%rax), %rdi
pushq $0x8
popq %rsi
callq 0x2120
movq %rax, 0x10(%rsp)
cmpq $0x0, (%r13)
movq %r13, 0x28(%rsp)
jne 0x1a6a3
movq $0x0, 0x18(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x1379f9
xorl %r10d, %r10d
movq 0x10(%rsp), %r12
jmp 0x1aa1d
pushq $0x3f
popq %rax
subq %r12, %rax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %edi, %edi
xorl %ebp, %ebp
movq %rax, 0x18(%rsp)
movq 0x10(%r13), %rax
movq (%rax,%rbx,8), %rdx
movq 0x18(%rax,%rbx,8), %r13
leaq (%rdx,%rdx), %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %rsi
subb 0x18(%rsi), %cl
shrq %cl, %rax
shrq $0x20, %r13
movq 0x30(%rsi), %rcx
andq %rdx, %rcx
movq %rcx, %rsi
subq %r13, %rsi
decq %r13
incq %rsi
testq %rdx, %rdx
movl $0x0, %edx
cmovsq %rdx, %r13
cmovnsq %rcx, %rsi
addq %rcx, %r13
testq %r13, %r13
cmovleq %rdx, %r13
testq %rsi, %rsi
cmovleq %rdx, %rsi
movl %eax, %r15d
movq 0x8(%rsp), %rcx
shlq %cl, %r15
movl %esi, %r12d
addq %r15, %r12
addq %r12, %r12
cmpq %r14, %rbx
movq %r8, (%rsp)
jne 0x1a75e
testq %rbx, %rbx
movq %r8, %r14
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rbx, %rcx
orq $0x1, %rcx
movq %r12, (%rax,%rbx,8)
jmp 0x1a784
leaq 0x1(%rbx), %rcx
movq %r12, (%rdi,%rbx,8)
cmpq %r14, %rcx
jne 0x1a784
leaq (%r14,%r14), %r12
movq %r14, %rsi
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %r14, %rcx
movq %r12, %r14
incq %r13
movl %r13d, %eax
addq %r15, %rax
leaq 0x1(,%rax,2), %rdx
leaq 0x2(%rbx), %rax
movq %rdx, (%rdi,%rcx,8)
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %rsi
movq 0x8(%rsi,%rbx,8), %r9
leaq (%r9,%r9), %rdx
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
subb 0x18(%r8), %cl
shrq %cl, %rdx
movl 0x18(%rsi,%rbx,8), %r13d
movq 0x30(%r8), %rcx
andq %r9, %rcx
movq %rcx, %rsi
subq %r13, %rsi
decq %r13
incq %rsi
testq %r9, %r9
movl $0x0, %r8d
cmovsq %r8, %r13
cmovnsq %rcx, %rsi
addq %rcx, %r13
testq %r13, %r13
cmovleq %r8, %r13
testq %rsi, %rsi
cmovleq %r8, %rsi
movl %edx, %r15d
movq 0x8(%rsp), %rcx
shlq %cl, %r15
movl %esi, %r12d
addq %r15, %r12
addq %r12, %r12
cmpq %r14, %rax
jne 0x1a83a
leaq (%r14,%r14), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rsi
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %rbx, %rax
orq $0x3, %rax
movq %r12, (%rdi,%r14,8)
movq 0x20(%rsp), %r14
jmp 0x1a861
leaq 0x3(%rbx), %rax
movq %r12, 0x10(%rdi,%rbx,8)
cmpq %r14, %rax
jne 0x1a861
leaq (%r14,%r14), %r12
movq %r14, %rsi
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %r14, %rax
movq %r12, %r14
movq (%rsp), %r8
incq %r13
movl %r13d, %ecx
addq %r15, %rcx
leaq 0x1(,%rcx,2), %rcx
addq $0x4, %rbx
movq %rcx, (%rdi,%rax,8)
incq %rbp
addq $0x8, %r8
movq 0x18(%rsp), %rax
addq $-0x20, %rax
movq 0x28(%rsp), %r13
cmpq (%r13), %rbp
jb 0x1a6bc
movq %rdi, %rsi
subq %rax, %rsi
movq %rdi, 0x18(%rsp)
callq 0x1379f9
testq %rbx, %rbx
movq 0x10(%rsp), %r12
jne 0x1a8bf
xorl %r10d, %r10d
jmp 0x1aa1d
pushq $0x1
popq %rbp
xorl %r15d, %r15d
movq %rbx, %rdx
xorl %ebx, %ebx
xorl %r10d, %r10d
xorl %r14d, %r14d
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r9
movq %rdx, 0x20(%rsp)
pushq $0x40
popq %rcx
movq 0x30(%rsp), %rax
subq 0x18(%rax), %rcx
cmpq %rbp, %rdx
jne 0x1a8fe
movq (%rdi,%r14,8), %r11
shrq %cl, %r11
jmp 0x1a915
movq (%rdi,%rbp,8), %rax
shrq %cl, %rax
movq (%rdi,%r14,8), %r11
shrq %cl, %r11
cmpq %r11, %rax
je 0x1aa08
movq %r15, (%r12,%r11,8)
cmpq %r14, %rbp
jne 0x1a926
movq %r15, %rax
jmp 0x1a9f5
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rbp, 0x8(%rsp)
movq %r13, %rax
movq (%rdi,%r14,8), %rcx
movl %ecx, %r13d
andl $0x1, %r13d
negq %r13
orq $0x1, %r13
addq %rax, %r13
cmpq %r8, %rax
setge %dl
cmpq %r8, %r13
setl %sil
orb %dl, %sil
je 0x1a9d2
cmpq %r8, %r13
setl %cl
cmpq %r8, %rax
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1a9db
cmpq %rbx, %r15
jne 0x1a9b2
leaq (%r15,%r15), %rbx
testq %r15, %r15
pushq $0x2
popq %rax
cmoveq %rax, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movq %r10, %rdi
movq %r12, %rbp
movq %r11, %r12
callq 0x2370
movq %r12, %r11
movq %rbp, %r12
movq 0x8(%rsp), %rbp
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, %r10
leaq 0x1(%r15), %rax
shlq $0x4, %r15
movq %r12, (%r10,%r15)
movq (%rdi,%r14,8), %rcx
andq %r9, %rcx
shrq %rcx
movq %rcx, 0x8(%r10,%r15)
movq %rax, %r15
jmp 0x1a9db
andq %r9, %rcx
shrq %rcx
movq %rcx, %r12
incq %r14
cmpq %r14, %rbp
jne 0x1a931
movq 0x10(%rsp), %r12
movq (%r12,%r11,8), %rax
movq 0x28(%rsp), %r13
movl %r15d, %ecx
subl %eax, %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r12,%r11,8)
movq %rbp, %r14
leaq 0x1(%rbp), %rax
movq 0x20(%rsp), %rdx
cmpq %rbp, %rdx
movq %rax, %rbp
jne 0x1a8e4
movq (%r13), %rax
testq %rax, %rax
movq %r10, 0x20(%rsp)
jne 0x1aa8c
movq $0x0, (%rsp)
xorl %eax, %eax
movq 0x27a5a4(%rip), %rcx    # 0x294fe0
movq (%rcx), %rdi
leaq 0x221c94(%rip), %rsi    # 0x23c6da
leaq 0x221cc5(%rip), %rdx    # 0x23c712
movq %rax, %rcx
movq (%rsp), %rbx
movq %rbx, %r8
xorl %eax, %eax
callq 0x22b0
movq %rbx, (%r13)
movq 0x18(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
callq 0x2220
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x8(%r10), %rcx
movq %rcx, 0x8(%rsp)
xorl %esi, %esi
xorl %edi, %edi
movq $0x0, (%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 0x30(%rsp), %rdx
movq 0x18(%rdx), %r15
movq %r15, %rcx
negq %rcx
shrq %cl, %r14
movq 0x30(%rdx), %r10
movq %r10, %rcx
andq %rbp, %rcx
movq %r12, %rdx
movq %rcx, %r12
subq %rbx, %r12
decq %rbx
incq %r12
testq %rbp, %rbp
cmovsq %rsi, %rbx
cmovnsq %rcx, %r12
addq %rcx, %rbx
testq %rbx, %rbx
cmovleq %rsi, %rbx
testq %r12, %r12
cmovleq %rsi, %r12
movl %r14d, %ecx
movq (%rdx,%rcx,8), %rbp
movq %rbp, %r14
shrq $0x20, %r14
je 0x1ab65
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 0x8(%rsp), %r15
xorl %ebp, %ebp
movq -0x8(%r15,%rbp), %r13
movq (%r15,%rbp), %rdx
cmpq %rbx, %rdx
cmovaeq %rbx, %rdx
cmpq %r12, %r13
cmovbeq %r12, %r13
cmpq %r13, %rdx
ja 0x1ab7b
addq $0x10, %rbp
cmpq %rbp, %r14
jne 0x1ab3b
movq 0x28(%rsp), %r13
movq 0x10(%rsp), %r12
incq %rdi
cmpq %rax, %rdi
jb 0x1aaa1
jmp 0x1aa35
movq 0x8(%r9), %rdx
leaq (%rdx,%rdx), %rbx
shrq %cl, %rbx
andq %rdx, %r10
movl %r11d, %ecx
movq %r10, %r11
subq %rcx, %r11
decq %rcx
incq %r11
testq %rdx, %rdx
cmovsq %rsi, %rcx
cmovnsq %r10, %r11
addq %r10, %rcx
testq %rcx, %rcx
cmovleq %rsi, %rcx
testq %r11, %r11
cmovleq %rsi, %r11
movl %ebx, %edx
movq 0x10(%rsp), %r12
movq (%r12,%rdx,8), %rbx
movq %rbx, %r10
shrq $0x20, %r10
movq 0x28(%rsp), %r13
je 0x1ab6a
incl %ecx
movl %ebx, %ebx
movl %r11d, %r11d
shlq $0x4, %r10
shlq $0x4, %rbx
addq 0x8(%rsp), %rbx
xorl %r14d, %r14d
movq -0x8(%rbx,%r14), %rdx
movq (%rbx,%r14), %r15
cmpq %rcx, %r15
cmovaeq %rcx, %r15
cmpq %r11, %rdx
cmovbeq %r11, %rdx
cmpq %rdx, %r15
ja 0x1ac0e
addq $0x10, %r14
cmpq %r14, %r10
jne 0x1abe4
jmp 0x1ab6a
movq (%rsp), %rcx
movq %rcx, %rax
shlq $0x5, %rax
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups %xmm1, 0x10(%r8,%rax)
movups %xmm0, (%r8,%rax)
incq %rcx
movq %rcx, (%rsp)
movq (%r13), %rax
jmp 0x1ab6a