// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: count_k_hits_adv(All_reads*, ul_idx_t const*, char*, UC_Read*, overlap_region_alloc*, Candidates_list*, asg64_v*, Chain_Data*, double, unsigned long, unsigned long)
void count_k_hits_adv(All_reads *rref, const ul_idx_t *uref, char* qstr, UC_Read *buf, 
overlap_region_alloc* ol, Candidates_list *cl, asg64_v* ii, Chain_Data *dp, double sgap_rate, 
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;
        refine_khits(&(ol->list[k]), cl, dp, sgap_rate);
    }
    radix_sort_bc64(ii->a, ii->a+ii->n); 
    // prt_khit(cl, ol, NULL, 109111, "a");
    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 && cl->list[i].readID != ((uint32_t)(0x7fffffff)); 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);
            // if(z->y_id == 109111) {
            //     fprintf(stderr, "[M::%s::m->%ld] qoff::%u, toff::%u\n", __func__, m, 
            //                         cl->list[m].self_offset, cl->list[m].offset);
            // }
            m++;
        }
    }
    cl->length = cn = m; srt = ii->a+on; srt_n = ii->n-on;
    // prt_khit(cl, ol, NULL, 109111, "b");
    radix_sort_bc64(srt, srt+srt_n);
    if(basec) {
        resize_UC_Read(buf, (khit<<1)); str0 = buf->seq; str1 = buf->seq + khit;
    }
    // prt_khit(cl, ol, NULL, 109111, "c");
    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;
        }
    }
    // prt_khit(cl, ol, NULL, 109111, "d");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
movq 0xa0(%rsp), %rbp
movq 0x10(%r8), %r14
movq 0x8(%r9), %rax
movq %rax, 0x60(%rsp)
cmpq %r14, 0x8(%rbp)
movq %r9, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
jae 0x62e91
leaq -0x1(%r14), %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(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x20(%rsp), %r9
movq %rax, 0x10(%rbp)
movq %rbx, 0x48(%rsp)
movq $0x0, (%rbp)
movq %r14, 0x38(%rsp)
testq %r14, %r14
jle 0x62f0e
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %eax, %eax
movq %r9, %r15
movq 0x38(%rsp), %r13
movq (%r12), %rdi
movslq 0x24(%rdi,%rbx), %rcx
addq %rbx, %rdi
movq 0x10(%rbp), %rdx
movq %rcx, (%rdx,%rax,8)
movq (%rbp), %rax
shlq $0x20, (%rdx,%rax,8)
movq (%rbp), %rax
orq %r14, (%rdx,%rax,8)
movq %r15, %rsi
movq 0xa8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
callq 0x62912
incq %r14
movq (%rbp), %rax
incq %rax
movq %rax, (%rbp)
addq $0xa8, %rbx
cmpq %r14, %r13
jne 0x62eb7
jmp 0x62f15
xorl %eax, %eax
movq 0x38(%rsp), %r13
movq 0x10(%rbp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
testq %r13, %r13
movq %r12, 0x40(%rsp)
jle 0x63084
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl $0x80000000, %r10d      # imm = 0x80000000
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movl (%rcx,%r8,8), %ecx
imulq $0xa8, %rcx, %rcx
movslq 0x24(%rax,%rcx), %rdx
movq (%rdi), %rsi
movq %rdx, %r15
shlq $0x4, %r15
movl (%rsi,%r15), %esi
andl %r9d, %esi
movl %r13d, 0x24(%rax,%rcx)
movq 0x60(%rsp), %rbx
subq %rdx, %rbx
jle 0x63071
movq %r13, %r12
shlq $0x4, %r12
movl %esi, 0x30(%rsp)
movq (%rdi), %rax
movl (%rax,%r15), %ecx
andl %r9d, %ecx
cmpl %esi, %ecx
jne 0x63071
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x63071
leaq (%rax,%r15), %rcx
movups (%rcx), %xmm0
movups %xmm0, (%rax,%r12)
movq 0x10(%rbp), %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), %r14d
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x63042
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x3, %rsi
movq %r13, 0x28(%rsp)
movq %rbp, %r13
movq %r8, %rbp
callq 0x2370
movl 0x30(%rsp), %esi
movl $0x80000000, %r10d      # imm = 0x80000000
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq %rbp, %r8
movq %r13, %rbp
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%rbp)
shlq $0x20, %r14
orq %r13, %r14
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movq %r14, (%rcx,%rax,8)
incq %r13
addq $0x10, %r12
addq $0x10, %r15
decq %rbx
jne 0x62f92
incq %r8
movq 0x38(%rsp), %rcx
cmpq %rcx, %r8
jne 0x62f47
jmp 0x6308f
movq %r13, %rcx
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
movq 0xb0(%rsp), %rbx
movq %r13, 0x8(%rdi)
movq (%rbp), %r14
movq 0x10(%rbp), %rax
leaq (%rax,%rcx,8), %r12
leaq (%rax,%r14,8), %rsi
subq %rcx, %r14
movq %r12, %rdi
callq 0x45d47
cmpq $0x0, 0xb8(%rsp)
je 0x630fc
leaq (%rbx,%rbx), %r15
movq 0x48(%rsp), %rbx
cmpq %r15, 0x10(%rbx)
jge 0x630e2
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r15, 0x10(%rbx)
movq (%rbx), %rdx
movq 0xb0(%rsp), %rax
movq %rdx, 0x28(%rsp)
addq %rdx, %rax
movq %rax, 0x30(%rsp)
jmp 0x6310e
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x20(%rsp), %rcx
testq %r14, %r14
jle 0x631dc
incq %r14
xorl %ebp, %ebp
movl $0x1, %ebx
cmpq %r13, %rbx
je 0x63141
movq (%r12,%rbx,8), %rax
xorq (%r12,%rbp,8), %rax
shrq $0x20, %rax
jne 0x63141
movq %rbp, %rax
jmp 0x631cd
cmpq $0x0, 0xb8(%rsp)
setne %al
movq %rbx, %r15
subq %rbp, %r15
cmpq $0x2, %r15
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x631aa
leaq (%r12,%rbp,8), %r9
movq 0x30(%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 0x40(%rsp), %rdx
movq 0xb0(%rsp), %r8
callq 0x62475
movq 0x20(%rsp), %rcx
testq %rax, %rax
movl $0x1, %eax
cmoveq %rax, %r15
movq %rbx, %rax
cmpq %rbp, %rbx
jle 0x631cd
movq (%rcx), %rax
movl (%r12,%rbp,8), %edx
shlq $0x4, %rdx
movl %r15d, 0xc(%rax,%rdx)
incq %rbp
cmpq %rbp, %rbx
jne 0x631b5
movq %rbx, %rax
incq %rbx
movq %rax, %rbp
cmpq %r14, %rbx
jne 0x63126
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq