// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %rbx
movq %rsi, %r8
movq $-0x1, %rsi
movl %ebx, %ecx
shlq %cl, %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
testq %r8, %r8
movq %rdx, 0x40(%rsp)
movq %r8, 0x8(%rsp)
je 0x1367b0
notq %rsi
leaq -0x1(%rbx), %rcx
movq 0x50(%rsp), %r10
movq 0x58(%rsp), %rbp
movq 0x60(%rsp), %r14
movq 0x68(%rsp), %r15
xorl %r12d, %r12d
leaq 0xf76d0(%rip), %r9      # 0x22da10
xorl %edx, %edx
xorl %r13d, %r13d
movq %rcx, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
movq %rsi, 0x48(%rsp)
movzbl (%rdi,%r12), %eax
movzbl (%r9,%rax), %eax
cmpq $0x3, %rax
ja 0x13640b
movq %rdx, %r11
movq %r14, %rdx
movl %eax, %r14d
andl $0x1, %r14d
leaq (%r14,%r10,2), %r10
andq %rsi, %r10
movq %r10, 0x50(%rsp)
addq %rbp, %rbp
shrl %eax
orq %rax, %rbp
andq %rsi, %rbp
movq %rbp, 0x58(%rsp)
xorl $0x1, %r14d
shlq %cl, %r14
shrq %rdx
orq %rdx, %r14
movq %r14, 0x60(%rsp)
movq %r15, %rdx
shrq %rdx
movl $0x1, %r15d
subl %eax, %r15d
shlq %cl, %r15
orq %rdx, %r15
movq %r11, %rdx
movq %r15, 0x68(%rsp)
incq %rdx
cmpq %rbx, %rdx
jb 0x136424
cmpq %r15, %rbp
jne 0x1363d9
movq $-0x1, %r11
cmpq %r14, %r10
je 0x1363e0
setae %al
movzbl %al, %r11d
cmpq $-0x1, %r11
movq %r10, 0x30(%rsp)
je 0x136435
movq %r11, %rax
shlq $0x4, %rax
cmpq $0x20, %rbx
ja 0x136441
movq 0x50(%rsp,%rax), %r10
shlq $0x20, %r10
orq 0x58(%rsp,%rax), %r10
jmp 0x1364d2
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r10d, %r10d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
incq %r12
cmpq %r8, %r12
jb 0x136354
jmp 0x1367b0
movq $-0x1, %r10
jmp 0x1364d2
movq 0x50(%rsp,%rax), %rcx
movq 0x58(%rsp,%rax), %rax
movq %rdx, %r9
movq %rcx, %rdx
notq %rdx
shlq $0x15, %rcx
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x18, %rdx
xorq %rcx, %rdx
imulq $0x109, %rdx, %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
movq %rax, %rcx
notq %rcx
shlq $0x15, %rax
addq %rcx, %rax
movq %rax, %rcx
shrq $0x18, %rcx
xorq %rax, %rcx
imulq $0x109, %rcx, %rax      # imm = 0x109
movq %rax, %rcx
shrq $0xe, %rcx
xorq %rax, %rcx
leaq (%rcx,%rcx,4), %rax
leaq (%rcx,%rax,4), %rax
movq %rax, %rcx
shrq $0x1c, %rcx
xorq %rax, %rcx
addq %rdx, %rcx
movq %r9, %rdx
movq %rcx, %r10
shlq $0x1f, %r10
addq %rcx, %r10
cmpq $-0x1, %r11
je 0x136588
movq %r11, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq 0x18(%rsp), %rax
movb 0x50(%rax), %cl
movq $-0x1, %r9
shlq %cl, %r9
notq %r9
movq 0x78(%rax), %r8
andq %r10, %r9
shlq $0x5, %r9
movq (%r8,%r9), %rsi
movq 0x10(%rsi), %rdx
xorl %r11d, %r11d
movl $0x0, %edi
testq %rdx, %rdx
je 0x1365af
movq %r8, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movb (%rsi), %cl
movl $0x1, %edi
shll %cl, %edi
movq %r10, %rax
shrq $0xc, %rax
imull $0x9e3779b9, %eax, %r8d # imm = 0x9E3779B9
negb %cl
shrl %cl, %r8d
leal -0x1(%rdi), %ecx
movq 0x8(%rsi), %rbx
movl %r8d, %eax
movl %eax, %r14d
shrl $0x5, %r14d
movl (%rbx,%r14,4), %r14d
btl %eax, %r14d
jae 0x1365a0
movl %eax, %r14d
shlq $0x4, %r14
movq (%rdx,%r14), %r14
xorq %r10, %r14
cmpq $0x1000, %r14           # imm = 0x1000
jb 0x13659e
incl %eax
andl %ecx, %eax
cmpl %r8d, %eax
jne 0x136555
jmp 0x1365a0
movq 0x28(%rsp), %rcx
leaq 0xf747c(%rip), %r9      # 0x22da10
movq 0x30(%rsp), %r10
jmp 0x136424
movl %eax, %edi
movq 0x40(%rsp), %rbx
movq 0x78(%rsp), %r14
movq 0x70(%rsp), %r8
testq %rdx, %rdx
je 0x1365bf
movb (%rsi), %cl
movl $0x1, %r11d
shll %cl, %r11d
cmpl %r11d, %edi
jne 0x1365c9
xorl %r10d, %r10d
jmp 0x136637
addq %r9, %r8
movl %edi, %eax
shlq $0x4, %rax
movq 0x8(%rdx,%rax), %r9
leaq (,%r9,8), %r13
addq 0x10(%r8), %r13
movq (%rdx,%rax), %r10
movl $0xfff, %eax            # imm = 0xFFF
andq %rax, %r10
cmpq %rax, %r10
jne 0x136637
cmpl 0x18(%r8), %edi
jne 0x136600
movq 0x8(%r8), %r10
jmp 0x136634
testq %rdx, %rdx
je 0x136610
movb (%rsi), %cl
movl $0x1, %eax
shll %cl, %eax
jmp 0x136612
xorl %eax, %eax
incl %edi
cmpl %eax, %edi
je 0x1365fa
movq 0x8(%rsi), %rax
movl %edi, %ecx
shrl $0x5, %ecx
movl (%rax,%rcx,4), %eax
btl %edi, %eax
jae 0x136600
movl %edi, %eax
shlq $0x4, %rax
movq 0x8(%rdx,%rax), %r10
subq %r9, %r10
leaq -0x1(%r10), %rax
movq 0x18(%rsp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x136783
cmpq $0x2, %r10
jb 0x1366c1
movl $0x40, %ecx
movq 0x18(%rsp), %rax
subq 0x18(%rax), %rcx
movb $0x1, %dl
movl $0x2, %eax
xorl %esi, %esi
movq (%r13,%rsi,8), %rdi
addq %rdi, %rdi
shrq %cl, %rdi
incq %rsi
cmpq %r10, %rsi
setb %r8b
jae 0x1366b6
movq (%r13,%rsi,8), %r9
addq %r9, %r9
shrq %cl, %r9
cmpq %r9, %rdi
je 0x1366ad
movq %rax, %r9
cmpq %r10, %r9
setb %r8b
jae 0x1366b6
movq (%r13,%r9,8), %r11
addq %r11, %r11
shrq %cl, %r11
incq %r9
cmpq %r11, %rdi
jne 0x136691
testb %r8b, %r8b
jne 0x13677a
incq %rax
cmpq %r10, %rsi
setb %dl
jne 0x136667
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq 0x88(%rsp), %r8
movq 0x38(%rsp), %r9
leaq 0x90(%rsp), %rax
pushq %rax
pushq %r10
pushq %r13
pushq 0x30(%rsp)
callq 0x135d74
addq $0x20, %rsp
movq 0x90(%rsp), %rax
cmpq $-0x1, %rax
je 0x136734
leaq 0x1(%rax), %rcx
cmpq %rbx, %rcx
movq 0x48(%rsp), %rsi
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %rcx
leaq 0xf72ee(%rip), %r9      # 0x22da10
movq 0x30(%rsp), %r10
jae 0x136749
notq %rax
addq %rbx, %rax
movq %rax, %rdx
jmp 0x13676b
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rsi
xorpd %xmm0, %xmm0
jmp 0x136792
subq %rbx, %r12
addq %rax, %r12
incq %r12
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r10d, %r10d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rdi
jmp 0x136424
testb $0x1, %dl
je 0x1366c1
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x28(%rsp), %rcx
leaq 0xf7272(%rip), %r9      # 0x22da10
movq 0x30(%rsp), %r10
movq 0x80(%rsp), %rdx
jmp 0x136424
movq 0x38(%rsp), %rdi
movq (%rdi), %rsi
movq %rsi, %rax
subq 0x20(%rsp), %rax
je 0x136a56
cmpq $0x2, %rax
jb 0x1367f2
movq 0x10(%rdi), %rax
movq 0x20(%rsp), %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %rsi
addq %rax, %rsi
callq 0x13356d
movq 0x8(%rsp), %r8
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rsp), %rcx
movq %rcx, %rbx
cmpq %rcx, %rax
jbe 0x1369dc
movq %r8, %xmm0
punpckldq 0xeea2d(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xeea35(%rip), %xmm0    # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xf87b1(%rip), %xmm1    # 0x22efe0
addsd 0xeea39(%rip), %xmm1    # 0x225270
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0xeea1d(%rip), %xmm1    # 0x225268
cvttsd2si %xmm1, %r14
andq %rdx, %r14
orq %rcx, %r14
movq 0x20(%rsp), %rbx
movq %rbx, %r15
movq %r15, %r12
movq 0x10(%rdi), %rsi
movq %r15, %r13
shlq $0x4, %r13
movq (%rsi,%r13), %rdx
movq 0x18(%rsp), %rcx
movq 0x28(%rcx), %rcx
decq %rcx
movq %rdx, %r8
shrq %cl, %r8
leaq (%rsi,%r13), %rdi
leaq 0x1(%r15), %r9
cmpq %r9, %rax
cmovaq %rax, %r9
movq %rdi, %rax
movq (%rax), %r10
movq %r10, %r11
shrq %cl, %r11
cmpq %r8, %r11
jne 0x1368b9
subq %rdx, %r10
cmpq %r14, %r10
ja 0x1368b9
incq %r15
addq $0x10, %rax
cmpq %r15, %r9
jne 0x136894
movq %r9, %r15
movq %r15, %rbp
subq %r12, %rbp
cmpq $0x2, %rbp
jb 0x1368d4
movq %r15, %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x133787
movq 0x38(%rsp), %rdi
movq 0x10(%rdi), %rax
testq %rbp, %rbp
je 0x1369ac
cmpq $0x1, %rbp
jne 0x1368f8
addq %rax, %r13
movl 0xc(%r13), %ebp
jmp 0x1369ac
movq %r15, %rdi
shlq $0x4, %rdi
movq -0x8(%rax,%rdi), %rcx
movl %ecx, %edx
movq %rcx, %r8
shrq $0x20, %r8
movq %rdx, %rsi
subq %r8, %rsi
notq %rsi
addq $-0x2, %rbp
js 0x13698f
negq %r8
leaq (%rdx,%r8), %r9
incq %r9
notq %r12
addq %r15, %r12
leaq (%rax,%rdi), %r8
addq $-0x18, %r8
xorl %edi, %edi
movq (%r8), %r10
movl %r10d, %r11d
shrq $0x20, %r10
negq %r10
addq %r11, %r10
incq %r10
cmpq %r10, %r9
movq %r10, %r13
cmovaq %r9, %r13
cmpq %r11, %rdx
movq %r11, %rbp
cmovbq %rdx, %rbp
cmpq %rbp, %r13
jbe 0x136971
addq %rdi, %rsi
leaq (%rdx,%rsi), %rdi
incq %rdi
movq %r11, %rdx
jmp 0x136978
cmpq %r10, %r9
cmovbq %r9, %r10
movq %r10, %rsi
negq %rsi
decq %r12
addq $-0x10, %r8
movq %r10, %r9
testq %r12, %r12
jg 0x136936
jmp 0x136991
xorl %edi, %edi
addq %rdi, %rdx
addq %rdx, %rsi
incq %rsi
subq %rdx, %rcx
shlq $0x10, %rcx
orq %rsi, %rcx
movq %rcx, %rbp
movq 0x38(%rsp), %rdi
movq %r15, %rcx
shlq $0x4, %rcx
movq %rbx, %rdx
shlq $0x4, %rdx
movupd -0x10(%rax,%rcx), %xmm0
movupd %xmm0, (%rax,%rdx)
movq 0x10(%rdi), %rax
movl %ebp, 0xc(%rax,%rdx)
incq %rbx
movq (%rdi), %rax
cmpq %rax, %r15
jb 0x13685e
movsd 0x40(%rsp), %xmm0
unpcklps 0xee857(%rip), %xmm0    # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq %rbx, (%rdi)
subpd 0xee85c(%rip), %xmm0    # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xee1c8(%rip), %xmm1    # 0x224bd0
xorpd %xmm0, %xmm0
cmpltsd %xmm1, %xmm0
andpd %xmm0, %xmm1
movsd 0xee853(%rip), %xmm2    # 0x225270
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd 0xee82f(%rip), %xmm0    # 0x225268
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rax, %rcx
movq %rdi, %rsi
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
callq 0x15f22c
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq