// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_alignment(char*, unsigned long, unsigned long, kvec_vote*, ha_ug_index const*, unsigned long, unsigned long)
void get_alignment(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, k, l = 0, k_len, c_sfx, 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;
    ///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;
                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
    }
    
    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;
    i = m = buf_iter;
    while (i < buf->a.n)
    {
        interpret_pos(idx, &buf->a.a[i], &rev, &uID, &ref_p, &self_p, &cnt, NULL);
        ///fprintf(stderr, "after-i: %lu, uID: %lu, ref_p: %lu, self_p: %lu\n", i, uID, ref_p, self_p);
        cur_ref_p = buf->a.a[i].ref;
        index_beg = i;
        ///ref>>(idx->pos_bits-1) = (rev:1):(uID:uID-bits):(ref_pos>=self_pos:1)
        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);
        ///fprintf(stderr, "i-1: %lu, self_p: %u\n", i-1, (uint32_t)buf->a.a[i - 1].off_cnt);
        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);
        ///fprintf(stderr, "m: %lu, self_p: %u\n", m, (uint32_t)buf->a.a[m].off_cnt);
        m++;
    }
    buf->a.n = m; 

    /*******************************for debug************************************/
    // for (i = buf_iter; i < buf->a.n; i++)
    // {
    //     uint64_t eLen, tLen;
    //     interpret_pos(idx, &buf->a.a[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     if(maxLen < eLen) fprintf(stderr, "ERROR1\n");
    //     if(i == max_i && maxLen != eLen) fprintf(stderr, "ERROR2\n");
    // }
    /*******************************for debug************************************/
    ///select the best alignment at [buf_iter, m)
    
    /*******************************for debug************************************/
    // fprintf(stderr, "len1:%lu, max_i: %lu\n", buf->a.n - buf_iter, max_i);
    // for (i = buf_iter; i < buf->a.n; i++)
    // {
    //     uint64_t eLen, tLen;
    //     interpret_pos(idx, &buf->a.a[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     fprintf(stderr, "(%lu) rev: %lu, uID: %lu, ref_p: %lu, self_p: %lu, eLen: %lu, tLen: %lu\n", 
    //                                                         i, rev, uID, ref_p, self_p, eLen, tLen);
    // }
    /*******************************for debug************************************/
    compress_mapped_pos_advance(idx, buf, buf_iter, (k_mer * 0.1) > 0? (k_mer * 0.1) : 1);
    /*******************************for debug************************************/
    // fprintf(stderr, "len2:%lu, max_i: %lu\n", buf->a.n - buf_iter, max_i);
    // for (i = buf_iter; i < buf->a.n; i++)
    // {
    //     uint64_t eLen, tLen;
    //     interpret_pos(idx, &buf->a.a[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
    //     fprintf(stderr, "(%lu) rev: %lu, uID: %lu, ref_p: %lu, self_p: %lu, eLen: %lu, tLen: %lu\n", 
    //                                                         i, rev, uID, ref_p, self_p, eLen, tLen);
    // }
    // if(buf->a.n != m) fprintf(stderr, "Changed\n");
    // fprintf(stderr, "\n");
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x48(%rsp)
movq %r8, %r15
movq %rcx, %r14
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
pushq $-0x1
popq %r13
movl %edx, %ecx
shlq %cl, %r13
movq %rsi, %r8
movq %rdi, %rbx
notq %r13
leaq -0x1(%rdx), %rcx
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
xorl %ebp, %ebp
leaq 0xf667d(%rip), %r10     # 0x1ebb00
pushq $0x1
popq %r11
xorl %r12d, %r12d
movq %rdx, 0x18(%rsp)
movq %rsi, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
cmpq %r8, %r12
jae 0xf566c
movzbl (%rbx,%r12), %eax
movzbl (%r10,%rax), %eax
cmpq $0x3, %rax
ja 0xf55b3
movl %eax, %edi
andl $0x1, %edi
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %rsi
leaq (%rdi,%r9,2), %r9
andq %r13, %r9
movq %r9, 0x20(%rsp)
addq %rsi, %rsi
shrl %eax
movq %rax, %r9
orq %rsi, %r9
andq %r13, %r9
movq %r9, 0x28(%rsp)
xorl $0x1, %edi
shlq %cl, %rdi
movq 0x30(%rsp), %rsi
shrq %rsi
orq %rsi, %rdi
movq %rdi, 0x30(%rsp)
movq 0x38(%rsp), %rdi
shrq %rdi
movl %r11d, %esi
subl %eax, %esi
shlq %cl, %rsi
orq %rdi, %rsi
movq %rsi, 0x38(%rsp)
incq %rbp
cmpq %rdx, %rbp
jb 0xf5642
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x120a71
cmpq $-0x1, 0x58(%rsp)
je 0xf5628
movq %r15, %rdi
movq %rax, %rsi
leaq 0x50(%rsp), %rdx
callq 0x120ad7
movq %rax, 0x70(%rsp)
leaq -0x1(%rax), %rcx
cmpq 0x48(%r15), %rcx
jae 0xf5628
movq 0x50(%rsp), %r10
cmpq $0x2, %rax
jb 0xf55c7
pushq $0x40
popq %rcx
subq 0x18(%r15), %rcx
movq %r10, %rdx
movq %rax, %rsi
xorl %edi, %edi
cmpq %rax, %rdi
je 0xf55c7
movq (%r10,%rdi,8), %r8
addq %r8, %r8
shrq %cl, %r8
movq %r8, 0x68(%rsp)
incq %rdi
pushq $0x1
popq %r9
cmpq %r9, %rsi
je 0xf55aa
movq (%rdx,%r9,8), %r11
addq %r11, %r11
shrq %cl, %r11
incq %r9
cmpq %r11, %r8
jne 0xf5591
jmp 0xf5628
decq %rsi
addq $0x8, %rdx
jmp 0xf5576
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
xorl %ebp, %ebp
jmp 0xf5642
movq 0x58(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq %r14, %r9
leaq 0x78(%rsp), %r11
pushq %r11
pushq %rax
pushq %r10
pushq %r15
callq 0xf50ce
addq $0x20, %rsp
movq 0x78(%rsp), %rax
cmpq $-0x1, %rax
je 0xf5628
leaq 0x1(%rax), %rcx
movq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
movq 0x10(%rsp), %rcx
leaq 0xf64e9(%rip), %r10     # 0x1ebb00
pushq $0x1
popq %r11
jae 0xf564a
notq %rax
addq %rdx, %rax
movq %rax, %rbp
jmp 0xf5665
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rcx
leaq 0xf64c2(%rip), %r10     # 0x1ebb00
pushq $0x1
popq %r11
incq %r12
jmp 0xf5499
subq %rdx, %r12
addq %rax, %r12
incq %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %r8
jmp 0xf5642
movq (%r14), %rsi
movq %rsi, %rax
movq 0x48(%rsp), %rbp
subq %rbp, %rax
je 0xf5896
cmpq $0x2, %rax
jb 0xf56aa
movq 0x10(%r14), %rax
movq %rbp, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %rsi
addq %rax, %rsi
callq 0xf2d57
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rdx
movq %r8, %xmm0
punpckldq 0xedbb9(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xedbc1(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xf7a4d(%rip), %xmm1    # 0x1ed120
addsd 0xedbc5(%rip), %xmm1    # 0x1e32a0
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0xedbad(%rip), %xmm1    # 0x1e3298
sarq $0x3f, %rcx
cvttsd2si %xmm1, %r12
andq %rcx, %r12
orq %rax, %r12
movq %rbp, %rdi
movq %r14, 0x10(%rsp)
movq %r15, 0x60(%rsp)
cmpq (%r14), %rbp
jae 0xf581e
movq %rdi, 0x8(%rsp)
movq %rbp, %r13
shlq $0x4, %r13
movq 0x10(%r14), %rsi
addq %r13, %rsi
movq %r15, %rdi
leaq 0x90(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x88(%rsp), %r9
pushq $0x0
leaq 0x78(%rsp), %rax
pushq %rax
callq 0x120d2b
addq $0x10, %rsp
movq (%r14), %rbx
movq 0x10(%r14), %rsi
cmpq %rbx, %rbp
cmovaq %rbp, %rbx
leaq (%rsi,%r13), %rdi
movq (%rsi,%r13), %rdx
movq %rdi, %r8
movq %rbp, %rax
cmpq %rax, %rbx
je 0xf57a3
movq (%r8), %r9
movb 0x28(%r15), %cl
decb %cl
movq %r9, %r10
shrq %cl, %r10
movq %rdx, %r11
shrq %cl, %r11
cmpq %r11, %r10
jne 0xf57a0
subq %rdx, %r9
cmpq %r12, %r9
ja 0xf57a0
incq %rax
addq $0x10, %r8
jmp 0xf5770
movq %rax, %rbx
movq %rbx, %r14
subq %rbp, %r14
cmpq $0x2, %r14
jb 0xf57c9
movq %rbx, %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xf2f6f
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rsi
jmp 0xf57ce
movq 0x10(%rsp), %r15
addq %r13, %rsi
movq %rsi, %rdi
movq %r14, %rsi
callq 0x120d9e
movq 0x10(%r15), %rcx
movq %rbx, %rdx
shlq $0x4, %rdx
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
shlq $0x4, %rsi
movupd -0x10(%rcx,%rdx), %xmm0
movupd %xmm0, (%rcx,%rsi)
movq 0x10(%r15), %rcx
movl %eax, 0xc(%rcx,%rsi)
incq %rdi
movq %rbx, %rbp
movq %r15, %r14
movq 0x60(%rsp), %r15
movq 0x18(%rsp), %rdx
jmp 0xf5707
movq %rdx, %xmm0
punpckldq 0xeda45(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq %rdi, (%r14)
subpd 0xeda4a(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xed3a6(%rip), %xmm1    # 0x1e2bf0
xorpd %xmm0, %xmm0
cmpltsd %xmm1, %xmm0
andpd %xmm0, %xmm1
movsd 0xeda41(%rip), %xmm2    # 0x1e32a0
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd 0xeda1d(%rip), %xmm0    # 0x1e3298
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rax, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq 0x48(%rsp), %rdx
callq 0x120e55
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq