// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_alignment_debug(char*, unsigned long, unsigned long, kvec_vote*, ha_ug_index const*, unsigned long, unsigned long)
void get_alignment_debug(char *r, uint64_t len, uint64_t k_mer, kvec_vote* buf, const ha_ug_index* idx, uint64_t buf_iter, uint64_t rid)
{
    uint64_t i, j, l = 0,  m, skip, *pos_list = NULL, cnt, rev, self_p, ref_p, uID;
    uint64_t x[4], mask = (1ULL<<k_mer) - 1, shift = k_mer - 1, hash;
    /****************************may have bugs********************************/
    /**
    ///buf->a.n = 0;
    uint64_t k_len, c_sfx, k;
    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*)idx, hash, &pos_list);
                if(cnt > idx->hap_cnt || cnt <= 0) continue;

                if(cnt > 1)
                {
                    for (j = 0; j < cnt; j++)
                    {
                        uID = (pos_list[j] << 1) >> (64 - idx->uID_bits);
                        for (k = j + 1; k < cnt; k++)
                        {
                            if(uID == ((pos_list[k] << 1) >> (64 - idx->uID_bits))) break;
                        }
                        if(k < cnt) break;
                    }

                    if(j < cnt) continue;
                }
                // if(cnt > 0) fprintf(stderr, "+i: %lu, l: %lu, cnt: %lu\n", i, l, cnt);
                get_longest_hit(r, len, k_mer, i, skip, buf, idx, pos_list, cnt, &c_sfx);
                // if(cnt > 0) fprintf(stderr, "c_sfx: %lu\n", c_sfx);
                if(c_sfx != (uint64_t)-1)
                {
                    k_len = c_sfx;
                    if((k_len + 1) >= k_mer)
                    {
                        l = 0, x[0] = x[1] = x[2] = x[3] = 0;
                        i = i + k_len - (k_mer - 1);
                    }
                    else
                    {
                        ///l = i - (i + k_len - (k_mer - 1));
                        l = k_mer - k_len - 1;
                    }  
                }
                // if(cnt > 0) fprintf(stderr, "-i: %lu, l: %lu\n", i, l);
            }
            
        } else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
    }
    **/

    s_hit *p = NULL; uint64_t u_len;
    ///buf->a.n = 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;
                /*******************************for debug************************************/
                // if(debug_hash_value(r, i, k_mer) != hash)
                // {
                //     fprintf(stderr, "ERROR\n");
                // }
                /*******************************for debug************************************/
                cnt = get_hc_pt1_count((ha_ug_index*)idx, hash, &pos_list);
                if(cnt > idx->hap_cnt) continue;
                if(cnt != 1) continue; ///might be able to be disabled in future

                
                for (j = 0; j < cnt; j++)
                {
                    kv_pushp(s_hit, buf->a, &p);
                    rev = (pos_list[j]>>63) != skip;
                    self_p = i;
                    ref_p = pos_list[j] & idx->pos_mode;
                    uID = (pos_list[j] << 1) >> (64 - idx->uID_bits);
                    u_len = idx->ug->u.a[uID].len;
                    if(rev) ref_p = u_len - 1 - (ref_p + 1 - k_mer);
                    p->off_cnt = self_p | ((uint64_t)k_mer << 32); ///high bits should be the legnth

                    p->ref = ref_p >= self_p? (ref_p-self_p) 
                                    : (self_p-ref_p) + ((uint64_t)1 << (idx->pos_bits - 1));
                    p->ref = (rev << 63)|(pos_list[j] & idx->uID_mode)|(p->ref&idx->pos_mode);


                    /*******************************for debug************************************/
                    // if(check_exact_match(r, i + 1 - k_mer, len,
                    //                     idx->ug->u.a[uID].s, ref_p  + 1 - k_mer, u_len, k_mer, rev, 0) != k_mer
                    //     ||
                    //    check_exact_match(r, i, len,
                    //                     idx->ug->u.a[uID].s, ref_p, u_len, k_mer, rev, 1) != k_mer)
                    // {
                    //     fprintf(stderr, "ERROR\n");
                    // }
                    /*******************************for debug************************************/
                }
                
                if(cnt == 1)
                {
                    ///uint64_t debug_right = 0, debug_left = 0, debug_len;

                    j = check_exact_match(r, self_p + 1, len, idx->ug->u.a[uID].s, ref_p + 1, u_len, len, rev, 0);
                    
                    ///debug_right = j;
                    ///if(j == 0) continue;
                    if((j + 1) >= k_mer)
                    {
                        l = 0, x[0] = x[1] = x[2] = x[3] = 0;
                        i = i + j - (k_mer - 1);
                    }
                    else
                    {
                        ///l = i - (i + j - (k_mer - 1));
                        l = k_mer - j -1;
                    }
                    buf->a.a[buf->a.n-1].off_cnt += ((uint64_t)j << 32) + j;

                    if(self_p >= k_mer && ref_p >= k_mer)
                    {
                        j = check_exact_match(r, self_p - k_mer, len, idx->ug->u.a[uID].s, 
                                                                ref_p - k_mer, u_len, len, rev, 1);
                        buf->a.a[buf->a.n-1].off_cnt += ((uint64_t)j << 32);
                        ///debug_left = j;
                    }


                    // debug_len = check_exact_match(r, self_p + debug_right, len, idx->ug->u.a[uID].s, 
                    // ref_p + debug_right, u_len, len, rev, 1);
                    // if(debug_len!= (debug_left + debug_right + k_mer))
                    // {
                    //     fprintf(stderr, "debug_len: %lu, debug_left: %lu, debug_right: %lu\n",
                    //     debug_len, debug_left, debug_right);
                    // }
                }
                
            }
            
        } else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
    }
    /****************************may have bugs********************************/
    
    if(buf->a.n - buf_iter == 0) return;
    if(buf->a.n - buf_iter > 1) radix_sort_hc_s_hit_an1(buf->a.a + buf_iter, buf->a.a + buf->a.n);


    uint64_t cur_ref_p, thres = (len * HIC_R_E_RATE) + 1, index_beg, ovlp;
    /****************************may have bugs********************************/
    uint64_t maxLen = 0, max_i = (uint64_t)-1;
    /****************************may have bugs********************************/
    i = m = buf_iter;
    while (i < buf->a.n)
    {
        interpret_pos(idx, &buf->a.a[i], &rev, &uID, &ref_p, &self_p, &cnt, NULL);
        cur_ref_p = buf->a.a[i].ref;
        index_beg = i;
        while ((i < buf->a.n) && 
                ((buf->a.a[i].ref>>(idx->pos_bits-1)) == (cur_ref_p>>(idx->pos_bits-1))) && 
               (buf->a.a[i].ref - cur_ref_p <= thres))
        {
            i++;
        }
        if(i - index_beg > 1)
        {
            radix_sort_hc_s_hit_an2(buf->a.a + index_beg, buf->a.a + i);//sort by self_p
        }
        ovlp = collect_votes(buf->a.a + index_beg, i - index_beg);
        buf->a.a[m] = buf->a.a[i - 1];
        buf->a.a[m].off_cnt = (buf->a.a[m].off_cnt << 32)>>32;
        buf->a.a[m].off_cnt += ((uint64_t)ovlp<<32);
        m++;
        /****************************may have bugs********************************/
        if(maxLen < (ovlp&((uint64_t)65535))) maxLen = (ovlp&((uint64_t)65535)), max_i = m;
        /****************************may have bugs********************************/
    }
    buf->a.n = m; 
    /****************************may have bugs********************************/
    ///compress_mapped_pos_advance(idx, buf, buf_iter, (k_mer * 0.1) > 0? (k_mer * 0.1) : 1);
    compress_mapped_pos_debug(idx, buf, buf_iter, max_i, thres);
    /****************************may have bugs********************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, 0x78(%rsp)
movq %r8, (%rsp)
movq %rcx, %r14
movq %rdx, %r15
leaq 0x98(%rsp), %rax
andq $0x0, (%rax)
movq %rsi, %r13
movq %rdi, 0x40(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x38(%rsp)
movl %r15d, %ecx
shlq %cl, %rax
notq %rax
movq %rax, 0xb0(%rsp)
leaq -0x1(%rdx), %rax
movq %rax, 0x88(%rsp)
movq %rdx, %rax
shlq $0x20, %rax
movq %rax, 0xa8(%rsp)
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %rax
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
xorl %r10d, %r10d
leaq 0xf61d4(%rip), %r9      # 0x1ebb00
xorl %ebp, %ebp
movq %rdx, 0x30(%rsp)
movq %rsi, 0x8(%rsp)
cmpq %r13, %rbp
jae 0xf5cfe
movq 0x40(%rsp), %rax
movzbl (%rax,%rbp), %eax
movzbl (%r9,%rax), %eax
cmpq $0x3, %rax
ja 0xf5b98
movl %eax, %edx
andl $0x1, %edx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rsi
leaq (%rdx,%rcx,2), %rcx
movq 0xb0(%rsp), %rdi
andq %rdi, %rcx
movq %rcx, 0x50(%rsp)
addq %rsi, %rsi
shrl %eax
movq %rax, %rcx
orq %rsi, %rcx
andq %rdi, %rcx
movq %rcx, 0x58(%rsp)
xorl $0x1, %edx
movq 0x88(%rsp), %rcx
shlq %cl, %rdx
movq 0x60(%rsp), %rsi
shrq %rsi
orq %rsi, %rdx
movq %rdx, 0x60(%rsp)
movq 0x68(%rsp), %rdx
shrq %rdx
pushq $0x1
popq %rsi
subl %eax, %esi
shlq %cl, %rsi
orq %rdx, %rsi
movq %rsi, 0x68(%rsp)
incq %r10
cmpq %r15, %r10
jb 0xf5cf6
movq %r10, %r13
leaq 0x50(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %r15, %rdx
callq 0x120a71
cmpq $-0x1, 0xa0(%rsp)
je 0xf5c26
movq (%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
leaq 0x98(%rsp), %rdx
callq 0x120ad7
movq %rax, 0xd0(%rsp)
cmpq $0x1, %rax
jne 0xf5c21
cmpq $0x0, 0x48(%r15)
je 0xf5c10
movq %rbp, %rax
orq 0xa8(%rsp), %rax
movq %rax, 0x48(%rsp)
movb $0x1, %al
xorl %r15d, %r15d
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rsi
testb $0x1, %al
je 0xf5bb0
movq (%r14), %r9
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %r9
jne 0xf5a85
leaq (%r9,%r9), %rsi
testq %r9, %r9
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %r9
leaq 0x1(%r9), %rcx
movq %rcx, (%r14)
movq 0x98(%rsp), %rdx
movq (%rdx,%r15,8), %r12
movq %r12, %rdi
shrq $0x3f, %rdi
movq 0xa0(%rsp), %r8
xorl %ecx, %ecx
cmpq %r8, %rdi
setne %cl
movq %rcx, 0x10(%rsp)
movq %rcx, 0xc8(%rsp)
movq %rbp, 0xc0(%rsp)
movq (%rsp), %r11
movq 0x30(%r11), %rsi
movq %rsi, %rbx
andq %r12, %rbx
movq %rbx, 0x90(%rsp)
addq %r12, %r12
movb 0x18(%r11), %r10b
xorl %ecx, %ecx
subb %r10b, %cl
shrq %cl, %r12
movq %r12, 0xb8(%rsp)
movq (%r11), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %r12, %r10
movl (%rcx,%r10), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %r8, %rdi
je 0xf5b21
notq %rbx
addq 0x88(%rsp), %rbx
addq %rcx, %rbx
movq %rbx, 0x90(%rsp)
shlq $0x4, %r9
addq %r9, %rax
movq 0x48(%rsp), %r9
movq %r9, 0x8(%rax)
movq %rbx, %r9
subq %rbp, %r9
movq %rcx, 0x20(%rsp)
jae 0xf5b5d
movq %rbp, %r10
subq %rbx, %r10
movq (%rsp), %r11
movb 0x28(%r11), %cl
decb %cl
pushq $0x1
popq %r13
movq %r13, %r9
shlq %cl, %r9
addq %r10, %r9
jmp 0xf5b65
movq (%rsp), %r11
pushq $0x1
popq %r13
xorl %ecx, %ecx
cmpq %r8, %rdi
setne %cl
movq %r9, (%rax)
shlq $0x3f, %rcx
movq 0x20(%r11), %rdi
andq (%rdx,%r15,8), %rdi
andq %r9, %rsi
orq %rdi, %rsi
orq %rcx, %rsi
movq %rsi, (%rax)
xorl %eax, %eax
movq %rbp, %rcx
movq %rbp, %rsi
movq %r13, %r15
jmp 0xf5a4b
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r10d, %r10d
jmp 0xf5cf6
movq %rcx, 0x18(%rsp)
incq %rsi
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r12, %rcx
movq 0x20(%rax,%rcx), %rcx
leaq 0x1(%rbx), %r8
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %r13
movq %r13, %rdx
movq 0x28(%rsp), %r9
pushq $0x0
pushq 0x20(%rsp)
pushq %r13
callq 0x120bf6
addq $0x20, %rsp
leaq 0x1(%rax), %rcx
movq 0x30(%rsp), %r15
cmpq %r15, %rcx
jae 0xf5c3a
movq %rax, %r10
notq %r10
addq %r15, %r10
jmp 0xf5c56
movq %r13, %r10
movq 0x30(%rsp), %r15
leaq 0xf5ee1(%rip), %r9      # 0x1ebb00
jmp 0xf5c30
movq 0x30(%rsp), %r15
leaq 0xf5ed3(%rip), %r9      # 0x1ebb00
movq %r13, %r10
movq 0x8(%rsp), %r13
jmp 0xf5cf6
subq %r15, %rbp
addq %rax, %rbp
incq %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r10d, %r10d
leaq 0xf5ea3(%rip), %r9      # 0x1ebb00
movabsq $0x100000001, %rcx      # imm = 0x100000001
imulq %rcx, %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
shlq $0x4, %rcx
addq %rax, -0x8(%rdx,%rcx)
movq 0x18(%rsp), %rax
movq %rax, %rsi
subq %r15, %rsi
jb 0xf5cf1
movq %rbx, %r8
subq %r15, %r8
jb 0xf5cec
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r12, %rcx
movq 0x20(%rax,%rcx), %rcx
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq %r13, %rdx
movq 0x28(%rsp), %r9
pushq $0x1
pushq 0x20(%rsp)
pushq %r13
movq %r10, %r13
callq 0x120bf6
movq %r13, %r10
movq 0x28(%rsp), %r13
leaq 0xf5e2c(%rip), %r9      # 0x1ebb00
addq $0x20, %rsp
shlq $0x20, %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
shlq $0x4, %rcx
addq %rax, -0x8(%rdx,%rcx)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
incq %rbp
jmp 0xf5938
movq (%r14), %rsi
movq %rsi, %rax
movq 0x78(%rsp), %r12
subq %r12, %rax
je 0xf5ef1
cmpq $0x2, %rax
jb 0xf5d32
movq 0x10(%r14), %rax
movq %r12, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %rsi
addq %rax, %rsi
callq 0xf2d57
movq %r13, %xmm0
punpckldq 0xed531(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xed539(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xf73c5(%rip), %xmm1    # 0x1ed120
addsd 0xed53d(%rip), %xmm1    # 0x1e32a0
cvttsd2si %xmm1, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0xed521(%rip), %xmm1    # 0x1e3298
cvttsd2si %xmm1, %rbx
andq %rcx, %rbx
orq %rax, %rbx
xorl %r15d, %r15d
movq %r12, %rdi
movq (%rsp), %rbp
movq %r14, 0x80(%rsp)
cmpq (%r14), %r12
jae 0xf5ed6
movq %rdi, 0x20(%rsp)
movq %r15, 0x10(%rsp)
movq %r12, %r15
shlq $0x4, %r15
movq 0x10(%r14), %rsi
addq %r15, %rsi
movq %rbp, %rdi
leaq 0xc8(%rsp), %rdx
leaq 0xb8(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0xc0(%rsp), %r9
pushq $0x0
leaq 0xd8(%rsp), %rax
pushq %rax
callq 0x120d2b
addq $0x10, %rsp
movq (%r14), %r13
movq 0x10(%r14), %rsi
cmpq %r13, %r12
cmovaq %r12, %r13
leaq (%rsi,%r15), %rdi
movq %r15, 0x48(%rsp)
movq (%rsi,%r15), %rdx
movq %rdi, %r8
movq %r12, %rax
cmpq %rax, %r13
je 0xf5e3f
movq (%r8), %r9
movb 0x28(%rbp), %cl
decb %cl
movq %r9, %r10
shrq %cl, %r10
movq %rdx, %r11
shrq %cl, %r11
cmpq %r11, %r10
jne 0xf5e3c
subq %rdx, %r9
cmpq %rbx, %r9
ja 0xf5e3c
incq %rax
addq $0x10, %r8
jmp 0xf5e0d
movq %rax, %r13
movq %r13, %rbp
subq %r12, %rbp
cmpq $0x2, %rbp
jb 0xf5e68
movq %r13, %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xf2f6f
movq 0x80(%rsp), %r14
movq 0x10(%r14), %rsi
jmp 0xf5e70
movq 0x80(%rsp), %r14
movq 0x10(%rsp), %r15
addq 0x48(%rsp), %rsi
movq %rsi, %rdi
movq %rbp, %rsi
callq 0x120d9e
movq 0x10(%r14), %rcx
movq %r13, %rdx
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rdi, %rsi
shlq $0x4, %rsi
movupd -0x10(%rcx,%rdx), %xmm0
movupd %xmm0, (%rcx,%rsi)
movq 0x10(%r14), %rcx
incq %rdi
movzwl %ax, %edx
cmpq %rdx, %r15
cmovbeq %rdx, %r15
movl %eax, 0xc(%rcx,%rsi)
movq 0x38(%rsp), %rax
cmovbq %rdi, %rax
movq %rax, 0x38(%rsp)
movq %r13, %r12
movq (%rsp), %rbp
jmp 0xf5d94
movq %rdi, (%r14)
movq %rbp, %rdi
movq %r14, %rsi
movq 0x78(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %rbx, %r8
callq 0x121332
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq