// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, -0x18(%rsp)
movq 0x131eea(%rip), %r11    # 0x296530
testq %r11, %r11
je 0x1649d8
movq %rcx, %r14
movq 0x40(%r11), %rbx
movq $-0x1, %rdx
movl %ebx, %ecx
shlq %cl, %rdx
xorl %eax, %eax
movq %rax, (%r9)
movq %rax, (%r8)
testq %rsi, %rsi
je 0x1649d1
movq %r8, -0x28(%rsp)
movq %r9, -0x30(%rsp)
notq %rdx
movq %rdx, -0x8(%rsp)
leaq -0x1(%rbx), %rax
movq %rax, -0x10(%rsp)
xorl %r15d, %r15d
xorl %r10d, %r10d
xorl %ebp, %ebp
xorl %eax, %eax
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rsi, -0x40(%rsp)
movq %rdi, -0x48(%rsp)
movq %r14, -0x20(%rsp)
movq %r11, -0x60(%rsp)
movq %rbx, -0x58(%rsp)
movzbl (%rdi,%r15), %ecx
leaq 0xd555d(%rip), %rdx     # 0x239c20
movzbl (%rdx,%rcx), %edx
cmpq $0x3, %rdx
ja 0x164737
movq %r10, %r12
movq %rax, %r10
movl %edx, %eax
andl $0x1, %eax
leaq (%rax,%r9,2), %r9
movq -0x8(%rsp), %rcx
andq %rcx, %r9
addq %r8, %r8
shrl %edx
orq %rdx, %r8
andq %rcx, %r8
shrq %r10
xorl $0x1, %eax
movq -0x10(%rsp), %rcx
shlq %cl, %rax
orq %r10, %rax
movq %rbp, %r10
movl $0x1, %ebp
subl %edx, %ebp
shlq %cl, %rbp
shrq %r10
orq %r10, %rbp
movq %r12, %r10
incq %r10
cmpq %rbx, %r10
jb 0x1648a7
cmpq %rbp, %r8
jne 0x164749
cmpq %rax, %r9
jne 0x164749
movq %r9, %rax
movq %r8, %rbp
jmp 0x1648a7
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %eax, %eax
xorl %ebp, %ebp
xorl %r10d, %r10d
jmp 0x1648a7
setae %cl
testb %cl, %cl
movq %r9, %rdx
cmovneq %rax, %rdx
cmpq $0x20, %rbx
movq %r10, -0x50(%rsp)
ja 0x164775
shlq $0x20, %rdx
testb %cl, %cl
movq %r8, %rbx
cmovneq %rbp, %rbx
orq %rdx, %rbx
jmp 0x1647ff
movq %rdx, %rsi
notq %rsi
shlq $0x15, %rdx
addq %rsi, %rdx
movq %rdx, %rsi
shrq $0x18, %rsi
xorq %rdx, %rsi
imulq $0x109, %rsi, %rdx      # imm = 0x109
movq %rdx, %rsi
shrq $0xe, %rsi
xorq %rdx, %rsi
leaq (%rsi,%rsi,4), %rdx
leaq (%rsi,%rdx,4), %rdx
movq %rdx, %rsi
shrq $0x1c, %rsi
xorq %rdx, %rsi
testb %cl, %cl
movq %r8, %rcx
cmovneq %rbp, %rcx
movq %rcx, %rdx
notq %rdx
shlq $0x15, %rcx
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x18, %rdx
xorq %rcx, %rdx
imulq $0x109, %rdx, %rcx      # imm = 0x109
movq %rcx, %rdx
shrq $0xe, %rdx
xorq %rcx, %rdx
leaq (%rdx,%rdx,4), %rcx
leaq (%rdx,%rcx,4), %rcx
movq %rcx, %rdx
shrq $0x1c, %rdx
xorq %rcx, %rdx
addq %rsi, %rdx
movq %rdx, %rbx
shlq $0x1f, %rbx
addq %rdx, %rbx
movb 0x50(%r11), %cl
movq $-0x1, %r10
shlq %cl, %r10
notq %r10
movq 0x78(%r11), %rcx
andq %rbx, %r10
shlq $0x5, %r10
movq (%rcx,%r10), %r11
movq 0x10(%r11), %rdx
testq %rdx, %rdx
je 0x16488e
movq %rcx, %r14
movb (%r11), %cl
movl $0x1, %edi
shll %cl, %edi
movq %rbx, %rsi
shrq $0xc, %rsi
imull $0x9e3779b9, %esi, %esi # imm = 0x9E3779B9
negb %cl
shrl %cl, %esi
addq %r10, %r14
movq %r14, -0x38(%rsp)
leal -0x1(%rdi), %ecx
movq 0x8(%r11), %r14
movl %esi, %r11d
movl %r11d, %r10d
shrl $0x5, %r10d
movl (%r14,%r10,4), %r10d
btl %r11d, %r10d
jae 0x16488e
movl %r11d, %r12d
shlq $0x4, %r12
movq (%rdx,%r12), %r13
movq %r13, %r10
xorq %rbx, %r10
cmpq $0x1000, %r10           # imm = 0x1000
jb 0x1648b8
incl %r11d
andl %ecx, %r11d
cmpl %esi, %r11d
jne 0x164858
movq -0x40(%rsp), %rsi
movq -0x48(%rsp), %rdi
movq -0x60(%rsp), %r11
movq -0x58(%rsp), %rbx
movq -0x50(%rsp), %r10
incq %r15
cmpq %rsi, %r15
jne 0x1646b7
jmp 0x1649d1
cmpl %edi, %r11d
movq -0x40(%rsp), %rsi
movq -0x50(%rsp), %r10
jne 0x1648d8
movq -0x48(%rsp), %rdi
movq -0x60(%rsp), %r11
movq -0x58(%rsp), %rbx
jmp 0x1648a7
movq 0x8(%rdx,%r12), %rbx
movl $0xfff, %edi            # imm = 0xFFF
andq %rdi, %r13
cmpq %rdi, %r13
jne 0x16492d
movq -0x38(%rsp), %rdi
cmpl 0x18(%rdi), %r11d
jne 0x1648fb
movq 0x8(%rdi), %r13
jmp 0x16492a
cmpl %r11d, %ecx
je 0x164921
incl %r11d
movl %r11d, %edi
shrl $0x5, %edi
movl (%r14,%rdi,4), %edi
btl %r11d, %edi
jae 0x1648fb
movl %r11d, %ecx
shlq $0x4, %rcx
movq 0x8(%rdx,%rcx), %r13
jmp 0x16492a
movq -0x38(%rsp), %rcx
movq 0x8(%rcx), %r13
subq %rbx, %r13
testq %r13, %r13
movq -0x48(%rsp), %rdi
je 0x164898
shlq $0x3, %rbx
movq -0x38(%rsp), %rcx
addq 0x10(%rcx), %rbx
movl $0x40, %ecx
movq -0x60(%rsp), %rdx
subq 0x18(%rdx), %rcx
xorl %r10d, %r10d
xorl %edx, %edx
movq -0x20(%rsp), %r14
movq (%rbx,%r10,8), %r11
addq %r11, %r11
shrq %cl, %r11
cmpq -0x18(%rsp), %r11
movl $0x1, %r12d
cmoveq %r12, %rdx
cmpq %r14, %r11
je 0x1649a4
incq %r10
cmpq %r10, %r13
jne 0x164960
movq -0x30(%rsp), %rcx
testq %rdx, %rdx
movq -0x60(%rsp), %r11
movq -0x58(%rsp), %rbx
movq -0x50(%rsp), %r10
jne 0x1649c9
jmp 0x1648a7
testq %rdx, %rdx
je 0x164898
movq -0x30(%rsp), %rcx
incq (%rcx)
movq -0x28(%rsp), %rcx
movq -0x60(%rsp), %r11
movq -0x58(%rsp), %rbx
movq -0x50(%rsp), %r10
incq (%rcx)
jmp 0x1648a7
movl $0x1, %eax
jmp 0x1649da
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq