// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %rcx, %rbx
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movq 0xa0(%rsp), %r13
movq %r8, 0x38(%rsp)
movq 0x10(%r8), %r15
movq 0x8(%r9), %rax
movq %rax, 0x60(%rsp)
cmpq %r15, 0x8(%r13)
jae 0x6327d
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(%r13)
movq 0x10(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r13)
movq $0x0, (%r13)
testq %r15, %r15
jle 0x632d1
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%r13), %rcx
addq $0x24, %rax
xorl %edx, %edx
xorl %esi, %esi
movslq (%rax), %rdi
movq %rdi, (%rcx,%rsi,8)
movq (%r13), %rsi
shlq $0x20, (%rcx,%rsi,8)
movq (%r13), %rsi
orq %rdx, (%rcx,%rsi,8)
incq %rdx
movq (%r13), %rsi
incq %rsi
movq %rsi, (%r13)
addq $0xa8, %rax
cmpq %rdx, %r15
jne 0x6329e
jmp 0x632d3
xorl %esi, %esi
movq %rbx, 0x40(%rsp)
movq 0x10(%r13), %rdi
leaq (%rdi,%rsi,8), %rsi
callq 0x45d47
testq %r15, %r15
jle 0x63439
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl $0x80000000, %r10d      # imm = 0x80000000
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
movq %r15, 0x58(%rsp)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%r13), %rcx
movl (%rcx,%r8,8), %ecx
imulq $0xa8, %rcx, %rcx
movslq 0x24(%rax,%rcx), %rdx
movq (%rdi), %rsi
movq %rdx, %rbp
shlq $0x4, %rbp
movl (%rsi,%rbp), %esi
movl %r14d, 0x24(%rax,%rcx)
movq 0x60(%rsp), %rbx
subq %rdx, %rbx
jle 0x63426
movq %r14, %r12
shlq $0x4, %r12
movl %esi, 0x28(%rsp)
movq (%rdi), %rax
movl (%rax,%rbp), %ecx
xorl %esi, %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x63426
leaq (%rax,%rbp), %rcx
movups (%rcx), %xmm0
movups %xmm0, (%rax,%r12)
movq 0x10(%r13), %rax
movq (%rdi), %rcx
movl (%rax,%r8,8), %eax
andl %r9d, %eax
movl (%rcx,%r12), %edx
andl %r10d, %edx
orl %eax, %edx
movl %edx, (%rcx,%r12)
movq (%rdi), %rax
movl $0x0, 0xc(%rax,%r12)
movl 0x8(%rax,%r12), %r15d
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0x633f7
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x3, %rsi
movq %r14, 0x30(%rsp)
movq %r13, %r14
movq %r8, %r13
callq 0x2370
movl 0x28(%rsp), %esi
movl $0x80000000, %r10d      # imm = 0x80000000
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq %r13, %r8
movq %r14, %r13
movq 0x30(%rsp), %r14
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%r13)
shlq $0x20, %r15
orq %r14, %r15
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
movq %r15, (%rcx,%rax,8)
incq %r14
addq $0x10, %r12
addq $0x10, %rbp
decq %rbx
jne 0x63351
incq %r8
movq 0x58(%rsp), %r15
cmpq %r15, %r8
jne 0x6330a
jmp 0x63441
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
movq 0xa8(%rsp), %r12
movq %r14, 0x8(%rdi)
movq (%r13), %rbp
movq 0x10(%r13), %rax
leaq (%rax,%r15,8), %r13
leaq (%rax,%rbp,8), %rsi
subq %r15, %rbp
movq %r13, %rdi
callq 0x45d47
cmpq $0x0, 0xb0(%rsp)
je 0x634a7
leaq (%r12,%r12), %r15
movq 0x40(%rsp), %rbx
cmpq %r15, 0x10(%rbx)
jge 0x63494
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r15, 0x10(%rbx)
movq (%rbx), %rcx
movq %rcx, 0x30(%rsp)
leaq (%rcx,%r12), %rax
movq %rax, 0x28(%rsp)
jmp 0x634b9
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rcx
testq %rbp, %rbp
jle 0x63592
incq %rbp
xorl %r12d, %r12d
movl $0x1, %r15d
cmpq %r14, %r15
je 0x634f0
movq (%r13,%r15,8), %rax
xorq (%r13,%r12,8), %rax
shrq $0x20, %rax
jne 0x634f0
movq %r12, %rax
jmp 0x63583
cmpq $0x0, 0xb0(%rsp)
setne %al
movq %r15, %rbx
subq %r12, %rbx
cmpq $0x2, %rbx
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x63560
leaq (,%r12,8), %r9
addq %r13, %r9
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rbx, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0xa8(%rsp), %r8
callq 0x62475
movq 0x20(%rsp), %rcx
testq %rax, %rax
movl $0x1, %eax
cmoveq %rax, %rbx
movq %r15, %rax
cmpq %r12, %r15
jle 0x63583
movq (%rcx), %rax
movl (%r13,%r12,8), %edx
shlq $0x4, %rdx
movl %ebx, 0xc(%rax,%rdx)
incq %r12
cmpq %r12, %r15
jne 0x6356b
movq %r15, %rax
incq %r15
movq %rax, %r12
cmpq %rbp, %r15
jne 0x634d3
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq