// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, 0x18(%rsp)
testq %rdx, %rdx
je 0x6ec51
movq %r9, %rbp
movq %r8, %r13
movq %rdx, %rbx
movq 0x88(%rsp), %r10
movq $0x0, (%r10)
cmpq %r9, 0x8(%r10)
movq %rsi, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
jae 0x6ea72
leaq -0x1(%rbp), %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(%r10)
movq 0x10(%r10), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x88(%rsp), %r10
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rsi
movq %rax, 0x10(%r10)
cmpq $0x1, %rbx
movq %rbx, 0x40(%rsp)
adcq $0x0, %rbx
movq %rbx, 0x38(%rsp)
movq 0x80(%rsp), %r8
xorl %r12d, %r12d
movl $0x2, %ebx
xorl %r14d, %r14d
movq (%rsi,%r14,8), %rax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %r9
addq %rax, %r9
movl 0xc(%r8,%r9), %eax
movl 0x10(%r8,%r9), %edx
imulq $0xa8, %rax, %rax
movq 0x18(%rsp), %rdi
movq 0x58(%rdi,%rax), %rax
shlq $0x5, %rdx
movslq (%rax,%rdx), %rdx
movl 0x18(%r8,%r9), %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
addq %rdi, %rdx
movl 0x14(%r8,%r9), %r9d
shlq $0x5, %r9
movl 0x4(%rax,%r9), %eax
subl %edi, %eax
incl %eax
movslq %eax, %r15
cmpq %rcx, %rdx
ja 0x6eb4f
cmpq %r13, %r15
jb 0x6eb4f
movq 0x8(%r10), %rax
cmpq %rax, (%r10)
jne 0x6eb39
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbx, %rsi
movq %rsi, 0x8(%r10)
movq 0x10(%r10), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x88(%rsp), %r10
movq 0x10(%rsp), %rcx
movq 0x80(%rsp), %r8
movq 0x28(%rsp), %rsi
movq %rax, 0x10(%r10)
movq (%rsi,%r14,8), %rax
movq (%r10), %r9
movq 0x10(%r10), %rdx
leaq 0x1(%r9), %rdi
movq %rdi, (%r10)
movq %rax, (%rdx,%r9,8)
cmpq %r13, %r15
adcq $0x0, %r12
incq %r14
cmpq %r14, 0x38(%rsp)
jne 0x6ea97
movq %r13, 0x20(%rsp)
movq (%r10), %rax
cmpq %rbp, %rax
jne 0x6eece
testq %rax, %rax
movq 0x10(%rsp), %rsi
je 0x6ee22
movq %rax, 0x8(%rsp)
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq $-0x1, %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x80(%rsp), %r8
movq 0x10(%r10), %rax
movl (%rax,%rbp,8), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rax, %rdx
leaq (%r8,%rdx), %rcx
movl 0xc(%r8,%rdx), %eax
imulq $0xa8, %rax, %rdi
addq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %r10, %r13
callq 0x6cc54
testq %rax, %rax
js 0x6eeaf
xorl %ecx, %ecx
cmpq %r14, %rax
sete %cl
addq %rcx, %rbx
cmpq %r14, %rax
cmovlq %rax, %r14
cmovlq %rbp, %r15
movl $0x1, %ecx
cmovlq %rcx, %rbx
shlq $0x20, %rax
movq 0x10(%r13), %rcx
orq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq %rbp, 0x8(%rsp)
movq 0x80(%rsp), %r8
movq 0x10(%rsp), %rsi
movq %r13, %r10
jne 0x6eba1
cmpq $0x1, %rbx
jne 0x6ec58
movl $0x1, %r13d
testq %r15, %r15
movq 0x18(%rsp), %rbp
je 0x6eca3
movq 0x10(%r10), %rax
movq (%rax,%r15,8), %rcx
movq (%rax), %rdx
movq %rdx, (%rax,%r15,8)
movq %rcx, (%rax)
jmp 0x6eca3
xorl %eax, %eax
jmp 0x6eea0
movq 0x18(%rsp), %rbp
jl 0x6eca0
movq 0x10(%r10), %rax
xorl %r13d, %r13d
xorl %ecx, %ecx
movq 0x8(%rsp), %r9
movq (%rax,%rcx,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
cmpq %r14, %rdi
jne 0x6ec91
cmpq %r13, %rcx
je 0x6ec8e
movq (%rax,%r13,8), %rdi
movq %rdi, (%rax,%rcx,8)
movq %rdx, (%rax,%r13,8)
incq %r13
incq %rcx
cmpq %r9, %rcx
jae 0x6eca8
cmpq %rbx, %r13
jl 0x6ec6d
jmp 0x6eca8
xorl %r13d, %r13d
movq 0x8(%rsp), %r9
movq 0x10(%r10), %rax
cmpq $0x1, %r9
movq %r9, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movl (%rax,%rdx,8), %r9d
leaq (%r9,%r9,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r9, %rdi
movl 0xc(%r8,%rdi), %edi
movl %edi, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x6ecb9
testq %r13, %r13
movq 0x20(%rsp), %r15
je 0x6ed1f
xorl %ebx, %ebx
movq 0x10(%r10), %rax
movl (%rax,%rbx,8), %eax
imulq $0xa8, %rax, %rdi
addq %rbp, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6c0db
movq 0x88(%rsp), %r10
movq 0x10(%rsp), %rsi
movq 0x10(%r10), %rax
movl (%rax,%rbx,8), %ecx
movq %rcx, (%rax,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x6ece4
cmpq $0x0, 0x90(%rsp)
je 0x6edd3
cmpq $0x2, %r13
movq 0x8(%rsp), %rbx
jb 0x6ed4e
movq 0x10(%r10), %rdi
leaq (%rdi,%r13,8), %rsi
callq 0x45d47
movq 0x88(%rsp), %r10
cmpq %rbx, %r13
movq 0x90(%rsp), %rax
movq %r13, 0x30(%rsp)
jae 0x6ee22
movq %rax, %r13
movq 0x30(%rsp), %rbp
movq 0x10(%r10), %rsi
movq (%rsi,%rbp,8), %r15
movl %r15d, %ebx
shrq $0x20, %r15
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
subq %r14, %r15
movq %r13, %rdi
movq 0x30(%rsp), %rdx
movq %rbx, %rcx
callq 0x6e96c
testq %rax, %rax
movl $0x0, %eax
cmovneq %rax, %r15
imulq $0xa8, %rbx, %rdi
movq 0x10(%rsp), %rsi
addq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
callq 0x6c0db
movq 0x88(%rsp), %r10
incq %rbp
cmpq %rbp, 0x8(%rsp)
jne 0x6ed6c
jmp 0x6ee22
movq 0x8(%rsp), %r15
cmpq %r15, %r13
movq 0x10(%rsp), %rbx
jae 0x6ee22
movq 0x10(%r10), %rax
movq (%rax,%r13,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subq %r14, %rcx
imulq $0xa8, %rax, %rdi
addq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x6c0db
movq 0x88(%rsp), %r10
incq %r13
cmpq %r13, %r15
jne 0x6ede2
testq %r12, %r12
movq 0x40(%rsp), %rax
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x18(%rsp), %rbx
je 0x6eea0
xorl %eax, %eax
movl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
xorl %ecx, %ecx
movq (%r11,%rcx,8), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
movq 0x80(%rsp), %r14
movl 0xc(%r14,%rsi), %edi
movl 0x14(%r14,%rsi), %r8d
imulq $0xa8, %rdi, %rdi
movq 0x58(%rbx,%rdi), %rdi
shlq $0x5, %r8
movl 0x4(%rdi,%r8), %edi
movl 0x18(%r14,%rsi), %esi
andl %r9d, %esi
subl %esi, %edi
incl %edi
movslq %edi, %rsi
cmpq %r10, %rsi
jb 0x6ee96
movq %rdx, (%r11,%rax,8)
incq %rax
incq %rcx
cmpq %rcx, 0x38(%rsp)
jne 0x6ee45
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b8cc2(%rip), %rdi    # 0x227b78
leaq 0x1b6786(%rip), %rsi    # 0x225643
leaq 0x1b900c(%rip), %rcx    # 0x227ed0
movl $0x4a2c, %edx           # imm = 0x4A2C
callq 0x2110
leaq 0x1b8bd4(%rip), %rdi    # 0x227aa9
leaq 0x1b6767(%rip), %rsi    # 0x225643
leaq 0x1b8fed(%rip), %rcx    # 0x227ed0
movl $0x4a26, %edx           # imm = 0x4A26
callq 0x2110