// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_region_phase(overlap_region*, unsigned long*, unsigned long, unsigned long, unsigned long, unsigned long, ul_ov_t*, asg64_v*, asg64_v*)
uint64_t gen_region_phase(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, asg64_v *ovidx)
{
    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;
        q[1] = ol[ovlp_id(*p)].w_list.a[ovlp_max_wid(*p)].x_end+1;
        if(q[0]<=s && q[1]>=e) {
            kv_push(uint64_t, *buf, id_a[k]);
            // buf[buf_n++] = id_a[k];
        }
        if(q[1] < e) rm_n++;
    }
    buf_n = buf->n;
    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)]); 
            // fprintf(stderr, "+++[M::%s::utg%.6dl] wid::%u, xoff::%u, coff::%u\n", __func__, 
            //     (int32_t)ol[ovlp_id(*p)].y_id+1, ovlp_cur_wid(*p), ovlp_cur_xoff(*p), ovlp_cur_coff(*p));
            err = extract_sub_cigar_err(z, s, e, p);
            // int64_t debug_err = extract_sub_cigar_err_debug(z, s, e);
            // assert(err == debug_err);
            // fprintf(stderr, "[M::%s::] err::%ld, debug_err::%ld\n", __func__, err, debug_err);

            // fprintf(stderr, "---[M::%s::utg%.6dl] wid::%u, xoff::%u, coff::%u, err::%ld\n", __func__, 
            //     (int32_t)ol[ovlp_id(*p)].y_id+1, ovlp_cur_wid(*p), ovlp_cur_xoff(*p), ovlp_cur_coff(*p), err);
            fprintf(stderr, "---[M::%s::utg%.6dl] xoff::[%lu, %lu), err::%ld\n", __func__, 
                (int32_t)ol[ovlp_id(*p)].y_id+1, s, e, err);
            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;
            // 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);
        }

        for (k = mn; k < buf_n; k++) {
            z = &(ol[id_get(buf->a[k])]); 
            push_sec_aln(z, s, e, ((buf->a[k]&id_set)?(0):(err_get(buf->a[k])-msc)));
        }

        for (k = 0; k < buf_n; k++) {
            ol[id_get(buf->a[k])].overlapLen = (uint32_t)-1;
        }
    }


    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;
            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
testq %rdx, %rdx
je 0x6c17e
movq %r9, %r14
movq %r8, %rbx
movq %rcx, %r9
movq %rdi, %r12
movq 0x88(%rsp), %r11
movq $0x0, (%r11)
cmpq %r14, 0x8(%r11)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
jae 0x6bf67
leaq -0x1(%r14), %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 0x88(%rsp), %r11
movq 0x8(%rsp), %r9
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rax, 0x10(%r11)
movq %r14, 0x18(%rsp)
movq 0x80(%rsp), %r10
xorl %r14d, %r14d
movl $0x2, %r15d
xorl %r13d, %r13d
movq (%rsi,%r13,8), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movl 0xc(%r10,%rax), %edi
movl 0x10(%r10,%rax), %r8d
imulq $0xa8, %rdi, %rdi
movq 0x58(%r12,%rdi), %rdi
shlq $0x5, %r8
movslq (%rdi,%r8), %r8
movl 0x14(%r10,%rax), %eax
shlq $0x5, %rax
movslq 0x4(%rdi,%rax), %rbp
incq %rbp
cmpq %r9, %r8
ja 0x6c025
cmpq %rbx, %rbp
jb 0x6c025
movq (%r11), %rdi
movq 0x10(%r11), %rax
cmpq 0x8(%r11), %rdi
jne 0x6c01a
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %r15, %rsi
movq %rsi, 0x8(%r11)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x88(%rsp), %r11
movq 0x8(%rsp), %r9
movq 0x80(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rax, 0x10(%r11)
movq (%rsi,%r13,8), %rcx
movq (%r11), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%r11)
movq %rcx, (%rax,%rdi,8)
cmpq %rbx, %rbp
adcq $0x0, %r14
incq %r13
cmpq %r13, %rdx
jne 0x6bf80
movq 0x18(%rsp), %rax
cmpq %rax, (%r11)
jne 0x6c35a
testq %rax, %rax
je 0x6c2d9
movq 0x10(%r11), %rax
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq $-0x1, %r8
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %rbx, 0x10(%rsp)
movq %r8, 0x40(%rsp)
movq %r11, 0x20(%rsp)
movq %rcx, 0x38(%rsp)
movl (%rax,%rcx,8), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r15
addq %rax, %r15
leaq (%r10,%r15), %rcx
movl 0xc(%r10,%r15), %eax
imulq $0xa8, %rax, %rdi
addq %r12, %rdi
movq %r9, %rsi
movq %rbx, %rdx
movq %r9, %rbx
callq 0x6b7ed
movq %rax, %rbp
movq 0x228f2e(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movq 0x80(%rsp), %rax
movl 0xc(%rax,%r15), %eax
imulq $0xa8, %rax, %rax
movl 0x10(%r12,%rax), %ecx
incl %ecx
movq %rbp, (%rsp)
leaq 0x1c7b6b(%rip), %rsi    # 0x233c46
leaq 0x1c7b95(%rip), %rdx    # 0x233c77
movq %rbx, %r8
movq 0x10(%rsp), %rbx
movq %rbx, %r9
xorl %eax, %eax
callq 0x22b0
testq %rbp, %rbp
js 0x6c33b
xorl %eax, %eax
movq 0x20(%rsp), %r11
cmpq %r11, %rbp
sete %al
addq %rax, %r13
cmpq %r11, %rbp
cmovlq %rbp, %r11
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %rcx
cmovlq %rcx, %r8
movl $0x1, %eax
cmovlq %rax, %r13
shlq $0x20, %rbp
movq 0x88(%rsp), %rdx
movq 0x10(%rdx), %rax
orq %rbp, (%rax,%rcx,8)
incq %rcx
movq 0x18(%rsp), %rdi
cmpq %rcx, %rdi
movq 0x80(%rsp), %r10
movq 0x8(%rsp), %r9
jne 0x6c06a
cmpq $0x1, %r13
jne 0x6c185
movl $0x1, %r15d
testq %r8, %r8
je 0x6c1c2
movq (%rax,%r8,8), %rcx
movq (%rax), %rsi
movq %rsi, (%rax,%r8,8)
movq %rcx, (%rax)
jmp 0x6c1c2
xorl %eax, %eax
jmp 0x6c32c
jl 0x6c1bf
xorl %ecx, %ecx
xorl %r15d, %r15d
movq (%rax,%rcx,8), %r8
movq %r8, %rsi
shrq $0x20, %rsi
cmpq %r11, %rsi
jne 0x6c1b0
cmpq %r15, %rcx
je 0x6c1ad
movq (%rax,%r15,8), %rsi
movq %rsi, (%rax,%rcx,8)
movq %r8, (%rax,%r15,8)
incq %r15
incq %rcx
cmpq %rdi, %rcx
jae 0x6c1c2
cmpq %r13, %r15
jl 0x6c18c
jmp 0x6c1c2
xorl %r15d, %r15d
movq %r11, 0x20(%rsp)
xorl %ecx, %ecx
movl (%rax,%rcx,8), %r8d
leaq (%r8,%r8,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %r8, %rsi
movl 0xc(%r10,%rsi), %esi
movl %esi, (%rax,%rcx,8)
imulq $0xa8, %rsi, %rsi
movl %ecx, 0x20(%r12,%rsi)
incq %rcx
cmpq %rcx, %rdi
jne 0x6c1c9
testq %r15, %r15
movq 0x90(%rsp), %rbx
je 0x6c246
xorl %ebp, %ebp
movq 0x10(%rdx), %rax
movl (%rax,%rbp,8), %eax
imulq $0xa8, %rax, %r13
addq %r12, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rcx
callq 0x6bd76
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x6bb93
movq 0x88(%rsp), %rdx
incq %rbp
cmpq %rbp, %r15
jne 0x6c203
movq 0x18(%rsp), %rbp
cmpq %rbp, %r15
movq 0x20(%rsp), %r13
movq 0x8(%rsp), %rbx
jae 0x6c2a1
movq 0x10(%rdx), %rax
movq (%rax,%r15,8), %rax
movl %eax, %ecx
imulq $0xa8, %rcx, %rdi
addq %r12, %rdi
movq %rax, %rcx
shrq $0x20, %rcx
subq %r13, %rcx
testq %rax, %rax
movl $0x0, %eax
cmovsq %rax, %rcx
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
callq 0x6bb93
movq 0x88(%rsp), %rdx
incq %r15
cmpq %r15, %rbp
jne 0x6c25a
movq 0x10(%rdx), %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq 0x80(%rsp), %r10
movq 0x10(%rsp), %rbx
movl (%rax,%rcx,8), %edi
imulq $0xa8, %rdi, %rdi
movl $0xffffffff, 0x20(%r12,%rdi) # imm = 0xFFFFFFFF
incq %rcx
cmpq %rcx, %rbp
jne 0x6c2be
testq %r14, %r14
je 0x6c329
xorl %ecx, %ecx
xorl %eax, %eax
movq (%rsi,%rcx,8), %r8
leaq (%r8,%r8,8), %rdi
leaq (%rdi,%rdi,2), %r9
addq %r8, %r9
movl 0xc(%r10,%r9), %edi
movl 0x14(%r10,%r9), %r9d
imulq $0xa8, %rdi, %rdi
movq 0x58(%r12,%rdi), %rdi
shlq $0x5, %r9
movslq 0x4(%rdi,%r9), %rdi
incq %rdi
cmpq %rbx, %rdi
jb 0x6c31f
movq %r8, (%rsi,%rax,8)
incq %rax
incq %rcx
cmpq %rcx, %rdx
jne 0x6c2e2
jmp 0x6c32c
movq %rdx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c7946(%rip), %rdi    # 0x233c88
leaq 0x1c540a(%rip), %rsi    # 0x231753
leaq 0x1c7875(%rip), %rcx    # 0x233bc5
movl $0x4765, %edx           # imm = 0x4765
callq 0x2110
leaq 0x1c7858(%rip), %rdi    # 0x233bb9
leaq 0x1c53eb(%rip), %rsi    # 0x231753
leaq 0x1c7856(%rip), %rcx    # 0x233bc5
movl $0x4755, %edx           # imm = 0x4755
callq 0x2110