// File: /workspace/github_cpp_makefile_star2_O2/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), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, 0x20(%rdi)
jae 0xfdd65
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%r15)
movq 0x28(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x28(%r15)
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0xfdd69
movq 0x28(%r15), %rdi
movq %rdx, 0x18(%r15)
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, 0x38(%r15)
jae 0xfddf4
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x38(%r15)
movq 0x40(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x40(%r15)
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0xfddf8
movq 0x40(%r15), %rdi
movq %rdx, 0x30(%r15)
shlq $0x3, %rdx
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x20e0
movq (%r15), %rsi
movq 0x10(%r15), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0xf0d02
pushq $0x1
popq %r13
cmpq (%r15), %r13
ja 0xfdf2e
movq 0x10(%r15), %rax
je 0xfde64
movq %r13, %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 %r12, %rsi
shlq $0x5, %rsi
movq (%rax,%rsi), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
cmpq %rsi, %rdx
je 0xfdf26
movq %r13, %rbp
subq %r12, %rbp
cmpq $0x2, %rbp
jb 0xfde8d
movq %r12, %rdi
shlq $0x5, %rdi
addq %rax, %rdi
movq %r13, %rsi
shlq $0x5, %rsi
addq %rax, %rsi
callq 0xf0f83
movq 0x10(%r15), %rax
movq %r12, %rcx
shlq $0x20, %rcx
orq %rcx, %rbp
movq 0x28(%r15), %rdx
movq %r12, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq -0x8(%rax), %rsi
addq %rsi, %rsi
movb 0x18(%r14), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rsi
movq %rbp, (%rdx,%rsi,8)
cmpq %r13, %r12
jae 0xfdf23
movq -0x8(%rax), %rsi
addq %rsi, %rsi
movb 0x18(%r14), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %rsi
movq (%rax), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
testq %rbx, %rbx
je 0xfdf0a
movq (%rbx), %r8
movq 0x80(%rbx), %rdi
movl %esi, %ecx
movl (%r8,%rcx,4), %esi
incq %rdi
cmpq %rsi, %rdi
jb 0xfdf1a
movl %edx, %edx
movl (%r8,%rdx,4), %esi
cmpq %rsi, %rdi
jae 0xfdf0e
jmp 0xfdf1a
movl %esi, %ecx
movl %edx, %edx
movq 0x40(%r15), %rsi
incq (%rsi,%rcx,8)
incq (%rsi,%rdx,8)
incq %r12
addq $0x20, %rax
jmp 0xfdec0
movq %r13, %r12
incq %r13
jmp 0xfde21
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq