// File: chhylp123[P]hifiasm/hic.cpp
// Function: compress_mapped_pos_advance(ha_ug_index const*, kvec_vote*, unsigned long, unsigned long)
inline void compress_mapped_pos_advance(const ha_ug_index* idx, kvec_vote* buf, uint64_t buf_iter, uint64_t ovlp_thre)
{
    if(buf_iter >= buf->a.n)
    {
        buf->a.n = buf_iter;
        return;
    }
    s_hit *p = NULL;
    uint64_t rev, uID, ref_p, self_p, eLen, tLen, i, j, cnt;
    uint64_t max_beg = 0, max_end = 0, max_i, max_occ, cur_beg, cur_end, ovlp;
    uint64_t second_i = (uint64_t)-1, second_occ;
    uint64_t max_eLen, sec_eLen;
    double max_eRate, sec_eRate, eRate;
    p = buf->a.a + buf_iter;
    cnt = buf->a.n - buf_iter;
    if(cnt > 1) radix_sort_hc_s_hit_off_cnt(p, p + cnt); ///buf save all hits, here sort by offset in reads 
    max_eLen = 0; max_i = (uint64_t)-1; max_occ = 0; max_eRate = -1;
    for (j = 1, i = 0; j <= cnt; ++j)
    {
        if(j == cnt || p[j].off_cnt != p[i].off_cnt)
        {
            ///occ = j - i;
            interpret_pos((ha_ug_index*)idx, &p[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
            eRate = (double)(eLen)/(double)(tLen);
            if(is_update_hit(max_eLen, max_eRate, eLen, eRate))
            {
                max_eLen = eLen; max_eRate = eRate;
                max_end = self_p; max_beg = self_p + 1 - tLen;
                max_i = i; max_occ = j - i;
            }
            // fprintf(stderr, "\n++++++[%lu, %lu] uID: %lu, ref_p: %lu, self_p: %lu, eLen: %lu, tLen: %lu, max_i: %lu\n", 
            // i, j, uID, ref_p, self_p, eLen, tLen, max_i);
            i = j;///must
        }
    }


    sec_eLen = 0; second_i = (uint64_t)-1; second_occ = 0; sec_eRate = -1;
    for (j = 1, i = 0; j <= cnt; ++j)
    {
        if(j == cnt || p[j].off_cnt != p[i].off_cnt)
        {
            if(i != max_i)
            {
                interpret_pos((ha_ug_index*)idx, &p[i], &rev, &uID, &ref_p, &self_p, &eLen, &tLen);
                eRate = (double)(eLen)/(double)(tLen);
                cur_end = self_p;
                cur_beg = self_p + 1 - tLen;
                // fprintf(stderr, "\n----[%lu, %lu] uID: %lu, ref_p: %lu, self_p: %lu, eLen: %lu, tLen: %lu, max_i: %lu\n", 
                // i, j, uID, ref_p, self_p, eLen, tLen, max_i);
                // fprintf(stderr, "max_beg: %lu, max_end: %lu, cur_beg: %lu, cur_end: %lu\n", 
                // max_beg, max_end, cur_beg, cur_end);
                ///overlap with max interval
                if(MAX(cur_beg, max_beg) <= MIN(cur_end, max_end))
                {
                    ovlp = MIN(cur_end, max_end) - MAX(cur_beg, max_beg) + 1;
                    /*******************************for debug************************************/
                    if(ovlp == MIN(max_end+1-max_end, tLen))///for non-unique k-mer
                    {
                        i = j;///must
                        continue;///fully contain
                    }
                    if(ovlp > ((max_end+1-max_end)*0.8) && eLen > (max_eLen*0.8))///best is not unique
                    {
                        buf->a.n = buf_iter;
                        return;
                    }
                    if(ovlp > ((max_end+1-max_end)*0.15) + 1)
                    {
                        i = j;///must
                        continue;///fully contain
                    }
                    
                    // if(ovlp > (MIN((max_end+1-max_end), (cur_end+1-cur_end))*0.15) + 1)
                    // {
                    //     if(eLen > (max_eLen*0.8))///best is not unique
                    //     {
                    //         buf->a.n = buf_iter;
                    //         return;
                    //     }
                    //     i = j;///must
                    //     continue;
                    // }
                    /*******************************for debug************************************/
                }

                if(is_update_hit(sec_eLen, sec_eRate, eLen, eRate))
                {
                    sec_eLen = eLen; sec_eRate = eRate;
                    second_i = i; second_occ = j - i;
                }
            }
            i = j;///must
        }
    }

    // fprintf(stderr, "max_i: %lu, max_occ: %lu, second_i: %lu, second_occ: %lu\n", 
    //             max_i, max_occ, second_i, second_occ);

    if(second_i == (uint64_t)-1)
    {
        i = 0;
        for (j = max_i; j < max_i + max_occ; j++, i++) p[i] = p[j];
    }
    else ///be carful about overwritten
    {
        i = 0;
        if(max_i <= second_i)
        {
            for (j = max_i; j < max_i + max_occ; j++, i++) p[i] = p[j];
            for (j = second_i; j < second_i + second_occ; j++, i++) p[i] = p[j];
        }
        else
        {
            for (j = second_i; j < second_i + second_occ; j++, i++) p[i] = p[j];
            for (j = max_i; j < max_i + max_occ; j++, i++) p[i] = p[j];
        } 
    }

    buf->a.n = buf_iter + max_occ + second_occ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jb 0x2138c8
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x21413d
movq $0x0, -0x28(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $-0x1, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x1, -0x70(%rbp)
jbe 0x213930
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x70(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1be4b0
movq $0x0, -0xc0(%rbp)
movq $-0x1, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movsd 0x16e91f(%rip), %xmm0   # 0x382278
movsd %xmm0, -0xd0(%rbp)
movq $0x1, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0x213ad2
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x2139b5
movq -0x28(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
je 0x213abf
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq %rsp, %rax
leaq -0x58(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x40(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0x213560
movaps 0x16e61a(%rip), %xmm3   # 0x382010
movq -0x50(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x16e619(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x58(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x213a6a
movq -0xc0(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x213ab7
movsd -0xd0(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x213ab7
movq -0x50(%rbp), %rax
movq %rax, -0xc0(%rbp)
movsd -0xe0(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
subq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x213ac1
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x213971
movq $0x0, -0xc8(%rbp)
movq $-0x1, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movsd 0x16e77d(%rip), %xmm0   # 0x382278
movsd %xmm0, -0xd8(%rbp)
movq $0x1, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0x213ee0
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x213b57
movq -0x28(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
je 0x213ecd
movq -0x60(%rbp), %rax
cmpq -0x88(%rbp), %rax
je 0x213ec5
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq %rsp, %rax
leaq -0x58(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x40(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0x213560
movaps 0x16e467(%rip), %xmm3   # 0x382010
movq -0x50(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x16e466(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x58(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
subq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x213c28
movq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x213c33
movq -0x78(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x80(%rbp), %rax
ja 0x213c5e
movq -0xa0(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x213c69
movq -0x80(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rax
movq -0xf8(%rbp), %rcx
cmpq %rcx, %rax
ja 0x213e5e
movq -0xa0(%rbp), %rax
cmpq -0x80(%rbp), %rax
ja 0x213c9d
movq -0xa0(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x213ca8
movq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x213cd3
movq -0x98(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0x213cde
movq -0x78(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rax
movq -0x110(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
subq -0x80(%rbp), %rax
cmpq -0x58(%rbp), %rax
ja 0x213d2f
movq -0x80(%rbp), %rax
addq $0x1, %rax
subq -0x80(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x213d3a
movq -0x58(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rax
movq -0x120(%rbp), %rcx
cmpq %rcx, %rax
jne 0x213d5a
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x213ecf
movaps 0x16e2af(%rip), %xmm1   # 0x382010
movq -0xa8(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x16e2ab(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x16e274(%rip), %xmm1   # 0x382000
movsd 0x16e49c(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x213e02
movq -0x50(%rbp), %xmm0
movaps 0x16e266(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x16e26a(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0xc0(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
movsd 0x16e448(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x213e02
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x21413d
movaps 0x16e207(%rip), %xmm1   # 0x382010
movq -0xa8(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x16e203(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x16e1cc(%rip), %xmm1   # 0x382000
movsd 0x16e40c(%rip), %xmm2   # 0x382248
mulsd %xmm2, %xmm1
movsd 0x16e1b8(%rip), %xmm2   # 0x382000
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x213e5c
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x213ecf
jmp 0x213e5e
movq -0xc8(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x213e8e
movq -0xc8(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x213ec3
movsd -0xd8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x213ec3
movq -0x50(%rbp), %rax
movq %rax, -0xc8(%rbp)
movsd -0xe0(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x213ec5
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x213ecf
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x213b13
cmpq $-0x1, -0xb0(%rbp)
jne 0x213f5f
movq $0x0, -0x60(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x88(%rbp), %rcx
addq -0x90(%rbp), %rcx
cmpq %rcx, %rax
jae 0x213f5a
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x213efd
jmp 0x214124
movq $0x0, -0x60(%rbp)
movq -0x88(%rbp), %rax
cmpq -0xb0(%rbp), %rax
ja 0x214050
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x88(%rbp), %rcx
addq -0x90(%rbp), %rcx
cmpq %rcx, %rax
jae 0x213fe3
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x213f86
movq -0xb0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0xb0(%rbp), %rcx
addq -0xb8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x21404b
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x213fee
jmp 0x214122
movq -0xb0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0xb0(%rbp), %rcx
addq -0xb8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x2140b8
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x21405b
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x88(%rbp), %rcx
addq -0x90(%rbp), %rcx
cmpq %rcx, %rax
jae 0x214120
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2140c3
jmp 0x214122
jmp 0x214124
movq -0x18(%rbp), %rcx
addq -0x90(%rbp), %rcx
addq -0xb8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)