// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rcx, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
testq %rdx, %rdx
je 0x3cfa2
movq %r9, %rbx
movq %r8, %r13
movq %rdx, %r12
movq %rsi, %rbp
movq 0x88(%rsp), %r9
andq $0x0, (%r9)
cmpq %rbx, 0x8(%r9)
jae 0x3ccee
leaq -0x1(%rbx), %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(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x88(%rsp), %r9
movq %rax, 0x10(%r9)
movq %rbx, 0x18(%rsp)
xorl %ebx, %ebx
pushq $0x2
popq %r15
xorl %eax, %eax
movq %rax, 0x38(%rsp)
cmpq %rbx, %r12
je 0x3cdc6
movq (%rbp,%rbx,8), %rcx
imulq $0x1c, %rcx, %rax
movq 0x80(%rsp), %r8
movl 0xc(%r8,%rax), %edx
movl 0x10(%r8,%rax), %esi
imulq $0xa8, %rdx, %rdx
movq 0x10(%rsp), %rdi
movq 0x58(%rdi,%rdx), %rdx
shlq $0x5, %rsi
movslq (%rdx,%rsi), %rsi
movl 0x18(%r8,%rax), %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
addq %rdi, %rsi
movl 0x14(%r8,%rax), %eax
shlq $0x5, %rax
movl 0x4(%rdx,%rax), %eax
subl %edi, %eax
incl %eax
movslq %eax, %r14
cmpq 0x28(%rsp), %rsi
ja 0x3cdb2
cmpq %r13, %r14
jb 0x3cdb2
movq (%r9), %rdx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rdx
jne 0x3cda7
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r15, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x88(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%rbp,%rbx,8), %rcx
movq (%r9), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r9)
movq %rcx, (%rax,%rdx,8)
cmpq %r13, %r14
movq 0x38(%rsp), %rax
adcq $0x0, %rax
incq %rbx
jmp 0x3ccfb
movq %rbp, 0x40(%rsp)
movq %r13, 0x8(%rsp)
movq %r12, 0x30(%rsp)
movq 0x18(%rsp), %rax
cmpq %rax, (%r9)
jne 0x3d0fe
testq %rax, %rax
je 0x3d04c
pushq $-0x1
popq %r13
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
pushq $0x1
popq %r14
xorl %r15d, %r15d
movq 0x18(%rsp), %r12
cmpq %rbp, %r12
je 0x3ce87
movq 0x10(%r9), %rax
movl (%rax,%rbp,8), %eax
imulq $0x1c, %rax, %rax
movq 0x80(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movl 0xc(%rdx,%rax), %eax
imulq $0xa8, %rax, %rdi
addq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r9, %rbx
callq 0x3b1ec
testq %rax, %rax
js 0x3d0df
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
cmpq %rdx, %rax
sete %cl
addq %rcx, %r15
cmpq %rdx, %rax
cmovlq %rax, %rdx
movq %rdx, 0x20(%rsp)
cmovlq %rbp, %r13
cmovlq %r14, %r15
shlq $0x20, %rax
movq 0x10(%rbx), %rcx
orq %rax, (%rcx,%rbp,8)
incq %rbp
movq %rbx, %r9
jmp 0x3ce08
cmpq $0x1, %r15
jne 0x3cfa9
movq 0x10(%r9), %rdi
testq %r13, %r13
je 0x3cea8
movq (%rdi,%r13,8), %rax
movq (%rdi), %rcx
movq %rcx, (%rdi,%r13,8)
movq %rax, (%rdi)
movq 0x8(%rsp), %r13
xorl %eax, %eax
cmpq %rax, %r12
je 0x3cecf
movl (%rdi,%rax,8), %ecx
imulq $0x1c, %rcx, %rcx
movq 0x80(%rsp), %rdx
movl 0xc(%rdx,%rcx), %ecx
movl %ecx, (%rdi,%rax,8)
incq %rax
jmp 0x3ceaf
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x3cf0c
movl (%rdi,%rbx,8), %eax
imulq $0xa8, %rax, %rdi
addq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x3a78e
movq 0x88(%rsp), %rax
movq 0x10(%rax), %rdi
movl (%rdi,%rbx,8), %eax
movq %rax, (%rdi,%rbx,8)
incq %rbx
jmp 0x3ced1
cmpq $0x0, 0x90(%rsp)
je 0x3cff4
cmpq $0x2, %r14
jb 0x3cf2a
leaq (%rdi,%r14,8), %rsi
callq 0x1905d
xorl %ebp, %ebp
movq %r14, %rbx
movq 0x88(%rsp), %rax
cmpq 0x18(%rsp), %rbx
jae 0x3d04c
movq 0x10(%rax), %rsi
movq (%rsi,%rbx,8), %r13
movl %r13d, %r15d
shrq $0x20, %r13
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
subq 0x20(%rsp), %r13
movq 0x90(%rsp), %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x3cc03
testq %rax, %rax
cmovneq %rbp, %r13
imulq $0xa8, %r15, %rdi
addq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
callq 0x3a78e
movq 0x88(%rsp), %rax
incq %rbx
jmp 0x3cf37
xorl %eax, %eax
jmp 0x3d0d0
movq 0x10(%r9), %rdi
xorl %eax, %eax
xorl %r14d, %r14d
movq 0x8(%rsp), %r13
cmpq %r12, %rax
jae 0x3cead
cmpq %r15, %r14
jge 0x3cead
movq (%rdi,%rax,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq 0x20(%rsp), %rdx
jne 0x3cfef
cmpq %r14, %rax
je 0x3cfec
movq (%rdi,%r14,8), %rdx
movq %rdx, (%rdi,%rax,8)
movq %rcx, (%rdi,%r14,8)
incq %r14
incq %rax
jmp 0x3cfb7
movq 0x18(%rsp), %rax
movq 0x88(%rsp), %rcx
cmpq %rax, %r14
jae 0x3d04c
movq 0x10(%rcx), %rax
movq (%rax,%r14,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subq 0x20(%rsp), %rcx
imulq $0xa8, %rax, %rdi
addq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x3a78e
movq 0x88(%rsp), %rcx
incq %r14
movq 0x18(%rsp), %rax
jmp 0x3d001
cmpq $0x0, 0x38(%rsp)
je 0x3d0cb
xorl %eax, %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
xorl %edx, %edx
movq 0x30(%rsp), %r10
movq 0x8(%rsp), %r11
movq 0x40(%rsp), %rbx
movq 0x10(%rsp), %r14
cmpq %rax, %r10
je 0x3d0c6
movq (%rbx,%rax,8), %rsi
imulq $0x1c, %rsi, %rdi
movq 0x80(%rsp), %r15
movl 0xc(%r15,%rdi), %r8d
movl 0x14(%r15,%rdi), %r9d
imulq $0xa8, %r8, %r8
movq 0x58(%r14,%r8), %r8
shlq $0x5, %r9
movl 0x4(%r8,%r9), %r8d
movl 0x18(%r15,%rdi), %edi
andl %ecx, %edi
subl %edi, %r8d
incl %r8d
movslq %r8d, %rdi
cmpq %r11, %rdi
jb 0x3d0c1
movq %rsi, (%rbx,%rdx,8)
incq %rdx
incq %rax
jmp 0x3d071
movq %rdx, %rax
jmp 0x3d0d0
movq 0x30(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a8af2(%rip), %rdi    # 0x1e5bd8
leaq 0x1a65b6(%rip), %rsi    # 0x1e36a3
leaq 0x1a8e3c(%rip), %rcx    # 0x1e5f30
movl $0x4a2c, %edx           # imm = 0x4A2C
callq 0x2110
leaq 0x1a8a04(%rip), %rdi    # 0x1e5b09
leaq 0x1a6597(%rip), %rsi    # 0x1e36a3
leaq 0x1a8e1d(%rip), %rcx    # 0x1e5f30
movl $0x4a26, %edx           # imm = 0x4A26
callq 0x2110