// File: /workspace/github_cpp_makefile_star2_O2/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
movsd %xmm0, 0x20(%rsp)
movq %r9, %rbx
movq %rcx, 0x40(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
movq 0xa0(%rsp), %rsi
movq %r8, 0x38(%rsp)
movq 0x10(%r8), %r15
movq 0x8(%r9), %r12
cmpq %r15, 0x8(%rsi)
jae 0x32387
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(%rsi)
movq 0x10(%rsi), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0xa0(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq 0xa8(%rsp), %rbp
xorl %r14d, %r14d
testq %r15, %r15
movl $0x0, %r13d
movq %r15, 0x48(%rsp)
cmovgq %r15, %r13
xorl %eax, %eax
xorl %r15d, %r15d
movq %rax, (%rsi)
cmpq %r15, %r13
je 0x32403
movq 0x38(%rsp), %rcx
movq (%rcx), %rdi
movslq 0x24(%rdi,%r14), %rcx
addq %r14, %rdi
movq 0x10(%rsi), %rdx
movq %rcx, (%rdx,%rax,8)
movq (%rsi), %rax
shlq $0x20, (%rdx,%rax,8)
movq (%rsi), %rax
orq %r15, (%rdx,%rax,8)
movq %rbx, %rsi
movq %rbp, %rdx
movsd 0x20(%rsp), %xmm0
callq 0x31e59
movq 0xa0(%rsp), %rsi
incq %r15
movq (%rsi), %rax
incq %rax
addq $0xa8, %r14
jmp 0x323a9
movq 0x10(%rsi), %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 %r13, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rbx, 0x60(%rsp)
cmpq %r13, %r10
je 0x32571
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), %ebp
andl %r8d, %ebp
movl %esi, 0x24(%rax,%rcx)
movq %rsi, %r15
shlq $0x4, %r15
cmpq %r12, %r11
jge 0x32564
movq (%rbx), %rax
movl (%rax,%r13), %ecx
andl %r8d, %ecx
cmpl %ebp, %ecx
jne 0x32564
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x32564
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 0x3253f
movq %rsi, 0x20(%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 %ebp, %ebx
movq %r10, %rbp
movq %r11, %r12
callq 0x2370
movq %r12, %r11
movq %rbp, %r10
movl %ebx, %ebp
movl $0x80000000, %r9d       # imm = 0x80000000
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0xa0(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x60(%rsp), %rbx
movq 0x30(%rsp), %r12
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 0x32479
incq %r10
movq 0x28(%rsp), %r13
jmp 0x32438
movq %rsi, 0x20(%rsp)
movq %rsi, 0x8(%rbx)
movq (%rdi), %r14
movq 0x10(%rdi), %rax
movq 0x48(%rsp), %rcx
leaq (%rax,%rcx,8), %rbp
leaq (%rax,%r14,8), %rsi
subq %rcx, %r14
movq %rbp, %rdi
callq 0x1905d
cmpq $0x0, 0xb8(%rsp)
je 0x325da
movq 0xb0(%rsp), %rax
movq %rbx, %r13
movq %rax, %rbx
leaq (%rax,%rax), %rsi
movq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x562fa
movq (%r12), %rcx
movq %rcx, 0x30(%rsp)
leaq (%rcx,%rbx), %rax
movq %rax, 0x28(%rsp)
movq %r13, %rbx
jmp 0x325ec
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r13d, %r13d
testq %r14, %r14
cmovleq %r13, %r14
incq %r14
pushq $0x1
popq %r15
movq 0x20(%rsp), %rdx
cmpq %r14, %r15
je 0x326bb
cmpq %rdx, %r15
je 0x32624
movq (%rbp,%r15,8), %rax
xorq (%rbp,%r13,8), %rax
shrq $0x20, %rax
je 0x326b3
cmpq $0x0, 0xb8(%rsp)
setne %al
movq %r15, %r12
subq %r13, %r12
cmpq $0x2, %r12
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x326ab
leaq (,%r13,8), %r9
addq %rbp, %r9
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rcx
movq 0xb0(%rsp), %r8
callq 0x31a5b
movq 0x20(%rsp), %rdx
testq %rax, %rax
pushq $0x1
popq %rax
cmoveq %rax, %r12
jmp 0x326ab
movq (%rbx), %rax
movl (%rbp,%r13,8), %ecx
shlq $0x4, %rcx
movl %r12d, 0xc(%rax,%rcx)
incq %r13
cmpq %r15, %r13
jl 0x32697
movq %r15, %r13
incq %r15
jmp 0x32602
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq