// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: count_k_hits(All_reads*, ul_idx_t const*, char*, UC_Read*, overlap_region_alloc*, Candidates_list*, asg64_v*, unsigned long, unsigned long)
void count_k_hits(All_reads *rref, const ul_idx_t *uref, char* qstr, UC_Read *buf, 
overlap_region_alloc* ol, Candidates_list *cl, asg64_v* ii, uint64_t khit, uint64_t basec)
{
    int64_t k, l, on = ol->length, m = 0, i, cn = cl->length, srt_n; 
    overlap_region *z; uint64_t t, ff, pid, *srt; char *str0 = NULL, *str1 = NULL;
    kv_resize(uint64_t, *ii, ol->length);
    for (k = 0, ii->n = 0; k < on; k++, ii->n++) {
        ii->a[ii->n] = ol->list[k].shared_seed;
        ii->a[ii->n] <<= 32; ii->a[ii->n] |= k;
    }
    radix_sort_bc64(ii->a, ii->a+ii->n); 

    for (k = m = 0; k < on; k++) {
        z = &(ol->list[(uint32_t)ii->a[k]]); i = z->shared_seed; 
        pid = cl->list[i].readID; z->shared_seed = m;
        for (; i < cn && cl->list[i].readID == pid; i++) {
            cl->list[m] = cl->list[i]; 
            cl->list[m].readID = (uint32_t)ii->a[k]; 
            cl->list[m].cnt = 0;
            t = cl->list[m].self_offset; t <<= 32; t |= m;
            kv_push(uint64_t, (*ii), t);
            m++;
        }
    }
    cl->length = cn = m; srt = ii->a+on; srt_n = ii->n-on;
    radix_sort_bc64(srt, srt+srt_n);
    if(basec) {
        resize_UC_Read(buf, (khit<<1)); str0 = buf->seq; str1 = buf->seq + khit;
    }

    for (k = 1, l = 0; k <= srt_n; k++) {
        if(k == cn || (srt[l]>>32) != (srt[k]>>32)) {
            ff = k - l;
            if(basec && ff > 1) {
                if(!k_hits_bcheck(rref, uref, ol, cl, khit, srt+l, k-l, qstr, str0, str1)) ff = 1;
            }
            for (i = l; i < k; i++) {
                cl->list[(uint32_t)srt[i]].cnt = ff;
                // fprintf(stderr, "[M::%s::] pos::%u, cnt::%lu\n", __func__, 
                // cl->list[(uint32_t)srt[i]].self_offset, ff);
            }
            
            l = k;
        }
    }

    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %rcx, 0x48(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
movq 0xa0(%rsp), %r9
movq %r8, 0x38(%rsp)
movq 0x10(%r8), %rcx
movq 0x8(%rbx), %rbp
cmpq %rcx, 0x8(%r9)
movq %rcx, 0x40(%rsp)
jae 0x32769
leaq -0x1(%rcx), %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, 0x8(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0xa0(%rsp), %r9
movq 0x40(%rsp), %rcx
movq %rax, 0x10(%r9)
xorl %eax, %eax
testq %rcx, %rcx
movl $0x0, %r14d
cmovgq %rcx, %r14
pushq $0x24
popq %rcx
xorl %edx, %edx
movq %rax, (%r9)
cmpq %rdx, %r14
je 0x327ba
movq 0x38(%rsp), %rsi
movq (%rsi), %rsi
movslq (%rsi,%rcx), %rsi
movq 0x10(%r9), %rdi
movq %rsi, (%rdi,%rax,8)
movq (%r9), %rax
shlq $0x20, (%rdi,%rax,8)
movq (%r9), %rax
orq %rdx, (%rdi,%rax,8)
incq %rdx
movq (%r9), %rax
incq %rax
addq $0xa8, %rcx
jmp 0x3277d
movq 0x10(%r9), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1905d
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl $0x80000000, %r9d       # imm = 0x80000000
xorl %r10d, %r10d
movq %r14, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
movq %rbx, 0x60(%rsp)
cmpq %r14, %r10
je 0x3291f
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rdi), %rcx
movl (%rcx,%r10,8), %ecx
imulq $0xa8, %rcx, %rcx
movslq 0x24(%rax,%rcx), %r11
movq (%rbx), %rdx
movq %r11, %r13
shlq $0x4, %r13
movl (%rdx,%r13), %r12d
movl %esi, 0x24(%rax,%rcx)
movq %rsi, %r15
shlq $0x4, %r15
cmpq %rbp, %r11
jge 0x32912
movq (%rbx), %rax
movl (%rax,%r13), %ecx
xorl %r12d, %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x32912
leaq (%rax,%r13), %rcx
movups (%rcx), %xmm0
movups %xmm0, (%rax,%r15)
movq 0x10(%rdi), %rax
movq (%rbx), %rcx
movl (%rax,%r10,8), %eax
andl %r8d, %eax
movl (%rcx,%r15), %edx
andl %r9d, %edx
orl %eax, %edx
movl %edx, (%rcx,%r15)
movq (%rbx), %rax
andl $0x0, 0xc(%rax,%r15)
movl 0x8(%rax,%r15), %r14d
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x328ed
movq %rsi, 0x30(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
movl %r12d, %ebx
movq %r10, %r12
movq %r11, %rbp
callq 0x2370
movq %rbp, %r11
movq %r12, %r10
movl %ebx, %r12d
movl $0x80000000, %r9d       # imm = 0x80000000
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0xa0(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rbx
movq 0x28(%rsp), %rbp
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
shlq $0x20, %r14
orq %rsi, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r14, (%rax,%rcx,8)
incq %rsi
incq %r11
addq $0x10, %r13
addq $0x10, %r15
jmp 0x3282d
incq %r10
movq 0x20(%rsp), %r14
jmp 0x327ef
movq %rsi, 0x30(%rsp)
movq %rsi, 0x8(%rbx)
movq (%rdi), %r14
movq 0x10(%rdi), %rax
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx,8), %rbp
leaq (%rax,%r14,8), %rsi
subq %rcx, %r14
movq %rbp, %rdi
callq 0x1905d
cmpq $0x0, 0xb0(%rsp)
je 0x32988
movq 0xa8(%rsp), %rax
movq %rbx, %r13
movq %rax, %rbx
leaq (%rax,%rax), %rsi
movq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x562fa
movq (%r12), %rcx
movq %rcx, 0x28(%rsp)
leaq (%rcx,%rbx), %rax
movq %rax, 0x20(%rsp)
movq %r13, %rbx
jmp 0x3299a
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
xorl %r13d, %r13d
testq %r14, %r14
cmovleq %r13, %r14
incq %r14
pushq $0x1
popq %r12
movq 0x30(%rsp), %rdx
cmpq %r14, %r12
je 0x32a69
cmpq %rdx, %r12
je 0x329d2
movq (%rbp,%r12,8), %rax
xorq (%rbp,%r13,8), %rax
shrq $0x20, %rax
je 0x32a61
cmpq $0x0, 0xb0(%rsp)
setne %al
movq %r12, %r15
subq %r13, %r15
cmpq $0x2, %r15
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x32a59
leaq (,%r13,8), %r9
addq %rbp, %r9
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rcx
movq 0xa8(%rsp), %r8
callq 0x31a5b
movq 0x30(%rsp), %rdx
testq %rax, %rax
pushq $0x1
popq %rax
cmoveq %rax, %r15
jmp 0x32a59
movq (%rbx), %rax
movl (%rbp,%r13,8), %ecx
shlq $0x4, %rcx
movl %r15d, 0xc(%rax,%rcx)
incq %r13
cmpq %r12, %r13
jl 0x32a45
movq %r12, %r13
incq %r12
jmp 0x329b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq