// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq $-0x1, %r10
movl %r15d, %ecx
shlq %cl, %r10
testq %rsi, %rsi
movq %rdx, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
je 0x13c647
notq %r10
leaq -0x1(%r15), %r11
xorl %r12d, %r12d
leaq 0xfd9cb(%rip), %rbp     # 0x239c20
xorl %r14d, %r14d
movq $0x0, 0x50(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r9d, %r9d
xorl %r8d, %r8d
movq %rdi, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %r11, 0x28(%rsp)
movzbl (%rdi,%r12), %eax
movzbl (%rbp,%rax), %eax
cmpq $0x3, %rax
ja 0x13c2ea
movq %rbx, %rdx
movl %eax, %ebx
andl $0x1, %ebx
leaq (%rbx,%r8,2), %r8
andq %r10, %r8
addq %r9, %r9
shrl %eax
orq %rax, %r9
andq %r10, %r9
shrq %rdx
xorl $0x1, %ebx
movl %r11d, %ecx
shlq %cl, %rbx
orq %rdx, %rbx
movq %r13, %rdx
movl $0x1, %r13d
subl %eax, %r13d
shlq %cl, %r13
shrq %rdx
orq %rdx, %r13
incq %r14
cmpq %r15, %r14
jb 0x13c616
cmpq %r13, %r9
jne 0x13c2fd
cmpq %rbx, %r8
jne 0x13c2fd
movq %r8, %rbx
movq %r9, %r13
jmp 0x13c616
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
jmp 0x13c616
setae %cl
testb %cl, %cl
movq %r8, %rax
cmovneq %rbx, %rax
movzbl %cl, %edx
cmpq $0x20, %r15
movq %r8, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq %rdx, 0x68(%rsp)
ja 0x13c337
shlq $0x20, %rax
testq %rdx, %rdx
movq %r9, %rbp
cmovneq %r13, %rbp
orq %rax, %rbp
jmp 0x13c3c2
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
testq %rdx, %rdx
movq %r9, %rax
cmovneq %r13, %rax
movq %rax, %rdx
notq %rdx
shlq $0x15, %rax
addq %rdx, %rax
movq %rax, %rdx
shrq $0x18, %rdx
xorq %rax, %rdx
imulq $0x109, %rdx, %rax      # imm = 0x109
movq %rax, %rdx
shrq $0xe, %rdx
xorq %rax, %rdx
leaq (%rdx,%rdx,4), %rax
leaq (%rdx,%rax,4), %rax
movq %rax, %rdx
shrq $0x1c, %rdx
xorq %rax, %rdx
addq %rcx, %rdx
movq %rdx, %rbp
shlq $0x1f, %rbp
addq %rdx, %rbp
movq %r14, 0x60(%rsp)
movq 0x30(%rsp), %rdx
movb 0x50(%rdx), %cl
movq $-0x1, %rax
shlq %cl, %rax
notq %rax
movq 0x78(%rdx), %r14
andq %rbp, %rax
shlq $0x5, %rax
movq (%r14,%rax), %r9
movq 0x10(%r9), %r8
testq %r8, %r8
je 0x13c47e
movb (%r9), %cl
movl $0x1, %r11d
shll %cl, %r11d
movq %rbp, %rdx
shrq $0xc, %rdx
imull $0x9e3779b9, %edx, %r15d # imm = 0x9E3779B9
negb %cl
shrl %cl, %r15d
addq %rax, %r14
leal -0x1(%r11), %ecx
movq 0x8(%r9), %r10
movl %r15d, %r9d
movq %r14, %rsi
movl %r9d, %eax
shrl $0x5, %eax
movl (%r10,%rax,4), %eax
btl %r9d, %eax
jae 0x13c460
movl %r9d, %edx
shlq $0x4, %rdx
movq (%r8,%rdx), %r14
movq %r14, %rax
xorq %rbp, %rax
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x13c557
incl %r9d
andl %ecx, %r9d
cmpl %r15d, %r9d
jne 0x13c428
xorl %r14d, %r14d
movq 0x38(%rsp), %r15
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
jmp 0x13c481
xorl %r14d, %r14d
leaq 0xfd798(%rip), %rbp     # 0x239c20
leaq -0x1(%r14), %rax
movq 0x30(%rsp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x13c607
cmpq $0x2, %r14
jb 0x13c4f0
movl $0x40, %ecx
movq 0x30(%rsp), %rax
subq 0x18(%rax), %rcx
xorl %edx, %edx
movq 0x50(%rsp), %rax
movq (%rax,%rdx,8), %r8
addq %r8, %r8
shrq %cl, %r8
incq %rdx
movq %rdx, %r9
cmpq %r14, %rdx
jae 0x13c4eb
movq 0x50(%rsp), %rax
movq (%rax,%r9,8), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %r8
je 0x13c607
incq %r9
cmpq %r14, %r9
jb 0x13c4cb
cmpq %r14, %rdx
jne 0x13c4b1
movq %r15, %rdx
movq %r12, %rcx
movq 0x68(%rsp), %r8
movq 0x20(%rsp), %r9
leaq 0x70(%rsp), %rax
pushq %rax
pushq %r14
pushq 0x60(%rsp)
pushq 0x48(%rsp)
callq 0x13bcbc
addq $0x20, %rsp
movq 0x70(%rsp), %rax
cmpq $-0x1, %rax
je 0x13c5f3
leaq 0x1(%rax), %rcx
cmpq %r15, %rcx
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
jae 0x13c624
notq %rax
addq %r15, %rax
movq %rax, %r14
jmp 0x13c63b
cmpl %r11d, %r9d
movq 0x38(%rsp), %r15
leaq 0xfd6ba(%rip), %rbp     # 0x239c20
jne 0x13c584
xorl %r14d, %r14d
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
jmp 0x13c488
movq 0x8(%r8,%rdx), %rdi
leaq (,%rdi,8), %rax
addq 0x10(%rsi), %rax
movq %rax, 0x50(%rsp)
movl $0xfff, %eax            # imm = 0xFFF
andq %rax, %r14
cmpq %rax, %r14
movq 0x28(%rsp), %r11
jne 0x13c5df
cmpl 0x18(%rsi), %r9d
jne 0x13c5b8
movq 0x8(%rsi), %r14
jmp 0x13c5dc
cmpl %r9d, %ecx
je 0x13c5b2
incl %r9d
movl %r9d, %eax
shrl $0x5, %eax
movl (%r10,%rax,4), %eax
btl %r9d, %eax
jae 0x13c5b8
movl %r9d, %eax
shlq $0x4, %rax
movq 0x8(%r8,%rax), %r14
subq %rdi, %r14
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
jmp 0x13c488
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x60(%rsp), %r14
incq %r12
cmpq %rsi, %r12
jb 0x13c27b
jmp 0x13c647
subq %r15, %r12
addq %rax, %r12
incq %r12
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
jmp 0x13c616
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, %rcx
movq 0x58(%rsp), %rbx
subq %rbx, %rcx
je 0x13c8f8
cmpq $0x2, %rcx
jb 0x13c690
movq 0x20(%rsp), %r14
movq 0x10(%r14), %rcx
movq %rbx, %rdi
shlq $0x4, %rdi
addq %rcx, %rdi
shlq $0x4, %rax
addq %rcx, %rax
movq %rax, %rsi
callq 0x139737
movq 0x8(%rsp), %rsi
movq (%r14), %rax
movq %rbx, %r14
cmpq %rbx, %rax
jbe 0x13c87c
movq %rsi, %xmm0
punpckldq 0xf4bd7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xf4bdf(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xfeb6b(%rip), %xmm1    # 0x23b230
addsd 0xf4c43(%rip), %xmm1    # 0x231310
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
subsd 0xf4c2b(%rip), %xmm1    # 0x231308
sarq $0x3f, %rdx
cvttsd2si %xmm1, %rbx
andq %rdx, %rbx
orq %rcx, %rbx
movq 0x20(%rsp), %rcx
movq 0x10(%rcx), %rsi
movq 0x58(%rsp), %r14
movq %r14, %r15
movq %r15, %r12
movq %r15, %r13
shlq $0x4, %r13
movq (%rsi,%r13), %rdx
movq 0x30(%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 0x13c754
subq %rdx, %r10
cmpq %rbx, %r10
ja 0x13c754
incq %r15
addq $0x10, %rax
cmpq %r15, %r9
jne 0x13c72f
movq %r9, %r15
movq %r15, %rbp
subq %r12, %rbp
cmpq $0x2, %rbp
jb 0x13c778
movq %r15, %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x139951
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
testq %rbp, %rbp
je 0x13c847
cmpq $0x1, %rbp
jne 0x13c793
addq %rsi, %r13
movl 0xc(%r13), %ebp
jmp 0x13c847
movq %r15, %rdi
shlq $0x4, %rdi
movq -0x8(%rsi,%rdi), %rax
movl %eax, %ecx
movq %rax, %rdx
shrq $0x20, %rdx
negq %rdx
leaq (%rcx,%rdx), %r9
incq %r9
movq %r9, %rdx
negq %rdx
addq $-0x2, %rbp
js 0x13c82b
notq %r12
addq %r15, %r12
leaq (%rsi,%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, %rcx
movq %r11, %rbp
cmovbq %rcx, %rbp
cmpq %rbp, %r13
jbe 0x13c809
addq %rdi, %rdx
leaq (%rcx,%rdx), %rdi
incq %rdi
movq %r11, %rcx
jmp 0x13c810
cmpq %r10, %r9
cmovbq %r9, %r10
movq %r10, %rdx
negq %rdx
decq %r12
addq $-0x10, %r8
movq %r10, %r9
testq %r12, %r12
jg 0x13c7ce
leaq 0x1(%rcx), %r8
jmp 0x13c831
leaq 0x1(%rcx), %r8
xorl %edi, %edi
addq %rdi, %r8
addq %rdx, %r8
addq %rdi, %rcx
subq %rcx, %rax
shlq $0x10, %rax
orq %r8, %rax
movq %rax, %rbp
movq %r15, %rax
shlq $0x4, %rax
movq %r14, %rcx
shlq $0x4, %rcx
movupd -0x10(%rsi,%rax), %xmm0
movupd %xmm0, (%rsi,%rcx)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
movl %ebp, 0xc(%rsi,%rcx)
incq %r14
movq (%rax), %rax
cmpq %rax, %r15
jb 0x13c6fd
movsd 0x38(%rsp), %xmm0
unpcklps 0xf49f7(%rip), %xmm0    # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq 0x20(%rsp), %rsi
movq %r14, (%rsi)
subpd 0xf49f7(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xf4363(%rip), %xmm1    # 0x230c10
xorpd %xmm0, %xmm0
cmpltsd %xmm1, %xmm0
andpd %xmm0, %xmm1
movsd 0xf4a4e(%rip), %xmm2    # 0x231310
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd 0xf4a2a(%rip), %xmm0    # 0x231308
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rax, %rcx
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rdx
callq 0x165bfb
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq