// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: parallel_count_hc_pt1(pldat_t*)
void parallel_count_hc_pt1(pldat_t* pl)
{
    uint64_t i, l = 0, uID, num_pos = 0, pos_thre;
    uint64_t x[4], mask = (1ULL<<pl->h->k) - 1, shift = pl->h->k - 1, hash, pos, skip, bucket_mask = (1ULL<<pl->h->pre) - 1;
    ma_utg_t *u = NULL;
    ch_buf_t k_pos;
    if(pl->is_cnt) l = ((pl->buf_bytes>>3)/pl->h->tot) + 1, pos_thre = pl->buf_bytes>>3;
    if(!pl->is_cnt) l = ((pl->buf_bytes>>4)/pl->h->tot) + 1, pos_thre = pl->buf_bytes>>4;
    for (i = 0; i < pl->h->tot; i++)
    {
        if(pl->is_cnt)
        {
            kv_resize(uint64_t, pl->cnt[i].a, l);
            pl->cnt[i].a.n = 0;
        } 
        
        if(!pl->is_cnt)
        {
            kv_resize(ch_buf_t, pl->buf[i].a, l);
            pl->buf[i].a.n = 0;
        } 
    }
        
     
    for (uID = 0; uID < pl->h->ug->u.n; uID++)
    {
        u = &(pl->h->ug->u.a[uID]);
        if(u->m == 0) continue;

        for (i = l = 0, x[0] = x[1] = x[2] = x[3] = 0; i < u->len; ++i) {
        int c = seq_nt4_table[(uint8_t)u->s[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 >= pl->h->k)
            {
                hash = hc_hash_long(x, &skip, pl->h->k);
                if(skip == (uint64_t)-1) continue;
                if(pl->is_cnt)
                {
                    kv_push(uint64_t, pl->cnt[hash & bucket_mask].a, hash);
                }
                else
                {
                    pos = (skip << 63) | ((uID << (64-pl->h->uID_bits))>>1) | (i & pl->h->pos_mode);
                    k_pos.key = hash; k_pos.pos = pos;
                    kv_push(ch_buf_t, pl->buf[hash & bucket_mask].a, k_pos);
                }
                num_pos++;

                if(num_pos >= pos_thre)
                {
                    num_pos = 0;
                    kt_for(pl->n_thread, worker_for, pl, pl->h->tot);
                }
            }
                
        } else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
    }
    }

    if(num_pos > 0) kt_for(pl->n_thread, worker_for, pl, pl->h->tot);

    for (i = 0; i < pl->h->tot; i++)
    {
        if(pl->cnt[i].a.m > 0) kv_destroy(pl->cnt[i].a), kv_init(pl->cnt[i].a);
        if(pl->buf[i].a.m > 0) kv_destroy(pl->buf[i].a), kv_init(pl->buf[i].a);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r8
movq 0x10(%rdi), %rsi
movq 0x40(%rsi), %rcx
movq $-0x1, %rax
movq $-0x1, %rdx
shlq %cl, %rdx
notq %rdx
movq %rdx, 0x38(%rsp)
decq %rcx
movq %rcx, 0x20(%rsp)
movb 0x50(%rsi), %cl
shlq %cl, %rax
notq %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rdi), %rax
xorl %ebx, %ebx
cmpl $0x1, (%rdi)
movl $0x0, %ecx
adcb $0x3, %cl
shrq %cl, %rax
movq %rax, 0x70(%rsp)
xorl %edx, %edx
divq 0x58(%rsi)
movq %rax, %r12
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %r14
shrq $0x10, %r14
orq %rcx, %r14
leaq 0x8(,%r14,8), %r13
incq %r14
movq %r14, %rbp
shlq $0x4, %rbp
xorl %r15d, %r15d
movq %rdi, 0x8(%rsp)
cmpl $0x0, (%r8)
je 0x13b1c4
movq 0x18(%r8), %rax
cmpq %r12, 0x8(%rax,%rbx)
jbe 0x13b196
addq %rbx, %rax
jmp 0x13b1f7
movq %r14, 0x8(%rax,%rbx)
movq 0x10(%rax,%rbx), %rdi
movq %r13, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq 0x18(%r8), %rcx
movq %rax, 0x10(%rcx,%rbx)
cmpl $0x0, (%r8)
movq $0x0, (%rcx,%rbx)
jne 0x13b1fe
movq 0x20(%r8), %rcx
cmpq %r12, 0x8(%rcx,%rbx)
ja 0x13b1ef
movq %r14, 0x8(%rcx,%rbx)
movq 0x10(%rcx,%rbx), %rdi
movq %rbp, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq 0x20(%r8), %rcx
movq %rax, 0x10(%rcx,%rbx)
leaq (%r15,%r15,2), %rax
leaq (%rcx,%rax,8), %rax
movq $0x0, (%rax)
incq %r15
movq 0x10(%r8), %rax
addq $0x18, %rbx
cmpq 0x58(%rax), %r15
jb 0x13b180
movq (%rax), %rcx
movq (%rcx), %rdx
testq %rdx, %rdx
je 0x13b5cb
xorl %r9d, %r9d
leaq 0xfe9f4(%rip), %r10     # 0x239c20
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rbp
movq 0x38(%rsp), %r13
movq 0x10(%rcx), %rsi
leaq (%r9,%r9,4), %rdi
cmpl $0x0, 0xc(%rsi,%rdi,8)
je 0x13b597
leaq (%rsi,%rdi,8), %rsi
testl $0x7fffffff, (%rsi)     # imm = 0x7FFFFFFF
je 0x13b597
movq %r9, 0x28(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r9d, %r9d
movq %rsi, 0x40(%rsp)
movq 0x20(%rsi), %rax
movzbl (%rax,%r12), %eax
movzbl (%r10,%rax), %eax
cmpq $0x3, %rax
ja 0x13b2f9
movq %r15, %rdx
movl %eax, %r15d
andl $0x1, %r15d
leaq (%r15,%r9,2), %r9
andq %r13, %r9
addq %rbx, %rbx
shrl %eax
orq %rax, %rbx
andq %r13, %rbx
xorl $0x1, %r15d
movl %ebp, %ecx
shlq %cl, %r15
shrq %rdx
orq %rdx, %r15
movq %r14, %rdx
shrq %rdx
movl $0x1, %r14d
subl %eax, %r14d
shlq %cl, %r14
orq %rdx, %r14
incq %rdi
movq 0x10(%r8), %rax
movq 0x40(%rax), %rcx
cmpq %rcx, %rdi
jb 0x13b575
cmpq %r14, %rbx
jne 0x13b30b
cmpq %r15, %r9
jne 0x13b30b
movq %r9, %r15
movq %rbx, %r14
jmp 0x13b575
xorl %r9d, %r9d
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %edi, %edi
jmp 0x13b575
setae %dl
movzbl %dl, %r10d
testb %r10b, %r10b
movq %r9, %rdx
cmovneq %r15, %rdx
cmpq $0x20, %rcx
ja 0x13b338
shlq $0x20, %rdx
testq %r10, %r10
movq %rbx, %r13
cmovneq %r14, %r13
orq %rdx, %r13
jmp 0x13b3c3
movq %rdx, %rcx
notq %rcx
shlq $0x15, %rdx
addq %rcx, %rdx
movq %rdx, %rcx
shrq $0x18, %rcx
xorq %rdx, %rcx
imulq $0x109, %rcx, %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
testq %r10, %r10
movq %rbx, %rcx
cmovneq %r14, %rcx
movq %rcx, %rsi
notq %rsi
shlq $0x15, %rcx
addq %rsi, %rcx
movq %rcx, %rsi
shrq $0x18, %rsi
xorq %rcx, %rsi
imulq $0x109, %rsi, %rcx      # imm = 0x109
movq %rcx, %rsi
shrq $0xe, %rsi
xorq %rcx, %rsi
leaq (%rsi,%rsi,4), %rcx
leaq (%rsi,%rcx,4), %rcx
movq %rcx, %rsi
shrq $0x1c, %rsi
xorq %rcx, %rsi
addq %rdx, %rsi
movq %rsi, %r13
shlq $0x1f, %r13
addq %rsi, %r13
cmpl $0x0, (%r8)
movq %rdi, 0x50(%rsp)
movq %r9, 0x48(%rsp)
je 0x13b457
movq 0x18(%r8), %rcx
movq %r13, %rdx
andq 0x30(%rsp), %rdx
leaq (%rdx,%rdx,2), %rbp
movq (%rcx,%rbp,8), %rax
cmpq 0x8(%rcx,%rbp,8), %rax
jne 0x13b43c
leaq (%rcx,%rbp,8), %rdi
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rax
movq %rax, 0x18(%rsp)
movq %rsi, 0x8(%rcx,%rax)
movq 0x10(%rdi), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq 0x18(%r8), %rcx
movq 0x18(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx)
movq (%rcx,%rdx), %rax
movq 0x10(%rcx,%rbp,8), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%rbp,8)
movq %r13, (%rdx,%rax,8)
movq 0x20(%rsp), %rbp
jmp 0x13b51d
movq %rbp, %r11
movb 0x18(%rax), %dl
xorl %ecx, %ecx
subb %dl, %cl
movq 0x28(%rsp), %rdx
shlq %cl, %rdx
movq %rdx, 0x18(%rsp)
movq 0x30(%rax), %rsi
andq %r12, %rsi
movq 0x20(%r8), %rcx
movq %r13, %rdx
andq 0x30(%rsp), %rdx
leaq (%rdx,%rdx,2), %rbp
movq (%rcx,%rbp,8), %rax
cmpq 0x8(%rcx,%rbp,8), %rax
jne 0x13b4f3
leaq (%rcx,%rbp,8), %rdi
movq %rsi, 0x60(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rax
movq %rax, 0x58(%rsp)
movq %rsi, 0x8(%rcx,%rax)
movq 0x10(%rdi), %rdi
shlq $0x4, %rsi
movq %r10, 0x68(%rsp)
callq 0x2370
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %r10
movq 0x8(%rsp), %r8
movq 0x20(%r8), %rcx
movq 0x58(%rsp), %rdx
movq %rax, 0x10(%rcx,%rdx)
movq (%rcx,%rdx), %rax
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %rdi
shrdq $0x1, %r10, %rdi
orq %rsi, %rdi
movq 0x10(%rcx,%rbp,8), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%rbp,8)
shlq $0x4, %rax
movq %r13, (%rdx,%rax)
movq %rdi, 0x8(%rdx,%rax)
movq %r11, %rbp
movq 0x10(%rsp), %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
cmpq 0x70(%rsp), %rcx
movq 0x38(%rsp), %r13
jb 0x13b55f
movl 0x28(%r8), %edi
movq 0x10(%r8), %rax
movq 0x58(%rax), %rcx
leaq 0x10c(%rip), %rsi       # 0x13b655
movq %r8, %rdx
callq 0x108f2c
movq 0x8(%rsp), %r8
movq $0x0, 0x10(%rsp)
leaq 0xfe6ba(%rip), %r10     # 0x239c20
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %r9
incq %r12
movl (%rsi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x13b27c
movq 0x10(%r8), %rax
movq (%rax), %rcx
movq (%rcx), %rdx
movq 0x28(%rsp), %r9
incq %r9
cmpq %rdx, %r9
jb 0x13b23f
cmpq $0x0, 0x10(%rsp)
je 0x13b5cb
movl 0x28(%r8), %edi
movq 0x58(%rax), %rcx
leaq 0x9b(%rip), %rsi        # 0x13b655
movq %r8, %rdx
callq 0x108f2c
movq 0x8(%rsp), %r8
movq 0x10(%r8), %rax
cmpq $0x0, 0x58(%rax)
je 0x13b646
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x18(%r8), %rax
cmpq $0x0, 0x8(%rax,%rbx)
je 0x13b606
movq 0x10(%rax,%rbx), %rdi
callq 0x2220
movq 0x8(%rsp), %r8
movq 0x18(%r8), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rbx)
movq $0x0, 0x10(%rax,%rbx)
movq 0x20(%r8), %rax
cmpq $0x0, 0x8(%rax,%rbx)
je 0x13b635
movq 0x10(%rax,%rbx), %rdi
callq 0x2220
movq 0x8(%rsp), %r8
movq 0x20(%r8), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rbx)
movq $0x0, 0x10(%rax,%rbx)
incq %r14
movq 0x10(%r8), %rax
addq $0x18, %rbx
cmpq 0x58(%rax), %r14
jb 0x13b5d7
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq