// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r8, %r13
movq %rcx, 0x48(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, 0x60(%rsp)
movq 0xb0(%rsp), %rsi
movq 0x10(%r8), %rbp
movq 0x8(%r9), %rax
movq %rax, 0x30(%rsp)
cmpq %rbp, 0x8(%rsi)
movq %r9, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
jae 0x629c7
leaq -0x1(%rbp), %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 0xb0(%rsp), %rsi
movq 0x20(%rsp), %r9
movq %rax, 0x10(%rsi)
movq $0x0, (%rsi)
testq %rbp, %rbp
movq %r13, 0x40(%rsp)
jle 0x62c05
movq 0xb8(%rsp), %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %eax, %eax
movq %r9, %r12
movq (%r13), %rdi
movslq 0x24(%rdi,%rbx), %rcx
addq %rbx, %rdi
movq 0x10(%rsi), %rdx
movq %rcx, (%rdx,%rax,8)
movq (%rsi), %rax
shlq $0x20, (%rdx,%rax,8)
movq (%rsi), %rax
orq %r14, (%rdx,%rax,8)
movq %r12, %rsi
movq %r15, %rdx
movsd 0x28(%rsp), %xmm0
callq 0x62435
movq 0xb0(%rsp), %rsi
incq %r14
movq (%rsi), %rax
incq %rax
movq %rax, (%rsi)
addq $0xa8, %rbx
cmpq %r14, %rbp
jne 0x629ee
movq 0x10(%rsi), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45373
movq 0xb0(%rsp), %r8
movq 0x10(%r8), %rax
testq %rbp, %rbp
jle 0x62c26
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 %rbp, 0x70(%rsp)
movq (%r13), %rcx
movl (%rax,%r9,8), %edx
imulq $0xa8, %rdx, %rdx
movslq 0x24(%rcx,%rdx), %r13
movq (%rdi), %rsi
movq %r13, %r12
shlq $0x4, %r12
movl (%rsi,%r12), %r15d
andl %r10d, %r15d
movl %ebx, 0x24(%rcx,%rdx)
cmpq %r13, 0x30(%rsp)
jle 0x62bed
movq (%rdi), %rcx
movl (%rcx,%r12), %edx
andl %r10d, %edx
cmpl %r15d, %edx
setne %sil
cmpl %r10d, %edx
sete %dl
orb %sil, %dl
jne 0x62bed
movq 0x30(%rsp), %rax
addq %rbx, %rax
subq %r13, %rax
movq %rax, 0x58(%rsp)
leaq (%rcx,%r12), %rdx
movq %rbx, %rbp
shlq $0x4, %rbp
movq 0x50(%rsp), %r14
subq %r13, %r14
movl %r15d, 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), %r15d
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x62b95
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, 0x38(%rsp)
movq %r9, %rbx
callq 0x2370
movl $0x80000000, %r11d      # imm = 0x80000000
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %r9
movq 0x38(%rsp), %rbx
movq 0xb0(%rsp), %r8
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%r8)
movq (%r8), %rcx
shlq $0x20, %r15
orq %rbx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r15, (%rax,%rcx,8)
subq $0x1, %r14
jb 0x62be8
movq (%rdi), %rcx
movl 0x10(%rcx,%r12), %esi
andl %r10d, %esi
incq %rbx
movl 0x28(%rsp), %r15d
cmpl %r15d, %esi
jne 0x62bed
incq %r13
movq %r13, %rdx
shlq $0x4, %rdx
addq %rcx, %rdx
addq $0x10, %r12
addq $0x10, %rbp
cmpl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
jne 0x62b08
jmp 0x62bed
movq 0x58(%rsp), %rbx
incq %r9
movq 0x70(%rsp), %rbp
cmpq %rbp, %r9
movq 0x40(%rsp), %r13
jne 0x62a8c
jmp 0x62c2d
movq 0x10(%rsi), %rdi
movq %rdi, %rsi
callq 0x45373
movq 0xb0(%rsp), %r8
movq 0x20(%rsp), %rdi
movq 0x10(%r8), %rax
xorl %ebx, %ebx
jmp 0x62c2d
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
movq 0xc0(%rsp), %r15
movq %rbx, 0x8(%rdi)
leaq (%rax,%rbp,8), %r13
movq (%r8), %r14
leaq (%rax,%r14,8), %rsi
subq %rbp, %r14
movq %r13, %rdi
callq 0x45373
cmpq $0x0, 0xc8(%rsp)
je 0x62c97
leaq (%r15,%r15), %r12
movq 0x48(%rsp), %rax
movq (%rax), %rdi
cmpq %r12, 0x10(%rax)
jge 0x62c87
movq %r12, %rsi
movq %r15, %rbp
movq %rax, %r15
callq 0x2370
movq %rax, %rdi
movq %rax, (%r15)
movq %r12, 0x10(%r15)
movq %rbp, %r15
movq 0x20(%rsp), %rcx
leaq (%rdi,%r15), %rax
movq %rax, 0x28(%rsp)
jmp 0x62ca7
xorl %edi, %edi
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rcx
testq %r14, %r14
jle 0x62d86
incq %r14
xorl %r12d, %r12d
movl $0x1, %ebp
movq %r14, 0x38(%rsp)
cmpq %rbx, %rbp
je 0x62cdd
movq (%r13,%rbp,8), %rax
xorq (%r13,%r12,8), %rax
shrq $0x20, %rax
jne 0x62cdd
movq %r12, %rax
jmp 0x62d77
cmpq $0x0, 0xc8(%rsp)
setne %al
movq %rbp, %r15
subq %r12, %r15
cmpq $0x2, %r15
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x62d53
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 0x40(%rsp), %rdx
movq 0xc0(%rsp), %r8
callq 0x61f67
movq %r14, %rdi
movq 0x38(%rsp), %r14
movq 0x20(%rsp), %rcx
testq %rax, %rax
movl $0x1, %eax
cmoveq %rax, %r15
movq %rbp, %rax
cmpq %r12, %rbp
jle 0x62d77
movq (%rcx), %rax
movl (%r13,%r12,8), %edx
shlq $0x4, %rdx
movl %r15d, 0xc(%rax,%rdx)
incq %r12
cmpq %r12, %rbp
jne 0x62d5e
movq %rbp, %rax
incq %rbp
movq %rax, %r12
cmpq %r14, %rbp
jne 0x62cc0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq