// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x20(%rbp), %rcx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1c27a6
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x1c8363(%rip), %rax    # 0x38a7e0
movzbl (%rax,%rcx), %eax
movl %eax, -0xec(%rbp)
cmpl $0x4, -0xec(%rbp)
jge 0x1c275f
movq -0xd0(%rbp), %rax
shlq %rax
movl -0xec(%rbp), %ecx
andl $0x1, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
andq -0xd8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
shlq %rax
movl -0xec(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
orq %rcx, %rax
andq -0xd8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
shrq %rax
movl -0xec(%rbp), %edx
andl $0x1, %edx
movl $0x1, %ecx
subl %edx, %ecx
movslq %ecx, %rdx
movq -0xe0(%rbp), %rcx
shlq %cl, %rdx
movq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
shrq %rax
movl -0xec(%rbp), %edx
sarl %edx
movl $0x1, %ecx
subl %edx, %ecx
movslq %ecx, %rdx
movq -0xe0(%rbp), %rcx
shlq %cl, %rdx
movq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
cmpq -0x20(%rbp), %rax
jb 0x1c275d
leaq -0xd0(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq -0x78(%rbp), %rsi
callq 0x212f90
movq %rax, -0xe8(%rbp)
cmpq $-0x1, -0x78(%rbp)
jne 0x1c2585
jmp 0x1c2795
movq -0x30(%rbp), %rdi
movq -0xe8(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x213060
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x48(%rcx), %rax
ja 0x1c25bb
cmpq $0x0, -0x88(%rbp)
ja 0x1c25c0
jmp 0x1c2795
cmpq $0x1, -0x88(%rbp)
jbe 0x1c269c
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x1c2688
movq -0x80(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
movq -0x30(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0xa8(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x1c2666
movq -0xa8(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
shlq %rdx
movq -0x30(%rbp), %rsi
movl $0x40, %ecx
subq 0x18(%rsi), %rcx
shrq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
jne 0x1c2656
jmp 0x1c2666
jmp 0x1c2658
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1c2619
movq -0x50(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x1c2675
jmp 0x1c2688
jmp 0x1c2677
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1c25d6
movq -0x48(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x1c269a
jmp 0x1c2795
jmp 0x1c269c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x28(%rbp), %r9
movq -0x30(%rbp), %rbx
movq -0x80(%rbp), %r11
movq -0x88(%rbp), %r10
leaq -0x68(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x1c1e00
cmpq $-0x1, -0x68(%rbp)
je 0x1c275b
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
cmpq -0x20(%rbp), %rax
jb 0x1c2749
movq $0x0, -0x58(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x40(%rbp), %rax
addq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
subq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x1c2759
movq -0x20(%rbp), %rax
subq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1c275b
jmp 0x1c275d
jmp 0x1c2793
movq $0x0, -0x58(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
jmp 0x1c2795
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1c245a
movq -0x28(%rbp), %rax
movq (%rax), %rax
subq -0x38(%rbp), %rax
cmpq $0x0, %rax
jne 0x1c27bc
jmp 0x1c2bc4
movq -0x28(%rbp), %rax
movq (%rax), %rax
subq -0x38(%rbp), %rax
cmpq $0x1, %rax
jbe 0x1c27fb
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x38(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1bdb80
movaps 0x1bf80e(%rip), %xmm1   # 0x382010
movq -0x18(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1bf80d(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x1c9006(%rip), %xmm1   # 0x38b830
mulsd %xmm1, %xmm0
movsd 0x1bf7ca(%rip), %xmm1   # 0x382000
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x1bf7a7(%rip), %xmm1   # 0x381ff8
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1c2ac5
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq -0x90(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
leaq -0xa0(%rbp), %r8
leaq -0x98(%rbp), %r9
leaq -0x88(%rbp), %rax
xorl %r10d, %r10d
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x213560
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rdx
xorl %eax, %eax
cmpq (%rdx), %rcx
movb %al, -0x111(%rbp)
jae 0x1c2997
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %rcx
subq $0x1, %rcx
shrq %cl, %rax
movq %rax, -0x120(%rbp)
movq -0xf8(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rcx
subq $0x1, %rcx
shrq %cl, %rdx
movq -0x120(%rbp), %rcx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x111(%rbp)
jne 0x1c2997
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
subq -0xf8(%rbp), %rax
cmpq -0x100(%rbp), %rax
setbe %al
movb %al, -0x111(%rbp)
movb -0x111(%rbp), %al
testb $0x1, %al
jne 0x1c29a3
jmp 0x1c29b4
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1c28fb
movq -0x40(%rbp), %rax
subq -0x108(%rbp), %rax
cmpq $0x1, %rax
jbe 0x1c29f3
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x108(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1be010
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x108(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
subq -0x108(%rbp), %rsi
callq 0x213680
movq %rax, -0x110(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x40(%rbp), %rax
subq $0x1, %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
shlq $0x20, %rcx
shrq $0x20, %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x110(%rbp), %rcx
shlq $0x20, %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1c2873
movq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x128(%rbp)
movaps 0x1bf518(%rip), %xmm1   # 0x382010
movq -0x20(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1bf517(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x1bc4f8(%rip), %xmm1   # 0x37f018
mulsd %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1c2b6a
movaps 0x1bf4dc(%rip), %xmm1   # 0x382010
movq -0x20(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1bf4db(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x1bc4bc(%rip), %xmm1   # 0x37f018
mulsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
jmp 0x1c2b7c
movsd 0x1bf48e(%rip), %xmm0   # 0x382000
movsd %xmm0, -0x140(%rbp)
jmp 0x1c2b7c
movq -0x128(%rbp), %rdx
movq -0x130(%rbp), %rsi
movq -0x138(%rbp), %rdi
movsd -0x140(%rbp), %xmm0
movsd 0x1bf457(%rip), %xmm2   # 0x381ff8
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rax
cvttsd2si %xmm0, %rcx
movq %rcx, %r8
sarq $0x3f, %r8
andq %r8, %rax
orq %rax, %rcx
callq 0x213890
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %rbp
retq
nop