// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rsi, %rax
movq %rdi, 0x80(%rsp)
movq (%rsi), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
jbe 0x12100e
movq %rax, 0x28(%rsp)
movq 0x10(%rax), %rbx
movq %rdx, 0x20(%rsp)
shlq $0x4, %rdx
movq %rdx, 0x18(%rsp)
leaq (%rbx,%rdx), %r15
cmpq $0x2, %rdi
movq %rdi, 0x10(%rsp)
jb 0x120eba
shlq $0x4, %rsi
addq %rbx, %rsi
movq %r15, %rdi
callq 0xf3184
movq 0x10(%rsp), %rdi
movq %rbx, 0x30(%rsp)
movq 0x18(%rsp), %rax
addq %rax, %rbx
addq $0x18, %rbx
movsd 0xc26ad(%rip), %xmm0    # 0x1e3580
movsd %xmm0, 0x8(%rsp)
pushq $0x1
popq %r12
pushq $-0x1
popq %rax
movq %rax, 0x70(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x78(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %rdi, %r12
ja 0x121016
je 0x120f23
movq (%rbx), %rax
movq %r14, %rcx
shlq $0x4, %rcx
cmpq 0x8(%r15,%rcx), %rax
jne 0x120f23
movq %r14, %rcx
jmp 0x120ffa
movq %r14, %rsi
shlq $0x4, %rsi
addq %r15, %rsi
movq 0x80(%rsp), %rdi
leaq 0x98(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x58(%rsp), %r9
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0x120d2b
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
movq 0x50(%rsp), %rax
movq %rax, %xmm1
movq 0xc22f2(%rip), %xmm2    # 0x1e3270
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xc22f6(%rip), %xmm3    # 0x1e3280
subpd %xmm3, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x48(%rsp), %rdx
movq %rdx, %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm3, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
cmpq %rax, %r13
jb 0x120fce
jne 0x121009
ucomisd 0x8(%rsp), %xmm0
movq %r12, %rcx
jbe 0x120ffa
movq 0x58(%rsp), %rbp
movq %rbp, 0x78(%rsp)
subq %rdx, %rbp
incq %rbp
movq %r12, %rcx
subq %r14, %rcx
movq %rcx, 0x40(%rsp)
movq %r14, 0x70(%rsp)
movq %r12, %rcx
movq %rax, %r13
movsd %xmm0, 0x8(%rsp)
incq %r12
addq $0x10, %rbx
movq %rcx, %r14
jmp 0x120eff
movq %r12, %rcx
jmp 0x120ffa
movq %rdx, (%rax)
jmp 0x121314
movq %r13, %xmm0
punpckldq 0xc224d(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xc2255(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xc24c1(%rip), %xmm1    # 0x1e3500
movapd %xmm1, 0xa0(%rsp)
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq 0x18(%rcx,%rax), %r12
movsd 0xc2521(%rip), %xmm0    # 0x1e3580
movsd %xmm0, 0x68(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x60(%rsp)
pushq $0x1
popq %r13
xorl %ebx, %ebx
movq $0x0, 0x38(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x70(%rsp), %r14
cmpq %rdi, %r13
ja 0x1211fa
je 0x1210af
movq (%r12), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
cmpq 0x8(%r15,%rcx), %rax
jne 0x1210af
movq %rbx, %rcx
jmp 0x1211eb
cmpq %r14, %rbx
jne 0x1210bc
movq %r13, %rcx
jmp 0x1211eb
movq %rbx, %rsi
shlq $0x4, %rsi
addq %r15, %rsi
movq 0x80(%rsp), %rdi
leaq 0x98(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x58(%rsp), %r9
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0x120d2b
popq %rax
popq %rcx
movq 0x50(%rsp), %rax
movq %rax, %xmm1
punpckldq 0xc2160(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xc2168(%rip), %xmm1    # 0x1e3280
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x48(%rsp), %rcx
movq 0x58(%rsp), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
incq %rsi
cmpq %rbp, %rsi
cmovbeq %rbp, %rsi
movq 0x78(%rsp), %rdi
cmpq %rdi, %rdx
cmovaeq %rdi, %rdx
movq %rdx, %rdi
subq %rsi, %rdi
jae 0x1211b4
movq %rcx, %xmm1
punpckldq 0xc2111(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xc2119(%rip), %xmm1    # 0x1e3280
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
cmpq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
jb 0x121194
jne 0x1210b4
ucomisd 0x68(%rsp), %xmm0
movq %r13, %rcx
jbe 0x1211eb
movq %r13, %rcx
subq %rbx, %rcx
movq %rcx, 0x38(%rsp)
movq %r13, %rcx
movq %rbx, 0x60(%rsp)
movq %rax, 0x8(%rsp)
movsd %xmm0, 0x68(%rsp)
jmp 0x1211eb
incq %rdi
xorl %r8d, %r8d
testq %rcx, %rcx
setne %r8b
cmpq %r8, %rdi
je 0x1211e3
testq %rdi, %rdi
je 0x121152
ucomisd 0xa0(%rsp), %xmm0
ja 0x121326
cmpq %rsi, %rdx
je 0x121152
movq %r13, %rcx
movq 0x10(%rsp), %rdi
incq %r13
addq $0x10, %r12
movq %rcx, %rbx
jmp 0x12108a
movq 0x60(%rsp), %r10
cmpq $-0x1, %r10
je 0x121247
cmpq %r10, %r14
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %r9
jbe 0x121283
leaq (%r9,%r10), %rax
movq %r10, %rcx
shlq $0x4, %rcx
movq 0x30(%rsp), %r8
cmpq %rax, %r10
jae 0x1212ac
movupd (%r15,%rcx), %xmm0
movupd %xmm0, (%r15)
incq %r10
addq $0x10, %r15
jmp 0x12122e
movq 0x40(%rsp), %rdi
leaq (%rdi,%r14), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x38(%rsp), %r9
cmpq %rax, %r14
jae 0x121308
movupd (%r15,%rcx), %xmm0
movupd %xmm0, (%r15)
incq %r14
addq $0x10, %r15
jmp 0x121266
leaq (%rdi,%r14), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movq 0x30(%rsp), %r8
cmpq %rax, %r14
jae 0x1212da
movupd (%r15,%rcx), %xmm0
movupd %xmm0, (%r15)
incq %r14
addq $0x10, %r15
jmp 0x121293
leaq (%rdi,%r14), %rax
addq 0x18(%rsp), %r8
movq %r14, %rcx
shlq $0x4, %rcx
addq %r8, %rcx
cmpq %rax, %r14
jae 0x121308
movupd (%rcx), %xmm0
movupd %xmm0, (%r15)
incq %r14
addq $0x10, %rcx
addq $0x10, %r15
jmp 0x1212bf
leaq (%r9,%r10), %rax
addq 0x18(%rsp), %r8
movq %r10, %rcx
shlq $0x4, %rcx
addq %r8, %rcx
cmpq %rax, %r10
jae 0x121308
movupd (%rcx), %xmm0
movupd %xmm0, (%r15)
incq %r10
addq $0x10, %rcx
addq $0x10, %r15
jmp 0x1212ed
addq %rsi, %rdi
addq %r9, %rdi
movq %rdi, %rax
movq %rax, (%rdx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rax
jmp 0x121311