// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdx, %r13
movq %rsi, %rbx
movq 0x18(%rdi), %rcx
pushq $0x3f
popq %rbp
movq %rcx, 0x40(%rsp)
subq %rcx, %rbp
movq %rdi, 0x50(%rsp)
movq (%rdi), %rax
movq (%rax), %rdi
pushq $0x8
popq %rsi
callq 0x2120
movq %rax, 0x60(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edi, %edi
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
movq %rbp, 0x58(%rsp)
cmpq (%rbx), %rcx
movq %rdi, 0x30(%rsp)
movq %r12, 0x8(%rsp)
jae 0x11a83a
movq %rcx, 0x48(%rsp)
movq %rax, 0x38(%rsp)
movq 0x10(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl 0x1c(%rax,%r12,8), %edx
movq 0x50(%rsp), %rdi
leaq 0x14(%rsp), %rcx
leaq 0x2c(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x117804
movq %r12, %rax
movl 0x14(%rsp), %r12d
movl %ebp, %ecx
shlq %cl, %r12
movl 0x2c(%rsp), %ecx
cmpq %r14, %rax
jne 0x11a707
leaq (%r14,%r14), %rbx
testq %r14, %r14
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq 0x30(%rsp), %rdi
movq %rcx, %r14
callq 0x2370
movl 0x14(%rsp), %r15d
movl %ebp, %ecx
shlq %cl, %r15
movq %r14, %rcx
movq %rbx, %r14
jmp 0x11a70f
movq %r12, %r15
movq 0x30(%rsp), %rax
addq %rcx, %r12
addq %r12, %r12
movq 0x8(%rsp), %rdx
leaq 0x1(%rdx), %rcx
movq %r12, (%rax,%rdx,8)
movl 0x28(%rsp), %r12d
cmpq %r14, %rcx
jne 0x11a742
leaq (%r14,%r14), %rbx
shlq $0x4, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2370
movq %rbx, %r14
movq 0x18(%rsp), %rcx
addq %r12, %r15
leaq 0x1(,%r15,2), %rdx
movq 0x8(%rsp), %rdi
leaq 0x2(%rdi), %rbx
movq %rdx, 0x8(%rax,%rdi,8)
movq 0x10(%rcx), %rcx
movq 0x8(%rcx,%rdi,8), %rsi
movl 0x18(%rcx,%rdi,8), %edx
movq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %rax, %r15
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x117804
movl 0x10(%rsp), %r12d
movq 0x58(%rsp), %rbp
movl %ebp, %ecx
shlq %cl, %r12
movl 0x24(%rsp), %eax
cmpq %r14, %rbx
jne 0x11a7cc
leaq (%r14,%r14), %rbx
shlq $0x4, %r14
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %r14
callq 0x2370
movq %rax, %rdi
movq %r14, %rax
movl 0x10(%rsp), %r15d
movl %ebp, %ecx
shlq %cl, %r15
movq %rbx, %r14
jmp 0x11a7d2
movq %r15, %rdi
movq %r12, %r15
addq %rax, %r12
addq %r12, %r12
movq 0x8(%rsp), %rcx
leaq 0x3(%rcx), %rax
movq %r12, 0x10(%rdi,%rcx,8)
movl 0x20(%rsp), %r12d
cmpq %r14, %rax
jne 0x11a806
leaq (%r14,%r14), %rbx
shlq $0x4, %r14
movq %r14, %rsi
callq 0x2370
movq %rax, %rdi
movq %rbx, %r14
movq 0x18(%rsp), %rbx
movq 0x48(%rsp), %rcx
addq %r12, %r15
leaq 0x1(,%r15,2), %rax
movq 0x8(%rsp), %r12
movq %rax, 0x18(%rdi,%r12,8)
addq $0x4, %r12
incq %rcx
movq 0x38(%rsp), %rax
addq $-0x20, %rax
jmp 0x11a679
pushq $-0x1
popq %rbp
movq 0x40(%rsp), %rcx
shrq %cl, %rbp
movq %rdi, %rsi
subq %rax, %rsi
callq 0xf116c
movq 0x30(%rsp), %r8
pushq $0x1
popq %r10
xorl %ebx, %ebx
xorl %r9d, %r9d
xorl %eax, %eax
xorl %r15d, %r15d
movq %rbp, 0x48(%rsp)
cmpq %r12, %r10
ja 0x11a9b6
pushq $0x40
popq %rcx
movq 0x50(%rsp), %rdx
subq 0x18(%rdx), %rcx
cmpq %r10, %r12
jne 0x11a88e
movq (%r8,%rbx,8), %rdx
shrq %cl, %rdx
movq %rdx, %rcx
jmp 0x11a8a8
movq (%r8,%r10,8), %rdx
shrq %cl, %rdx
movq (%r8,%rbx,8), %rsi
shrq %cl, %rsi
movq %rsi, %rcx
cmpq %rsi, %rdx
je 0x11a9ae
movq %rcx, %rdx
movq 0x60(%rsp), %rcx
movq %rdx, 0x58(%rsp)
movq %r15, (%rcx,%rdx,8)
xorl %ecx, %ecx
xorl %r11d, %r11d
cmpq %rbx, %r10
je 0x11a988
movq (%r8,%rbx,8), %rdx
movl %edx, %r14d
andl $0x1, %r14d
negq %r14
orq $0x1, %r14
addq %rcx, %r14
cmpq %r13, %rcx
setge %sil
cmpq %r13, %r14
setl %dil
orb %sil, %dil
je 0x11a974
cmpq %r13, %r14
setl %dl
cmpq %r13, %rcx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x11a97d
cmpq %rax, %r15
jne 0x11a954
leaq (%rax,%rax), %rbp
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbp
movq %rbp, %rsi
shlq $0x4, %rsi
movq %r9, %rdi
movq %r10, %r12
movq %r15, 0x38(%rsp)
movq %r13, %r15
movq %r11, %r13
callq 0x2370
movq %r13, %r11
movq %r15, %r13
movq 0x38(%rsp), %r15
movq %r12, %r10
movq 0x30(%rsp), %r8
movq %rax, %r9
movq %rbp, %rax
movq 0x48(%rsp), %rbp
leaq 0x1(%r15), %rcx
shlq $0x4, %r15
movq %r11, (%r9,%r15)
movq (%r8,%rbx,8), %rdx
andq %rbp, %rdx
shrq %rdx
movq %rdx, 0x8(%r9,%r15)
movq %rcx, %r15
jmp 0x11a97d
andq %rbp, %rdx
shrq %rdx
movq %rdx, %r11
incq %rbx
movq %r14, %rcx
jmp 0x11a8be
movq 0x60(%rsp), %rsi
movq 0x58(%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 %r10, %rbx
movq 0x8(%rsp), %r12
incq %r10
jmp 0x11a868
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq 0x18(%rsp), %rdx
movq 0x60(%rsp), %r13
movq %r9, 0x40(%rsp)
movq 0x50(%rsp), %r14
movq 0x40(%rsp), %r15
movq (%rdx), %rcx
cmpq %rcx, %rax
jae 0x11aad7
movq %rax, 0x38(%rsp)
movq 0x10(%rdx), %r12
movq (%r12,%rbx), %rsi
movq %r13, %rbp
movq 0x18(%r12,%rbx), %r13
movq %r13, %rdx
shrq $0x20, %rdx
movq %r14, %rdi
leaq 0x14(%rsp), %rcx
leaq 0x2c(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x117804
movl 0x14(%rsp), %eax
movq (%rbp,%rax,8), %rsi
movl %esi, %edi
shlq $0x4, %rdi
addq %r15, %rdi
shrq $0x20, %rsi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
callq 0x11a5f7
testq %rax, %rax
je 0x11aabe
movq 0x8(%r12,%rbx), %rsi
movq %r14, %rdi
movl %r13d, %edx
leaq 0x10(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x117804
movl 0x10(%rsp), %eax
movq (%rbp,%rax,8), %rsi
movl %esi, %edi
shlq $0x4, %rdi
addq %r15, %rdi
shrq $0x20, %rsi
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %ecx
callq 0x11a5f7
testq %rax, %rax
movq %rbp, %r13
je 0x11aab7
leaq (%r12,%rbx), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
shlq $0x5, %rcx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%r12,%rcx)
movups %xmm0, (%r12,%rcx)
incq %rdx
movq %rdx, 0x8(%rsp)
movq 0x18(%rsp), %rdx
jmp 0x11aac6
movq 0x18(%rsp), %rdx
movq %rbp, %r13
movq 0x38(%rsp), %rax
incq %rax
addq $0x20, %rbx
jmp 0x11a9dc
movq 0x138502(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq %rdx, %rbx
leaq 0xd3adf(%rip), %rsi     # 0x1ee5ca
leaq 0xd3b10(%rip), %rdx     # 0x1ee602
movq 0x8(%rsp), %r14
movq %r14, %r8
xorl %eax, %eax
callq 0x22b0
movq %r14, (%rbx)
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq