// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: count_unique_k_mers(char*, unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long*)
uint64_t count_unique_k_mers(char *r, uint64_t len, uint64_t query, uint64_t target, uint64_t *all, uint64_t *found)
{
    if(!ug_index) return 0;
    uint64_t i, j, l = 0, skip, *pos_list = NULL, cnt, uID, is_q, k_mer = ug_index->k;
    uint64_t x[4], mask = (1ULL<<k_mer) - 1, shift = k_mer - 1, hash;
    (*all) = (*found) = 0;
    for (i = l = 0, x[0] = x[1] = x[2] = x[3] = 0; i < len; ++i) {
        int c = seq_nt4_table[(uint8_t)r[i]];
        ///c = 00, 01, 10, 11
        if (c < 4) { // not an "N" base
            ///x[0] & x[1] are the forward k-mer
            ///x[2] & x[3] are the reverse complementary k-mer
            x[0] = (x[0] << 1 | (c&1))  & mask;
            x[1] = (x[1] << 1 | (c>>1)) & mask;
            x[2] = x[2] >> 1 | (uint64_t)(1 - (c&1))  << shift;
            x[3] = x[3] >> 1 | (uint64_t)(1 - (c>>1)) << shift;
            if (++l >= k_mer)
            {
                hash = hc_hash_long(x, &skip, k_mer);
                if(skip == (uint64_t)-1) continue;
                cnt = get_hc_pt1_count((ha_ug_index*)ug_index, hash, &pos_list);
                if(cnt <= 0) continue;
                
                for (j = 0, is_q = 0; j < cnt; j++)
                {
                    uID = (pos_list[j] << 1) >> (64 - ug_index->uID_bits);
                    if(query == uID) is_q = 1;
                    if(target == uID) break;
                }

                if(is_q)
                {
                    (*found)++;
                    if(j < cnt) (*all)++;
                }
            }
            
        } else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq 0x1350ca(%rip), %rax    # 0x254530
testq %rax, %rax
je 0x11f671
movq %rcx, %r15
movq %rdx, %r12
leaq 0x48(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x40(%rax), %rdx
pushq $-0x1
popq %rbp
movl %edx, %ecx
shlq %cl, %rbp
movq %r9, 0x58(%rsp)
andq $0x0, (%r9)
movq %r8, 0x50(%rsp)
andq $0x0, (%r8)
notq %rbp
leaq -0x1(%rdx), %rcx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
xorl %r13d, %r13d
leaq 0xcc644(%rip), %r11     # 0x1ebb00
pushq $0x1
popq %r14
xorl %ebx, %ebx
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rcx, (%rsp)
cmpq %rsi, %rbx
je 0x11f674
movzbl (%rdi,%rbx), %eax
movzbl (%r11,%rax), %eax
cmpq $0x3, %rax
ja 0x11f61f
movl %eax, %r9d
andl $0x1, %r9d
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %r8
leaq (%r9,%r10,2), %r10
andq %rbp, %r10
movq %r10, 0x20(%rsp)
addq %r8, %r8
shrl %eax
movq %rax, %r10
orq %r8, %r10
andq %rbp, %r10
movq %r10, 0x28(%rsp)
xorl $0x1, %r9d
shlq %cl, %r9
movq 0x30(%rsp), %r8
shrq %r8
orq %r8, %r9
movq %r9, 0x30(%rsp)
movq 0x38(%rsp), %r9
shrq %r9
pushq $0x1
popq %r8
subl %eax, %r8d
shlq %cl, %r8
orq %r9, %r8
movq %r8, 0x38(%rsp)
incq %r13
cmpq %rdx, %r13
jb 0x11f654
movq %r12, %r14
movq %r15, %r12
movq %rbp, %r15
movq %r11, %rbp
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x120a71
cmpq $-0x1, 0x60(%rsp)
je 0x11f62e
movq 0x134fa4(%rip), %rdi    # 0x254530
movq %rax, %rsi
leaq 0x48(%rsp), %rdx
callq 0x120ad7
testq %rax, %rax
je 0x11f62e
movq 0x48(%rsp), %rdi
movq 0x134f82(%rip), %r8     # 0x254530
xorl %esi, %esi
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movq %rbp, %r11
movq %r15, %rbp
movq %r12, %r15
movq %r14, %r12
pushq $0x1
popq %r14
movq %rcx, %rdx
cmpq %rcx, %rax
je 0x11f5f0
movq (%rdi,%rdx,8), %r9
addq %r9, %r9
movb 0x18(%r8), %r10b
xorl %ecx, %ecx
subb %r10b, %cl
shrq %cl, %r9
cmpq %r12, %r9
cmoveq %r14, %rsi
leaq 0x1(%rdx), %rcx
cmpq %r15, %r9
jne 0x11f5c5
testq %rsi, %rsi
je 0x11f65c
movq 0x58(%rsp), %rcx
incq (%rcx)
cmpq %rax, %rdx
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
jae 0x11f654
movq 0x50(%rsp), %rax
incq (%rax)
jmp 0x11f654
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
jmp 0x11f654
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
xorps %xmm0, %xmm0
movq %rbp, %r11
movq %r15, %rbp
movq %r12, %r15
movq %r14, %r12
pushq $0x1
popq %r14
incq %rbx
jmp 0x11f4d5
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
jmp 0x11f654
xorl %r14d, %r14d
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq