// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rax
movq (%rsi), %rsi
movq %rsi, %r12
subq %rdx, %r12
jbe 0x165f88
movq %rax, 0x18(%rsp)
movq 0x10(%rax), %rbx
movq %rdx, 0x10(%rsp)
movq %rdx, %rbp
shlq $0x4, %rbp
leaq (%rbx,%rbp), %r15
cmpq $0x2, %r12
jb 0x165c49
shlq $0x4, %rsi
addq %rbx, %rsi
movq %r15, %rdi
callq 0x139b6c
movq %rbx, (%rsp)
leaq (%rbx,%rbp), %rdx
addq $0x18, %rdx
movq $-0x1, %rcx
movl $0x1, %r8d
movsd 0xcb9ce(%rip), %xmm0    # 0x231638
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %eax, %eax
xorl %esi, %esi
xorl %edi, %edi
movq %r10, %r11
shlq $0x4, %r11
cmpq %r8, %r12
jne 0x165c89
movq 0x8(%r15,%r11), %rbx
jmp 0x165c98
movq 0x8(%r15,%r11), %rbx
cmpq %rbx, (%rdx)
jne 0x165c98
movq %r10, %r13
jmp 0x165cef
movq %rbx, %r11
shrq $0x20, %r11
movzwl %r11w, %r11d
movq %rbx, %r14
shrq $0x30, %r14
addq %r11, %r14
xorps %xmm1, %xmm1
cvtsi2sd %r11d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
divsd %xmm2, %xmm1
cmpq %r11, %r9
jb 0x165cd1
jne 0x165d08
ucomisd %xmm0, %xmm1
movq %r8, %r13
jbe 0x165cef
movl %ebx, %esi
movq %rsi, %rdi
subq %r14, %rdi
incq %rdi
movq %r8, %rax
subq %r10, %rax
movq %r10, %rcx
movq %r8, %r13
movq %r11, %r9
movapd %xmm1, %xmm0
leaq 0x1(%r8), %r11
addq $0x10, %rdx
movq %r13, %r10
cmpq %r12, %r8
movq %r11, %r8
jb 0x165c76
jmp 0x165d0d
movq %r8, %r13
jmp 0x165cef
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
mulsd 0xcb893(%rip), %xmm0    # 0x2315b0
movq (%rsp), %rdx
leaq (%rdx,%rbp), %r9
addq $0x18, %r9
movl $0x1, %r10d
movq $-0x1, 0x20(%rsp)
movsd 0xcb8f8(%rip), %xmm1    # 0x231638
xorl %r11d, %r11d
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
cmpq %r10, %r12
je 0x165d6d
movq (%r9), %rdx
movq %r13, %r8
shlq $0x4, %r8
cmpq 0x8(%r15,%r8), %rdx
jne 0x165d6d
movq %r13, %rbx
jmp 0x165e26
cmpq %rcx, %r13
jne 0x165d7a
movq %r10, %rbx
jmp 0x165e26
movq %r13, %rdx
shlq $0x4, %rdx
movq 0x8(%r15,%rdx), %rbx
movl %ebx, %edx
movq %rbx, %r8
shrq $0x20, %r8
movzwl %r8w, %ebp
shrq $0x30, %rbx
addq %rbp, %rbx
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
movq %rdx, %r14
subq %rbx, %r14
incq %r14
cmpq %rdi, %r14
cmovbeq %rdi, %r14
cmpq %rsi, %rdx
cmovaeq %rsi, %rdx
movq %rdx, %r8
subq %r14, %r8
jae 0x165df7
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
divsd %xmm3, %xmm2
cmpq %rbp, %r11
jb 0x165ddb
jne 0x165d72
ucomisd %xmm1, %xmm2
movq %r10, %rbx
jbe 0x165e26
movq %r10, %rdx
subq %r13, %rdx
movq %rdx, 0x8(%rsp)
movq %r10, %rbx
movq %r13, 0x20(%rsp)
movq %rbp, %r11
movapd %xmm2, %xmm1
jmp 0x165e26
movq %rsi, (%rsp)
movq %rax, %rsi
movq %rcx, %rax
movq %r11, %rcx
incq %r8
xorl %r11d, %r11d
testq %rbx, %rbx
setne %r11b
cmpq %r11, %r8
jne 0x165e3b
movq %r10, %rbx
movq %rcx, %r11
movq %rax, %rcx
movq %rsi, %rax
movq (%rsp), %rsi
incq %r10
addq $0x10, %r9
movq %rbx, %r13
cmpq %r12, %r10
jbe 0x165d4f
jmp 0x165e69
testq %r8, %r8
movq %rcx, %r11
movq %rax, %rcx
movq %rsi, %rax
movq (%rsp), %rsi
je 0x165dc0
ucomisd %xmm0, %xmm2
ja 0x165f9a
cmpq %r14, %rdx
jne 0x165d72
jmp 0x165dc0
movq 0x20(%rsp), %r10
cmpq $-0x1, %r10
je 0x165eec
cmpq %r10, %rcx
movq 0x8(%rsp), %r9
jbe 0x165f17
leaq (%r9,%r10), %rsi
cmpq %rsi, %r10
jae 0x165eac
movq %r10, %rdi
shlq $0x4, %rdi
movq %r9, %rdx
movq %r15, %r8
movupd (%r8,%rdi), %xmm0
movupd %xmm0, (%r8)
addq $0x10, %r8
decq %rdx
jne 0x165e98
leaq (%rax,%rcx), %rdx
cmpq %rdx, %rcx
jae 0x165f78
cmpq %rsi, %r10
cmovaq %r10, %rsi
shlq $0x4, %rsi
shlq $0x4, %r10
subq %r10, %rsi
shlq $0x4, %rcx
movq %rax, %rdx
movupd (%r15,%rcx), %xmm0
movupd %xmm0, (%r15,%rsi)
addq $0x10, %r15
decq %rdx
jne 0x165ed2
jmp 0x165f78
leaq (%rax,%rcx), %rdx
cmpq %rdx, %rcx
movq 0x8(%rsp), %r9
jae 0x165f78
shlq $0x4, %rcx
movq %rax, %rdx
movupd (%r15,%rcx), %xmm0
movupd %xmm0, (%r15)
addq $0x10, %r15
decq %rdx
jne 0x165f01
jmp 0x165f78
leaq (%rax,%rcx), %rsi
cmpq %rsi, %rcx
jae 0x165f41
movq %rcx, %rdi
shlq $0x4, %rdi
movq %rax, %rdx
movq %r15, %r8
movupd (%r8,%rdi), %xmm0
movupd %xmm0, (%r8)
addq $0x10, %r8
decq %rdx
jne 0x165f2d
leaq (%r9,%r10), %rdx
cmpq %rdx, %r10
jae 0x165f78
cmpq %rsi, %rcx
cmovaq %rcx, %rsi
shlq $0x4, %rsi
shlq $0x4, %rcx
subq %rcx, %rsi
shlq $0x4, %r10
movq %r9, %rcx
movupd (%r15,%r10), %xmm0
movupd %xmm0, (%r15,%rsi)
addq $0x10, %r15
decq %rcx
jne 0x165f63
addq 0x10(%rsp), %rax
addq %r9, %rax
movq %rax, %rdx
movq 0x18(%rsp), %rax
movq %rdx, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rdx
jmp 0x165f88