// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x20(%rsp)
movq %rcx, %rbx
movq %rsi, 0x68(%rsp)
movq %rdi, 0x60(%rsp)
movq 0xb0(%rsp), %r10
movq %r8, 0x38(%rsp)
movq 0x10(%r8), %r15
movq 0x8(%r9), %rax
movq %rax, 0x30(%rsp)
cmpq %r15, 0x8(%r10)
jae 0x62e2f
leaq -0x1(%r15), %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(%r10)
movq 0x10(%r10), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0xb0(%rsp), %r10
movq %rax, 0x10(%r10)
movq $0x0, (%r10)
testq %r15, %r15
movq %rbx, 0x48(%rsp)
jle 0x63031
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%r10), %rdi
addq $0x24, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rax), %rsi
movq %rsi, (%rdi,%rdx,8)
movq (%r10), %rdx
shlq $0x20, (%rdi,%rdx,8)
movq (%r10), %rdx
orq %rcx, (%rdi,%rdx,8)
incq %rcx
movq (%r10), %rdx
incq %rdx
movq %rdx, (%r10)
addq $0xa8, %rax
cmpq %rcx, %r15
jne 0x62e58
leaq (%rdi,%rdx,8), %rsi
callq 0x45373
movq 0xb0(%rsp), %r8
movq 0x10(%r8), %rax
testq %r15, %r15
jle 0x63052
movq 0x30(%rsp), %rcx
decq %rcx
movq %rcx, 0x50(%rsp)
xorl %r9d, %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0x80000000, %r11d      # imm = 0x80000000
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
movq %r15, 0x70(%rsp)
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movl (%rax,%r9,8), %edx
imulq $0xa8, %rdx, %rdx
movslq 0x24(%rcx,%rdx), %r15
movq (%rdi), %rsi
movq %r15, %r13
shlq $0x4, %r13
movl (%rsi,%r13), %r14d
movl %ebx, 0x24(%rcx,%rdx)
cmpq %r15, 0x30(%rsp)
jle 0x6301e
movq (%rdi), %rcx
movl (%rcx,%r13), %edx
xorl %r14d, %edx
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jne 0x6301e
movq 0x30(%rsp), %rax
addq %rbx, %rax
subq %r15, %rax
movq %rax, 0x58(%rsp)
leaq (%rcx,%r13), %rdx
movq %rbx, %rbp
shlq $0x4, %rbp
movq 0x50(%rsp), %r12
subq %r15, %r12
movl %r14d, 0x28(%rsp)
movups (%rdx), %xmm0
movups %xmm0, (%rcx,%rbp)
movq 0x10(%r8), %rax
movq (%rdi), %rcx
movl (%rax,%r9,8), %eax
andl %r10d, %eax
movl (%rcx,%rbp), %edx
andl %r11d, %edx
orl %eax, %edx
movl %edx, (%rcx,%rbp)
movq (%rdi), %rax
movl $0x0, 0xc(%rax,%rbp)
movl 0x8(%rax,%rbp), %r14d
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x62fcb
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
movq %rbx, 0x40(%rsp)
movq %r9, %rbx
callq 0x2370
movl $0x80000000, %r11d      # imm = 0x80000000
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %r9
movq 0x40(%rsp), %rbx
movq 0xb0(%rsp), %r8
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%r8)
movq (%r8), %rcx
shlq $0x20, %r14
orq %rbx, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r14, (%rax,%rcx,8)
subq $0x1, %r12
jb 0x63019
incq %r15
movq (%rdi), %rcx
movq %r15, %rdx
shlq $0x4, %rdx
addq %rcx, %rdx
movl 0x10(%rcx,%r13), %esi
movl 0x28(%rsp), %r14d
xorl %r14d, %esi
addq $0x10, %r13
addq $0x10, %rbp
incq %rbx
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x62f3e
jmp 0x6301e
movq 0x58(%rsp), %rbx
incq %r9
movq 0x70(%rsp), %r15
cmpq %r15, %r9
jne 0x62ecb
jmp 0x63059
movq 0x10(%r10), %rdi
movq %rdi, %rsi
callq 0x45373
movq 0xb0(%rsp), %r8
movq 0x20(%rsp), %rdi
movq 0x10(%r8), %rax
xorl %ebx, %ebx
jmp 0x63059
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
movq 0xb8(%rsp), %rbp
movq %rbx, 0x8(%rdi)
leaq (%rax,%r15,8), %r13
movq (%r8), %r14
leaq (%rax,%r14,8), %rsi
subq %r15, %r14
movq %r13, %rdi
callq 0x45373
cmpq $0x0, 0xc0(%rsp)
je 0x630be
leaq (,%rbp,2), %r12
movq 0x48(%rsp), %r15
movq (%r15), %rdi
cmpq %r12, 0x10(%r15)
jge 0x630ae
movq %r12, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r15)
movq %r12, 0x10(%r15)
movq 0x20(%rsp), %rcx
leaq (%rdi,%rbp), %rax
movq %rax, 0x28(%rsp)
jmp 0x630ce
xorl %edi, %edi
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rcx
testq %r14, %r14
jle 0x631ad
incq %r14
xorl %r12d, %r12d
movl $0x1, %ebp
movq %r14, 0x40(%rsp)
cmpq %rbx, %rbp
je 0x63104
movq (%r13,%rbp,8), %rax
xorq (%r13,%r12,8), %rax
shrq $0x20, %rax
jne 0x63104
movq %r12, %rax
jmp 0x6319e
cmpq $0x0, 0xc0(%rsp)
setne %al
movq %rbp, %r15
subq %r12, %r15
cmpq $0x2, %r15
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x6317a
leaq (,%r12,8), %r9
addq %r13, %r9
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %r15, (%rsp)
movq %rdi, %r14
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0xb8(%rsp), %r8
callq 0x61f67
movq %r14, %rdi
movq 0x40(%rsp), %r14
movq 0x20(%rsp), %rcx
testq %rax, %rax
movl $0x1, %eax
cmoveq %rax, %r15
movq %rbp, %rax
cmpq %r12, %rbp
jle 0x6319e
movq (%rcx), %rax
movl (%r13,%r12,8), %edx
shlq $0x4, %rdx
movl %r15d, 0xc(%rax,%rdx)
incq %r12
cmpq %r12, %rbp
jne 0x63185
movq %rbp, %rax
incq %rbp
movq %rax, %r12
cmpq %r14, %rbp
jne 0x630e7
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq