// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_region_phase_robust_rr(overlap_region*, unsigned long*, unsigned long, unsigned long, unsigned long, unsigned long, ul_ov_t*, asg64_v*, mask_ul_ov_t*)
uint64_t gen_region_phase_robust_rr(overlap_region* ol, uint64_t *id_a, uint64_t id_n, uint64_t s, uint64_t e, uint64_t dp, ul_ov_t *c_idx, asg64_v *buf, mask_ul_ov_t *mk)
{
    if(!id_n) return id_n;
    uint64_t k, m, mn, q[2], buf_n, rm_n, oid; int64_t err, msc, msc_k, msc_n;
    overlap_region *z; ul_ov_t *p; buf->n = 0; kv_resize(uint64_t, *buf, dp);
    for (k = buf_n = rm_n = 0; k < id_n; k++) {
        p = &(c_idx[id_a[k]]);
        q[0] = ol[ovlp_id(*p)].w_list.a[ovlp_min_wid(*p)].x_start+ovlp_bd(*p);
        q[1] = ol[ovlp_id(*p)].w_list.a[ovlp_max_wid(*p)].x_end+1-ovlp_bd(*p);
        if(q[0]<=s && q[1]>=e) {
            kv_push(uint64_t, *buf, id_a[k]);
        }
        if(q[1] < e) rm_n++;
    }
    buf_n = buf->n;
    // fprintf(stderr, "[M::%s] buf_n::%lu, dp::%lu\n", __func__, buf_n, dp);
    assert(buf_n == dp);//not right

    if(buf_n > 0) {
        for (k = 0, msc = INT32_MAX, msc_k = -1, msc_n = 0; k < buf_n; k++) {
            p = &(c_idx[(uint32_t)buf->a[k]]); z = &(ol[ovlp_id(*p)]); 
            err = extract_sub_cigar_err_rr(z, s, e, p);
            assert(err >= 0);  
            if(err < msc) {
                msc = err; msc_k = k; msc_n = 1;
            } else if(err == msc) {
                msc_n++;
            }
            buf->a[k] |= (((uint64_t)err)<<32);       
        }

        if(msc_n == 1) {
            p = &(c_idx[(uint32_t)buf->a[msc_k]]);
            z = &(ol[ovlp_id(*p)]); mn = 1;
            if(msc_k != 0) {
                m = buf->a[msc_k]; 
                buf->a[msc_k] = buf->a[0]; 
                buf->a[0] = m;
            }
        } else {
            for (k = mn = 0; k < buf_n && (int64_t)mn < msc_n; k++) {
                p = &(c_idx[(uint32_t)buf->a[k]]); 
                z = &(ol[ovlp_id(*p)]); 
                if((buf->a[k]>>32) == (uint64_t)msc) {
                    if(mn != k) {
                        m = buf->a[k]; 
                        buf->a[k] = buf->a[mn]; 
                        buf->a[mn] = m;
                    }
                    mn++;
                }
            }
        }
        // fprintf(stderr, "[M::%s] buf_n::%ld, msc_n::%ld, mn::%lu\n", __func__, buf_n, msc_n, mn);
        for (k = 0; k < buf_n; k++) {
            oid = ovlp_id((c_idx[(uint32_t)buf->a[k]]));
            buf->a[k] >>= 32; buf->a[k] <<= 32; buf->a[k] |= oid;
            // ol[oid].overlapLen = k;//no need to set 
            // if(s == 158482) fprintf(stderr, "k->%ld::oid->%ld[M::%s::utg%.6dl] pos::[%lu, %lu)\n", k, oid, __func__,  
            //                         (int32_t)ol[oid].y_id+1, s, e);
        }

        for (k = 0; k < mn; k++) {///best alignment
            z = &(ol[id_get(buf->a[k])]); 
            // reassign_sec_err(ol, ovidx, buf, k);
            // push_sec_aln(z, s, e, 0);
            push_sec_aln_robust(z, s, e, 0);
            buf->a[k] = id_get(buf->a[k]);///all equally best overlap pieces
        }
        if(!mk) {
            for (k = mn; k < buf_n; k++) {
                err = err_get(buf->a[k])-msc;
                z = &(ol[id_get(buf->a[k])]); 
                push_sec_aln_robust(z, s, e, err);
            }
        } else {
            if(mn > 1) radix_sort_bc64(buf->a, buf->a + mn);
            for (k = mn; k < buf_n; k++) {
                err = err_get(buf->a[k])-msc;
                if(is_mask_ov(mk, buf->a, mn, id_get(buf->a[k]))) err = 0;
                z = &(ol[id_get(buf->a[k])]); 
                push_sec_aln_robust(z, s, e, err);
            }
        }
    }


    if(rm_n) {
        for (k = m = 0; k < id_n; k++) {
            p = &(c_idx[id_a[k]]);
            q[1] = ol[ovlp_id(*p)].w_list.a[ovlp_max_wid(*p)].x_end+1-ovlp_bd(*p);
            if(q[1] < e) continue;
            id_a[m++] = id_a[k];
        }
        id_n = m;
    }
    return id_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, (%rsp)
testq %rdx, %rdx
je 0x6e640
movq %r8, %r13
movq %rdx, %rbp
movq 0x78(%rsp), %r11
movq $0x0, (%r11)
movq %r9, 0x8(%rsp)
cmpq %r9, 0x8(%r11)
movq %rsi, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
jae 0x6e486
movq 0x8(%rsp), %rax
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r11)
movq 0x10(%r11), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x78(%rsp), %r11
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rsi
movq %rax, 0x10(%r11)
movq 0x70(%rsp), %r9
xorl %r12d, %r12d
movl $0x2, %ebx
xorl %r14d, %r14d
movq (%rsi,%r14,8), %r10
leaq (%r10,%r10,8), %rax
leaq (%rax,%rax,2), %rax
addq %r10, %rax
movl 0xc(%r9,%rax), %edx
movl 0x10(%r9,%rax), %edi
imulq $0xa8, %rdx, %rdx
movq (%rsp), %r8
movq 0x58(%r8,%rdx), %rdx
shlq $0x5, %rdi
movslq (%rdx,%rdi), %r8
movl 0x18(%r9,%rax), %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
addq %rdi, %r8
movl 0x14(%r9,%rax), %eax
shlq $0x5, %rax
movl 0x4(%rdx,%rax), %eax
subl %edi, %eax
incl %eax
movslq %eax, %r15
cmpq %rcx, %r8
ja 0x6e545
cmpq %r13, %r15
jb 0x6e545
movq (%r11), %rdx
movq 0x10(%r11), %rax
cmpq 0x8(%r11), %rdx
jne 0x6e53a
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbx, %rsi
movq %rsi, 0x8(%r11)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x78(%rsp), %r11
movq 0x10(%rsp), %rcx
movq 0x70(%rsp), %r9
movq 0x20(%rsp), %rsi
movq %rax, 0x10(%r11)
movq (%rsi,%r14,8), %r10
movq (%r11), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%r11)
movq %r10, (%rax,%rdx,8)
cmpq %r13, %r15
adcq $0x0, %r12
incq %r14
cmpq %r14, %rbp
jne 0x6e496
movq %rbp, 0x28(%rsp)
movq %r13, 0x18(%rsp)
movq 0x8(%rsp), %rax
cmpq %rax, (%r11)
jne 0x6e8a0
testq %rax, %rax
movq 0x18(%rsp), %rbp
movq (%rsp), %r13
movq 0x10(%rsp), %rax
je 0x6e7f7
movq 0x10(%r11), %rdi
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq $-0x1, %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x70(%rsp), %r9
movq %rax, %rsi
movl (%rdi,%r14,8), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rax, %rdx
leaq (%r9,%rdx), %rcx
movl 0xc(%r9,%rdx), %eax
imulq $0xa8, %rax, %rdi
addq (%rsp), %rdi
movq %rbp, %rdx
callq 0x6c765
testq %rax, %rax
js 0x6e881
xorl %ecx, %ecx
cmpq %rbx, %rax
sete %cl
addq %rcx, %r15
cmpq %rbx, %rax
cmovlq %rax, %rbx
cmovlq %r14, %r13
movl $0x1, %ecx
cmovlq %rcx, %r15
shlq $0x20, %rax
movq 0x78(%rsp), %r10
movq 0x10(%r10), %rdi
orq %rax, (%rdi,%r14,8)
incq %r14
movq 0x8(%rsp), %r8
cmpq %r14, %r8
movq 0x70(%rsp), %r9
movq 0x10(%rsp), %rsi
jne 0x6e5a5
cmpq $0x1, %r15
jne 0x6e647
movl $0x1, %ebp
testq %r13, %r13
je 0x6e686
movq (%rdi,%r13,8), %rax
movq (%rdi), %rcx
movq %rcx, (%rdi,%r13,8)
movq %rax, (%rdi)
jmp 0x6e686
xorl %eax, %eax
jmp 0x6e872
jl 0x6e684
xorl %ebp, %ebp
xorl %eax, %eax
movq (%rsp), %r13
movq (%rdi,%rax,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq %rbx, %rdx
jne 0x6e675
cmpq %rbp, %rax
je 0x6e672
movq (%rdi,%rbp,8), %rdx
movq %rdx, (%rdi,%rax,8)
movq %rcx, (%rdi,%rbp,8)
incq %rbp
incq %rax
cmpq %r8, %rax
jae 0x6e68a
cmpq %r15, %rbp
jl 0x6e651
jmp 0x6e68a
xorl %ebp, %ebp
movq (%rsp), %r13
xorl %eax, %eax
movl (%rdi,%rax,8), %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%r9,%rdx), %ecx
movl %ecx, (%rdi,%rax,8)
incq %rax
cmpq %rax, %r8
jne 0x6e68c
testq %rbp, %rbp
je 0x6e724
xorl %r14d, %r14d
movq 0x18(%rsp), %r15
movq %rsi, %r13
movl (%rdi,%r14,8), %eax
imulq $0xa8, %rax, %rdi
addq (%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6bc87
movq 0x78(%rsp), %r10
movq 0x10(%r10), %rdi
movl (%rdi,%r14,8), %eax
movq %rax, (%rdi,%r14,8)
incq %r14
cmpq %r14, %rbp
jne 0x6e6ba
movq 0x80(%rsp), %r14
testq %r14, %r14
movq 0x8(%rsp), %r8
movq (%rsp), %r13
je 0x6e7ad
cmpq $0x1, %rbp
je 0x6e731
leaq (%rdi,%rbp,8), %rsi
callq 0x45373
movq 0x78(%rsp), %r10
movq 0x8(%rsp), %r8
jmp 0x6e731
movq 0x80(%rsp), %r14
testq %r14, %r14
je 0x6e7ad
cmpq %r8, %rbp
movq %rbp, 0x30(%rsp)
jae 0x6e7f7
movq %r14, %rbp
movq 0x30(%rsp), %r14
movq 0x10(%r10), %rsi
movq (%rsi,%r14,8), %r15
movl %r15d, %r13d
shrq $0x20, %r15
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
subq %rbx, %r15
movq %rbp, %rdi
movq 0x30(%rsp), %rdx
movq %r13, %rcx
callq 0x6e381
testq %rax, %rax
movl $0x0, %eax
cmovneq %rax, %r15
imulq $0xa8, %r13, %rdi
movq (%rsp), %r13
addq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
callq 0x6bc87
movq 0x78(%rsp), %r10
incq %r14
cmpq %r14, 0x8(%rsp)
jne 0x6e747
jmp 0x6e7f7
cmpq %r8, %rbp
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
jae 0x6e7f7
movq 0x10(%r10), %rax
movq (%rax,%rbp,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subq %rbx, %rcx
imulq $0xa8, %rax, %rdi
addq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6bc87
movq 0x78(%rsp), %r10
incq %rbp
cmpq %rbp, 0x8(%rsp)
jne 0x6e7bc
testq %r12, %r12
je 0x6e86d
xorl %eax, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
xorl %edx, %edx
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %rbx
movq (%rbx,%rdx,8), %rsi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
movq 0x70(%rsp), %r14
movl 0xc(%r14,%rdi), %r8d
movl 0x14(%r14,%rdi), %r9d
imulq $0xa8, %r8, %r8
movq 0x58(%r13,%r8), %r8
shlq $0x5, %r9
movl 0x4(%r8,%r9), %r8d
movl 0x18(%r14,%rdi), %edi
andl %ecx, %edi
subl %edi, %r8d
incl %r8d
movslq %r8d, %rdi
cmpq %r10, %rdi
jb 0x6e863
movq %rsi, (%rbx,%rax,8)
incq %rax
incq %rdx
cmpq %rdx, %r11
jne 0x6e814
jmp 0x6e872
movq 0x28(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c5400(%rip), %rdi    # 0x233c88
leaq 0x1c2ec4(%rip), %rsi    # 0x231753
leaq 0x1c574a(%rip), %rcx    # 0x233fe0
movl $0x4a2c, %edx           # imm = 0x4A2C
callq 0x2110
leaq 0x1c5312(%rip), %rdi    # 0x233bb9
leaq 0x1c2ea5(%rip), %rsi    # 0x231753
leaq 0x1c572b(%rip), %rcx    # 0x233fe0
movl $0x4a26, %edx           # imm = 0x4A26
callq 0x2110