// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rcx, -0x10(%rsp)
movq 0x12d9a5(%rip), %r11    # 0x28b530
testq %r11, %r11
je 0x15dfd3
movq %rsi, %rbp
movq 0x40(%r11), %rsi
movq $-0x1, %r14
movl %esi, %ecx
shlq %cl, %r14
xorl %eax, %eax
movq %r9, (%rsp)
movq %rax, (%r9)
movq %r8, -0x8(%rsp)
movq %rax, (%r8)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rsp)
movaps %xmm0, -0x70(%rsp)
testq %rbp, %rbp
je 0x15dfcc
movq %rdx, %r12
notq %r14
leaq -0x1(%rsi), %rax
movq %rax, 0x10(%rsp)
movq -0x70(%rsp), %r9
movq -0x68(%rsp), %r13
movq -0x60(%rsp), %rbx
movq -0x58(%rsp), %r8
xorl %r15d, %r15d
xorl %r10d, %r10d
movq $0x0, -0x30(%rsp)
movq %rdx, -0x40(%rsp)
movq %rdi, -0x48(%rsp)
movq %r11, 0x8(%rsp)
movq %rsi, -0x20(%rsp)
movq %r14, -0x28(%rsp)
movzbl (%rdi,%r15), %eax
leaq 0xcfdeb(%rip), %rcx     # 0x22da10
movzbl (%rcx,%rax), %eax
cmpq $0x3, %rax
ja 0x15dcd1
movq %rbx, %rdx
movl %eax, %ebx
andl $0x1, %ebx
leaq (%rbx,%r9,2), %r9
andq %r14, %r9
movq %r9, -0x70(%rsp)
addq %r13, %r13
shrl %eax
orq %rax, %r13
andq %r14, %r13
movq %r13, -0x68(%rsp)
xorl $0x1, %ebx
movq 0x10(%rsp), %rcx
shlq %cl, %rbx
shrq %rdx
orq %rdx, %rbx
movq %rbx, -0x60(%rsp)
movq %r8, %rdx
shrq %rdx
movl $0x1, %r8d
subl %eax, %r8d
shlq %cl, %r8
orq %rdx, %r8
movq %r8, -0x58(%rsp)
incq %r10
cmpq %rsi, %r10
jb 0x15dce9
cmpq %r8, %r13
jne 0x15dca0
movq $-0x1, %rax
cmpq %rbx, %r9
je 0x15dca6
setae %al
movzbl %al, %eax
cmpq $-0x1, %rax
movq %rbx, -0x78(%rsp)
je 0x15dcfa
movq %rax, %rcx
shlq $0x4, %rcx
cmpq $0x20, %rsi
ja 0x15dd06
movq -0x70(%rsp,%rcx), %rbx
shlq $0x20, %rbx
orq -0x68(%rsp,%rcx), %rbx
jmp 0x15dd97
movaps %xmm0, -0x60(%rsp)
movaps %xmm0, -0x70(%rsp)
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %r10d, %r10d
incq %r15
cmpq %rbp, %r15
jne 0x15dc19
jmp 0x15dfcc
movq $-0x1, %rbx
jmp 0x15dd97
movq -0x70(%rsp,%rcx), %rdx
movq -0x68(%rsp,%rcx), %rcx
movq %r9, %rbx
movq %rdx, %r9
notq %r9
shlq $0x15, %rdx
addq %r9, %rdx
movq %rdx, %r9
shrq $0x18, %r9
xorq %rdx, %r9
imulq $0x109, %r9, %rdx       # imm = 0x109
movq %rdx, %r9
shrq $0xe, %r9
xorq %rdx, %r9
leaq (%r9,%r9,4), %rdx
leaq (%r9,%rdx,4), %rdx
movq %rdx, %r9
shrq $0x1c, %r9
xorq %rdx, %r9
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 %r9, %rdx
movq %rbx, %r9
movq %rdx, %rbx
shlq $0x1f, %rbx
addq %rdx, %rbx
cmpq $-0x1, %rax
je 0x15de41
movq %r10, -0x18(%rsp)
movb 0x50(%r11), %cl
movq $-0x1, %r14
shlq %cl, %r14
notq %r14
movq 0x78(%r11), %r10
andq %rbx, %r14
shlq $0x5, %r14
movq (%r10,%r14), %r11
movq 0x10(%r11), %rax
xorl %esi, %esi
movl $0x0, %edx
testq %rax, %rax
movq %r9, -0x80(%rsp)
je 0x15de64
movq %r10, -0x38(%rsp)
movq %rbp, %rdi
movb (%r11), %cl
movl $0x1, %edx
shll %cl, %edx
movq %rbx, %r9
shrq $0xc, %r9
imull $0x9e3779b9, %r9d, %r10d # imm = 0x9E3779B9
negb %cl
shrl %cl, %r10d
leal -0x1(%rdx), %ecx
movq 0x8(%r11), %r9
movl %r10d, %ebp
movl %ebp, %r12d
shrl $0x5, %r12d
movl (%r9,%r12,4), %r12d
btl %ebp, %r12d
jae 0x15de4d
movl %ebp, %r12d
shlq $0x4, %r12
movq (%rax,%r12), %r12
xorq %rbx, %r12
cmpq $0x1000, %r12           # imm = 0x1000
jb 0x15de4b
incl %ebp
andl %ecx, %ebp
cmpl %r10d, %ebp
jne 0x15de0e
jmp 0x15de4d
movq -0x78(%rsp), %rbx
jmp 0x15dce9
movl %ebp, %edx
movq -0x40(%rsp), %r12
movq %rdi, %rbp
movq -0x48(%rsp), %rdi
movq -0x80(%rsp), %r9
movq -0x38(%rsp), %r10
testq %rax, %rax
je 0x15de73
movb (%r11), %cl
movl $0x1, %esi
shll %cl, %esi
cmpl %esi, %edx
movq -0x78(%rsp), %rbx
jne 0x15de81
xorl %r14d, %r14d
jmp 0x15deff
addq %r14, %r10
movl %edx, %ecx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r9
leaq (,%r9,8), %rsi
addq 0x10(%r10), %rsi
movq %rsi, -0x30(%rsp)
movq (%rax,%rcx), %r14
movl $0xfff, %ecx            # imm = 0xFFF
andq %rcx, %r14
cmpq %rcx, %r14
jne 0x15defa
cmpl 0x18(%r10), %edx
jne 0x15debd
movq 0x8(%r10), %r14
jmp 0x15def7
testq %rax, %rax
je 0x15dece
movb (%r11), %cl
movl $0x1, %esi
shll %cl, %esi
jmp 0x15ded0
xorl %esi, %esi
incl %edx
cmpl %esi, %edx
je 0x15deb7
movq 0x8(%r11), %rcx
movl %edx, %ebx
shrl $0x5, %ebx
movl (%rcx,%rbx,4), %ecx
movq -0x78(%rsp), %rbx
btl %edx, %ecx
jae 0x15debd
movl %edx, %ecx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r14
subq %r9, %r14
movq -0x80(%rsp), %r9
testq %r14, %r14
movq 0x8(%rsp), %r11
je 0x15dfb8
movq 0x18(%r11), %rdx
movq -0x30(%rsp), %rax
movq (%rax), %rsi
addq %rsi, %rsi
movq %rdx, %rcx
negq %rcx
shrq %cl, %rsi
xorl %eax, %eax
cmpq %r12, %rsi
sete %al
movb $0x1, %cl
cmpq -0x10(%rsp), %rsi
je 0x15df84
movq %r9, %rbx
movl $0x40, %ecx
subq %rdx, %rcx
movl $0x1, %r9d
movq %r9, %rdx
cmpq %r9, %r14
je 0x15df76
movq -0x30(%rsp), %rsi
movq (%rsi,%rdx,8), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
cmpq %r12, %rsi
movl $0x1, %r9d
cmoveq %r9, %rax
leaq 0x1(%rdx), %r9
cmpq -0x10(%rsp), %rsi
jne 0x15df47
cmpq %r14, %rdx
setb %cl
movq %rbx, %r9
movq -0x78(%rsp), %rbx
testq %rax, %rax
movq -0x20(%rsp), %rsi
movq -0x28(%rsp), %r14
movq -0x18(%rsp), %r10
je 0x15dce9
movq (%rsp), %rax
incq (%rax)
testb %cl, %cl
je 0x15dce9
movq -0x8(%rsp), %rax
incq (%rax)
jmp 0x15dce9
movq -0x20(%rsp), %rsi
movq -0x28(%rsp), %r14
movq -0x18(%rsp), %r10
jmp 0x15dce9
movl $0x1, %eax
jmp 0x15dfd5
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq