// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xd8, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x5c7d7
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq 0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq 0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq 0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq 0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq 0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x5c7d9
movq $0x0, -0x40(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x5c874
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x40(%rbp), %rcx
addq %rcx, %rax
movslq 0x24(%rax), %rdx
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rcx
movq (%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rcx
movq (%rcx), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x5c7ec
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x10(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2da20
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x5cad7
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movslq 0x24(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x90(%rbp)
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0x68(%rbp), %rcx
movb %al, -0xa9(%rbp)
jge 0x5c954
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
cmpq -0x90(%rbp), %rax
sete %al
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x5c963
jmp 0x5cac4
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x5ca86
movq 0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x5ca45
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x5ca53
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x5ca53
movq -0xb8(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x80(%rbp), %rdx
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x5c917
jmp 0x5cac6
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x5c8a7
movq -0x58(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
subq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x98(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x70(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2da20
cmpq $0x0, 0x20(%rbp)
je 0x5cb65
movq -0x28(%rbp), %rdi
movq 0x18(%rbp), %rsi
shlq %rsi
callq 0xa62c0
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
addq 0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq $0x1, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x5ccad
movq -0x40(%rbp), %rax
cmpq -0x68(%rbp), %rax
je 0x5cbbc
movq -0x98(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x98(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x5cc9a
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, 0x20(%rbp)
je 0x5cc47
cmpq $0x1, -0x88(%rbp)
jbe 0x5cc47
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x18(%rbp), %r8
movq -0x98(%rbp), %r9
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %r9
movq -0x40(%rbp), %rbx
subq -0x48(%rbp), %rbx
movq -0x20(%rbp), %r11
movq -0xa0(%rbp), %r10
movq -0xa8(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x5ae80
cmpq $0x0, %rax
jne 0x5cc45
movq $0x1, -0x88(%rbp)
jmp 0x5cc47
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x5cc92
movq -0x88(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rdx
movq -0x60(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x5cc4f
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x5cc9c
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x5cb75
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)