// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: idx_hc_links(kvec_pe_hit*, ha_ug_index*, bubble_type*)
void idx_hc_links(kvec_pe_hit* hits, ha_ug_index* idx, bubble_type* bub)
{
    uint64_t k, l;
    uint32_t qn, tn;

    kv_resize(uint64_t, hits->idx, idx->ug->g->n_seq);
    hits->idx.n = idx->ug->g->n_seq;
    memset(hits->idx.a, 0, hits->idx.n*sizeof(uint64_t));

    kv_resize(uint64_t, hits->occ, idx->ug->g->n_seq);
    hits->occ.n = idx->ug->g->n_seq;
    memset(hits->occ.a, 0, hits->occ.n*sizeof(uint64_t));

    radix_sort_pe_hit_idx_an1(hits->a.a, hits->a.a + hits->a.n);
    for (k = 1, l = 0; k <= hits->a.n; ++k) 
    {   
        if (k == hits->a.n || 
            ((hits->a.a[k].s<<1)>>(64 - idx->uID_bits)) != ((hits->a.a[l].s<<1)>>(64 - idx->uID_bits))) 
        {
            if (k - l > 1) radix_sort_pe_hit_idx_an2(hits->a.a + l, hits->a.a + k);
            
            hits->idx.a[((hits->a.a[l].s<<1)>>(64 - idx->uID_bits))] 
                                                = (uint64_t)l << 32 | (k - l);

            for (; l < k; l++)
            {
                qn = ((hits->a.a[l].s<<1)>>(64 - idx->uID_bits));
                tn = ((hits->a.a[l].e<<1)>>(64 - idx->uID_bits));
                if(bub && IF_HOM(qn, *bub)) continue;
                if(bub && IF_HOM(tn, *bub)) continue;
                hits->occ.a[qn]++;
                hits->occ.a[tn]++;
            }
            
            l = k;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq (%rsi), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x20(%rdi)
jae 0x13f897
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x20(%r15)
movq 0x28(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x28(%r15)
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
andq %r12, %rdx
movq %rdx, 0x18(%r15)
movq 0x28(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andq %r12, %rax
cmpq %rax, 0x38(%r15)
jae 0x13f921
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x38(%r15)
movq 0x40(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x40(%r15)
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, 0x30(%r15)
movq 0x40(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%r15), %rsi
movq 0x10(%r15), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x13134e
movq (%r15), %rax
testq %rax, %rax
je 0x13fa8c
movl $0x1, %r12d
xorl %r13d, %r13d
cmpq %rax, %r12
je 0x13f9ad
movq 0x10(%r15), %rax
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rdx
addq %rdx, %rdx
movb 0x18(%r14), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %rdx
movq %r13, %rsi
shlq $0x5, %rsi
movq (%rax,%rsi), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdx
jne 0x13f9ad
movq %r13, %rcx
jmp 0x13fa7a
movq %r12, %rbp
subq %r13, %rbp
cmpq $0x2, %rbp
jb 0x13f9d6
movq 0x10(%r15), %rax
movq %r13, %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq %r12, %rsi
shlq $0x5, %rsi
addq %rax, %rsi
callq 0x1315e1
movq %r13, %rax
shlq $0x20, %rax
orq %rax, %rbp
movq 0x10(%r15), %rax
movq %r13, %rdx
shlq $0x5, %rdx
movq (%rax,%rdx), %rsi
addq %rsi, %rsi
movb 0x18(%r14), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rsi
movq 0x28(%r15), %rcx
movq %rbp, (%rcx,%rsi,8)
movq %r12, %rcx
cmpq %r13, %r12
jbe 0x13fa7a
addq %rdx, %rax
addq $0x8, %rax
movq -0x8(%rax), %rdx
addq %rdx, %rdx
movb 0x18(%r14), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %rdx
movq (%rax), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
testq %rbx, %rbx
je 0x13fa5b
movq (%rbx), %rdi
movq 0x80(%rbx), %rcx
movl %edx, %r8d
movl (%rdi,%r8,4), %r8d
incq %rcx
cmpq %r8, %rcx
jb 0x13fa6b
movl %esi, %r8d
movl (%rdi,%r8,4), %edi
cmpq %rdi, %rcx
jb 0x13fa6b
movq 0x40(%r15), %rcx
movl %edx, %edx
incq (%rcx,%rdx,8)
movl %esi, %edx
incq (%rcx,%rdx,8)
incq %r13
addq $0x20, %rax
cmpq %r13, %r12
jne 0x13fa15
movq %r12, %rcx
incq %r12
movq (%r15), %rax
movq %rcx, %r13
cmpq %rax, %r12
jbe 0x13f96c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq