// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq 0x10(%rdi), %rsi
movq 0x40(%rsi), %r15
pushq $-0x1
popq %rax
movq %rax, %rdx
movl %r15d, %ecx
shlq %cl, %rdx
notq %rdx
movq %rdx, 0x68(%rsp)
decq %r15
movb 0x50(%rsi), %cl
shlq %cl, %rax
notq %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rdi), %rax
xorl %r14d, %r14d
cmpl $0x1, (%rdi)
movl $0x0, %ecx
adcb $0x3, %cl
shrq %cl, %rax
movq %rax, 0x60(%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, %rbp
shrq $0x10, %rbp
orq %rcx, %rbp
leaq 0x8(,%rbp,8), %rax
movq %rax, 0x8(%rsp)
incq %rbp
movq %rbp, %rax
shlq $0x4, %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
cmpq 0x58(%rsi), %r13
jae 0xf47fb
cmpl $0x0, (%rbx)
je 0xf47b8
movq 0x18(%rbx), %rax
cmpq %r12, 0x8(%rax,%r14)
jbe 0xf4790
addq %r14, %rax
jmp 0xf47e7
movq %rbp, 0x8(%rax,%r14)
movq 0x10(%rax,%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0x2370
movq 0x18(%rbx), %rcx
movq %rax, 0x10(%rcx,%r14)
movl (%rbx), %eax
andq $0x0, (%rcx,%r14)
testl %eax, %eax
jne 0xf47eb
movq 0x20(%rbx), %rcx
cmpq %r12, 0x8(%rcx,%r14)
ja 0xf47e0
movq %rbp, 0x8(%rcx,%r14)
movq 0x10(%rcx,%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0x2370
movq 0x20(%rbx), %rcx
movq %rax, 0x10(%rcx,%r14)
imulq $0x18, %r13, %rax
addq %rcx, %rax
andq $0x0, (%rax)
incq %r13
movq 0x10(%rbx), %rsi
addq $0x18, %r14
jmp 0xf4771
movq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0xf72f2(%rip), %r8      # 0x1ebb00
pushq $0x1
popq %r9
xorl %ecx, %ecx
movq %rbx, 0x48(%rsp)
movq %r15, 0x18(%rsp)
movq (%rsi), %rax
cmpq (%rax), %rcx
jae 0xf4ab6
movq 0x10(%rax), %r12
imulq $0x28, %rcx, %rax
cmpl $0x0, 0xc(%r12,%rax)
je 0xf4aae
movq %rcx, 0x10(%rsp)
addq %rax, %r12
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movl (%r12), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jae 0xf4aa5
movq 0x20(%r12), %rax
movzbl (%rax,%r13), %eax
movzbl (%r8,%rax), %eax
cmpq $0x3, %rax
ja 0xf498d
movl %eax, %edx
andl $0x1, %edx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
leaq (%rdx,%rcx,2), %rcx
movq 0x68(%rsp), %rdi
andq %rdi, %rcx
movq %rcx, 0x20(%rsp)
addq %rsi, %rsi
shrl %eax
movq %rax, %rcx
orq %rsi, %rcx
andq %rdi, %rcx
movq %rcx, 0x28(%rsp)
movq 0x30(%rsp), %rsi
shrq %rsi
xorl $0x1, %edx
movl %r15d, %ecx
shlq %cl, %rdx
orq %rsi, %rdx
movq %rdx, 0x30(%rsp)
movl %r9d, %edx
subl %eax, %edx
shlq %cl, %rdx
movq 0x38(%rsp), %rax
shrq %rax
orq %rax, %rdx
movq %rdx, 0x38(%rsp)
incq %r14
movq 0x10(%rbx), %rax
movq 0x40(%rax), %rdx
cmpq %rdx, %r14
jb 0xf4a9d
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x120a71
movq 0x70(%rsp), %r15
cmpq $-0x1, %r15
je 0xf499f
movq %rax, %rbp
cmpl $0x0, (%rbx)
je 0xf49a9
movq 0x18(%rbx), %rcx
movq %rbp, %rax
andq 0x40(%rsp), %rax
movq %rbx, %r15
imulq $0x18, %rax, %rbx
movq (%rcx,%rbx), %rax
cmpq 0x8(%rcx,%rbx), %rax
jne 0xf4972
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rcx,%rbx), %rax
movq %rsi, 0x8(%rcx,%rbx)
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x18(%r15), %rcx
movq %rax, 0x10(%rcx,%rbx)
movq (%rcx,%rbx), %rax
movq 0x18(%rsp), %r15
movq 0x10(%rcx,%rbx), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%rbx)
movq %rbp, (%rdx,%rax,8)
jmp 0xf4a51
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
jmp 0xf4a9d
movq 0x18(%rsp), %r15
jmp 0xf4a8f
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rdx
movb 0x18(%rax), %sil
xorl %ecx, %ecx
subb %sil, %cl
movq 0x10(%rsp), %rsi
shlq %cl, %rsi
movq 0x30(%rax), %rcx
andq %r13, %rcx
movq %rbp, %rax
andq 0x40(%rsp), %rax
imulq $0x18, %rax, %rbx
movq (%rdx,%rbx), %rax
cmpq 0x8(%rdx,%rbx), %rax
jne 0xf4a2a
movq %rsi, 0x58(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rdx,%rbx), %rax
movq %rsi, 0x8(%rdx,%rbx)
movq 0x10(%rax), %rdi
shlq $0x4, %rsi
movq %rcx, 0x50(%rsp)
callq 0x2370
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x20(%rdx), %rdx
movq %rax, 0x10(%rdx,%rbx)
movq (%rdx,%rbx), %rax
shldq $0x3f, %rsi, %r15
orq %rcx, %r15
movq 0x10(%rdx,%rbx), %rcx
leaq 0x1(%rax), %rsi
movq %rsi, (%rdx,%rbx)
shlq $0x4, %rax
movq %rbp, (%rcx,%rax)
movq %r15, 0x8(%rcx,%rax)
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rax
incq %rax
cmpq 0x60(%rsp), %rax
movq 0x48(%rsp), %rbx
jae 0xf4a6c
movq %rax, 0x8(%rsp)
jmp 0xf4a8f
movl 0x28(%rbx), %edi
movq 0x10(%rbx), %rax
movq 0x58(%rax), %rcx
leaq 0xce(%rip), %rsi        # 0xf4b4c
movq %rbx, %rdx
callq 0xc67c0
movq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0xf7067(%rip), %r8      # 0x1ebb00
pushq $0x1
popq %r9
incq %r13
jmp 0xf4856
movq 0x10(%rbx), %rsi
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0xf481e
cmpq $0x0, 0x8(%rsp)
je 0xf4ad4
movl 0x28(%rbx), %edi
movq 0x58(%rsi), %rcx
leaq 0x80(%rip), %rsi        # 0xf4b4c
movq %rbx, %rdx
callq 0xc67c0
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
cmpq 0x58(%rax), %r15
jae 0xf4b3d
movq 0x18(%rbx), %rax
cmpq $0x0, 0x8(%rax,%r14)
je 0xf4b0c
movq 0x10(%rax,%r14), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r14)
andq $0x0, 0x10(%rax,%r14)
movq 0x20(%rbx), %rax
cmpq $0x0, 0x8(%rax,%r14)
je 0xf4b34
movq 0x10(%rax,%r14), %rdi
callq 0x2220
movq 0x20(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r14)
andq $0x0, 0x10(%rax,%r14)
incq %r15
addq $0x18, %r14
jmp 0xf4ada
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq