// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, %rbx
movq (%rsi), %r14
movq %r14, %r12
subq %rdx, %r12
jbe 0x15f5fb
movq %rsi, 0x20(%rsp)
movq 0x10(%rsi), %r13
movq %rbx, %rbp
shlq $0x4, %rbp
movq %r13, %r15
addq %rbp, %r15
cmpq $0x2, %r12
jb 0x15f27a
movq %r14, %rsi
shlq $0x4, %rsi
addq %r13, %rsi
movq %r15, %rdi
callq 0x1339a2
leaq 0x8(%r15), %rsi
movq $-0x1, 0x18(%rsp)
cmpq %rbx, %r14
movq %rbx, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
jne 0x15f2ae
xorpd %xmm0, %xmm0
xorl %r9d, %r9d
xorl %r8d, %r8d
movq $-0x1, %rbp
xorl %eax, %eax
jmp 0x15f39d
movq %r14, 0x30(%rsp)
movq %r13, 0x28(%rsp)
leaq 0x18(,%r13), %rdi
addq %rbp, %rdi
movq $-0x1, %rbp
movl $0x1, %r10d
movsd 0xc6260(%rip), %xmm0    # 0x225538
xorl %r11d, %r11d
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r10, %r12
je 0x15f300
movq (%rdi), %rdx
movq %rcx, %rbx
shlq $0x4, %rbx
cmpq 0x8(%r15,%rbx), %rdx
jne 0x15f300
movq %rcx, %r13
jmp 0x15f360
movq %rcx, %rdx
shlq $0x4, %rdx
movq (%rsi,%rdx), %r14
movq %r14, %rdx
shrq $0x20, %rdx
movzwl %dx, %edx
movq %r14, %rbx
shrq $0x30, %rbx
addq %rdx, %rbx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
divsd %xmm2, %xmm1
cmpq %rdx, %r11
jb 0x15f341
jne 0x15f379
ucomisd %xmm0, %xmm1
movq %r10, %r13
jbe 0x15f360
movl %r14d, %r8d
movq %r8, %r9
subq %rbx, %r9
incq %r9
movq %r10, %rax
subq %rcx, %rax
movq %rcx, %rbp
movq %r10, %r13
movq %rdx, %r11
movapd %xmm1, %xmm0
leaq 0x1(%r10), %rdx
addq $0x10, %rdi
movq %r13, %rcx
cmpq %r12, %r10
movq %rdx, %r10
jb 0x15f2e5
jmp 0x15f37e
movq %r10, %r13
jmp 0x15f360
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
mulsd 0xc6122(%rip), %xmm0    # 0x2254b0
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r13
cmpq %rbx, %r14
jne 0x15f43b
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %r9
cmpq $-0x1, %r9
je 0x15f553
cmpq %r9, %rbp
movq 0x8(%rsp), %r10
jbe 0x15f587
leaq (%r10,%r9), %rcx
cmpq %rcx, %r9
movq 0x10(%rsp), %rdi
jae 0x15f3fb
movq %r9, %rdx
shlq $0x4, %rdx
movq %r10, %rsi
movq %r15, %r8
movupd (%r8,%rdx), %xmm0
movupd %xmm0, (%r8)
addq $0x10, %r8
decq %rsi
jne 0x15f3e7
leaq (%rax,%rbp), %rdx
cmpq %rdx, %rbp
jae 0x15f5ed
cmpq %rcx, %r9
cmovaq %r9, %rcx
shlq $0x4, %rcx
shlq $0x4, %r9
subq %r9, %rcx
shlq $0x4, %rbp
movq %rax, %rdx
movupd (%r15,%rbp), %xmm0
movupd %xmm0, (%r15,%rcx)
addq $0x10, %r15
decq %rdx
jne 0x15f421
jmp 0x15f5ed
movq 0x8(%rsp), %rcx
leaq (%rcx,%r13), %r10
addq $0x18, %r10
movl $0x1, %r11d
movq $-0x1, 0x18(%rsp)
movsd 0xc60d9(%rip), %xmm1    # 0x225538
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
cmpq %r11, %r12
je 0x15f48c
movq (%r10), %rcx
movq %r13, %rdx
shlq $0x4, %rdx
cmpq 0x8(%r15,%rdx), %rcx
jne 0x15f48c
movq %r13, %rbx
jmp 0x15f50c
cmpq %rbp, %r13
jne 0x15f496
movq %r11, %rbx
jmp 0x15f50c
movq %r13, %rcx
shlq $0x4, %rcx
movq (%rsi,%rcx), %rbx
movl %ebx, %edi
movq %rbx, %rcx
shrq $0x20, %rcx
movzwl %cx, %ecx
shrq $0x30, %rbx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
addq %rcx, %rbx
movq %rdi, %rdx
subq %rbx, %rdx
incq %rdx
cmpq %r9, %rdx
cmovbeq %r9, %rdx
cmpq %r8, %rdi
cmovaeq %r8, %rdi
subq %rdx, %rdi
jae 0x15f524
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
divsd %xmm3, %xmm2
cmpq %rcx, %r14
jb 0x15f4f2
jne 0x15f491
ucomisd %xmm1, %xmm2
movq %r11, %rbx
jbe 0x15f50c
movq %r11, %rdx
subq %r13, %rdx
movq %rdx, 0x8(%rsp)
movq %r11, %rbx
movq %r13, 0x18(%rsp)
movq %rcx, %r14
movapd %xmm2, %xmm1
incq %r11
addq $0x10, %r10
movq %rbx, %r13
cmpq %r12, %r11
jbe 0x15f46e
jmp 0x15f3af
incq %rdi
xorl %edx, %edx
testq %rbx, %rbx
setne %dl
cmpq %rdx, %rdi
je 0x15f491
testq %rdi, %rdi
je 0x15f547
ucomisd %xmm0, %xmm2
ja 0x15f60d
cmpq $0x1, %rdi
ja 0x15f491
jmp 0x15f4d7
leaq (%rax,%rbp), %rcx
cmpq %rcx, %rbp
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r10
jae 0x15f5ed
shlq $0x4, %rbp
movq %rax, %rcx
movupd (%r15,%rbp), %xmm0
movupd %xmm0, (%r15)
addq $0x10, %r15
decq %rcx
jne 0x15f571
jmp 0x15f5ed
leaq (%rax,%rbp), %rcx
cmpq %rcx, %rbp
movq 0x10(%rsp), %rdi
jae 0x15f5b6
movq %rbp, %rdx
shlq $0x4, %rdx
movq %rax, %rsi
movq %r15, %r8
movupd (%r8,%rdx), %xmm0
movupd %xmm0, (%r8)
addq $0x10, %r8
decq %rsi
jne 0x15f5a2
leaq (%r10,%r9), %rdx
cmpq %rdx, %r9
jae 0x15f5ed
cmpq %rcx, %rbp
cmovaq %rbp, %rcx
shlq $0x4, %rcx
shlq $0x4, %rbp
subq %rbp, %rcx
shlq $0x4, %r9
movq %r10, %rdx
movupd (%r15,%r9), %xmm0
movupd %xmm0, (%r15,%rcx)
addq $0x10, %r15
decq %rdx
jne 0x15f5d8
addq %rdi, %rax
addq %r10, %rax
movq %rax, %rbx
movq 0x20(%rsp), %rsi
movq %rbx, (%rsi)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rbx
jmp 0x15f5fb