// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, 0x1b59e5(%rip)    # 0x3c55d0
jne 0x20fbfa
movq $0x0, -0x8(%rbp)
jmp 0x20fed3
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x1b59bf(%rip), %rax    # 0x3c55d0
movq 0x40(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rcx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0x80(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x20fecb
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x17ab2c(%rip), %rax    # 0x38a7e0
movzbl (%rax,%rcx), %eax
movl %eax, -0xbc(%rbp)
cmpl $0x4, -0xbc(%rbp)
jge 0x20fe84
movq -0xa0(%rbp), %rax
shlq %rax
movl -0xbc(%rbp), %ecx
andl $0x1, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
andq -0xa8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
shlq %rax
movl -0xbc(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
orq %rcx, %rax
andq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
shrq %rax
movl -0xbc(%rbp), %edx
andl $0x1, %edx
movl $0x1, %ecx
subl %edx, %ecx
movslq %ecx, %rdx
movq -0xb0(%rbp), %rcx
shlq %cl, %rdx
movq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
shrq %rax
movl -0xbc(%rbp), %edx
sarl %edx
movl $0x1, %ecx
subl %edx, %ecx
movslq %ecx, %rdx
movq -0xb0(%rbp), %rcx
shlq %cl, %rdx
movq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq -0x80(%rbp), %rax
jb 0x20fe82
leaq -0xa0(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq -0x58(%rbp), %rsi
callq 0x212f90
movq %rax, -0xb8(%rbp)
cmpq $-0x1, -0x58(%rbp)
jne 0x20fdbc
jmp 0x20feba
movq 0x1b580d(%rip), %rdi    # 0x3c55d0
movq -0xb8(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x213060
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
ja 0x20fde3
jmp 0x20feba
movq $0x0, -0x48(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x20fe51
movq -0x60(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
movq 0x1b57bd(%rip), %rdx    # 0x3c55d0
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x20fe35
movq $0x1, -0x78(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x20fe41
jmp 0x20fe51
jmp 0x20fe43
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x20fdf3
cmpq $0x0, -0x78(%rbp)
je 0x20fe80
movq -0x38(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x20fe7e
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x20fe80
jmp 0x20fe82
jmp 0x20feb8
movq $0x0, -0x50(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
jmp 0x20feba
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x20fc91
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq