// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rcx, 0x8(%rsp)
testq %rdx, %rdx
je 0x3ad84
movq %r9, %r15
movq %r8, %r14
movq %rdx, %r13
movq %rdi, %r12
movq %rsi, 0x18(%rsp)
movq 0x98(%rsp), %rbx
andq $0x0, (%rbx)
cmpq %r9, 0x8(%rbx)
jae 0x3aa34
leaq -0x1(%r15), %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(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq %r15, 0x10(%rsp)
movq 0x90(%rsp), %r9
xorl %r15d, %r15d
xorl %edi, %edi
movq %r12, 0x50(%rsp)
cmpq %r15, %r13
je 0x3ab10
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rcx
imulq $0x1c, %rcx, %rax
movl 0xc(%r9,%rax), %edx
movl 0x10(%r9,%rax), %esi
imulq $0xa8, %rdx, %rdx
movq 0x58(%r12,%rdx), %rdx
shlq $0x5, %rsi
movslq (%rdx,%rsi), %rsi
movl 0x14(%r9,%rax), %eax
shlq $0x5, %rax
movslq 0x4(%rdx,%rax), %rbp
incq %rbp
cmpq 0x8(%rsp), %rsi
ja 0x3ab01
cmpq %r14, %rbp
jb 0x3ab01
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x3aaf6
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %r14, %r12
movq %rbx, %r14
movq %rdi, %rbx
movq %rax, %rdi
callq 0x2370
movq %rbx, %rdi
movq %r14, %rbx
movq %r12, %r14
movq 0x50(%rsp), %r12
movq 0x90(%rsp), %r9
movq %rax, 0x10(%rbx)
movq 0x18(%rsp), %rcx
movq (%rcx,%r15,8), %rcx
movq (%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movq %rcx, (%rax,%rdx,8)
cmpq %r14, %rbp
adcq $0x0, %rdi
incq %r15
jmp 0x3aa4b
movq %rdi, 0x30(%rsp)
movq 0x10(%rsp), %rdi
cmpq %rdi, (%rbx)
jne 0x3adfb
testq %rdi, %rdi
je 0x3ad2e
movq %r13, 0x28(%rsp)
pushq $-0x1
popq %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
pushq $0x1
popq %rbp
xorl %ecx, %ecx
xorl %r10d, %r10d
cmpq %rcx, %rdi
movq %r8, 0x20(%rsp)
je 0x3ac2b
movq %rsi, 0x40(%rsp)
movq %r10, 0x48(%rsp)
movq 0x10(%rbx), %rax
movq %rcx, 0x38(%rsp)
movl (%rax,%rcx,8), %eax
imulq $0x1c, %rax, %r13
leaq (%r9,%r13), %rcx
movl 0xc(%r9,%r13), %eax
imulq $0xa8, %rax, %rdi
addq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x3a359
movq %rax, %r15
movq 0x21844c(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq 0x90(%rsp), %rax
movl 0xc(%rax,%r13), %eax
imulq $0xa8, %rax, %rax
movl 0x10(%r12,%rax), %ecx
incl %ecx
movq %r15, (%rsp)
leaq 0x1aafd9(%rip), %rsi    # 0x1e5b96
leaq 0x1ab003(%rip), %rdx    # 0x1e5bc7
movq 0x8(%rsp), %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x22b0
testq %r15, %r15
js 0x3addc
xorl %eax, %eax
movq 0x20(%rsp), %r8
cmpq %r8, %r15
sete %al
movq 0x48(%rsp), %r10
addq %rax, %r10
cmpq %r8, %r15
cmovlq %r15, %r8
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rcx
cmovlq %rcx, %rsi
cmovlq %rbp, %r10
shlq $0x20, %r15
movq 0x10(%rbx), %rax
orq %r15, (%rax,%rcx,8)
incq %rcx
movq 0x10(%rsp), %rdi
movq 0x90(%rsp), %r9
jmp 0x3ab42
cmpq $0x1, %r10
jne 0x3ad99
movq 0x10(%rbx), %rax
testq %rsi, %rsi
je 0x3ac4c
movq (%rax,%rsi,8), %rcx
movq (%rax), %rdx
movq %rdx, (%rax,%rsi,8)
movq %rcx, (%rax)
xorl %ecx, %ecx
cmpq %rcx, %rdi
je 0x3ac73
movl (%rax,%rcx,8), %edx
imulq $0x1c, %rdx, %rdx
movl 0xc(%r9,%rdx), %edx
movl %edx, (%rax,%rcx,8)
imulq $0xa8, %rdx, %rdx
movl %ecx, 0x20(%r12,%rdx)
incq %rcx
jmp 0x3ac4e
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x3acba
movq 0x10(%rbx), %rax
movl (%rax,%r15,8), %eax
imulq $0xa8, %rax, %r13
addq %r12, %r13
movq %r12, %rdi
movq 0xa0(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x3a875
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x3a6a2
incq %r15
jmp 0x3ac76
xorl %r15d, %r15d
movq 0x20(%rsp), %r13
movq 0x10(%rbx), %rax
cmpq 0x10(%rsp), %rbp
jae 0x3ad00
movq (%rax,%rbp,8), %rax
movl %eax, %ecx
imulq $0xa8, %rcx, %rdi
addq %r12, %rdi
movq %rax, %rcx
shrq $0x20, %rcx
subq %r13, %rcx
testq %rax, %rax
cmovsq %r15, %rcx
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x3a6a2
incq %rbp
jmp 0x3acc2
xorl %ecx, %ecx
movq 0x28(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x90(%rsp), %r9
cmpq %rcx, %rsi
je 0x3ad2e
movl (%rax,%rcx,8), %edx
imulq $0xa8, %rdx, %rdx
orl $-0x1, 0x20(%r12,%rdx)
incq %rcx
jmp 0x3ad14
cmpq $0x0, 0x30(%rsp)
je 0x3ad87
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %r8
cmpq %rcx, %r13
je 0x3ad7f
movq (%r8,%rcx,8), %rdx
imulq $0x1c, %rdx, %rsi
movl 0xc(%r9,%rsi), %edi
movl 0x14(%r9,%rsi), %esi
imulq $0xa8, %rdi, %rdi
movq 0x58(%r12,%rdi), %rdi
shlq $0x5, %rsi
movslq 0x4(%rdi,%rsi), %rsi
incq %rsi
cmpq %r14, %rsi
jb 0x3ad7a
movq %rdx, (%r8,%rax,8)
incq %rax
incq %rcx
jmp 0x3ad3f
movq %rax, %r13
jmp 0x3ad87
xorl %r13d, %r13d
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rax
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpq %rdi, %rcx
jae 0x3ac4c
cmpq %r10, %rbp
jge 0x3ac4c
movq (%rax,%rcx,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpq %r8, %rsi
jne 0x3add7
cmpq %rbp, %rcx
je 0x3add4
movq (%rax,%rbp,8), %rsi
movq %rsi, (%rax,%rcx,8)
movq %rdx, (%rax,%rbp,8)
incq %rbp
incq %rcx
jmp 0x3ada1
leaq 0x1aadf5(%rip), %rdi    # 0x1e5bd8
leaq 0x1a88b9(%rip), %rsi    # 0x1e36a3
leaq 0x1aad24(%rip), %rcx    # 0x1e5b15
movl $0x4765, %edx           # imm = 0x4765
callq 0x2110
leaq 0x1aad07(%rip), %rdi    # 0x1e5b09
leaq 0x1a889a(%rip), %rsi    # 0x1e36a3
leaq 0x1aad05(%rip), %rcx    # 0x1e5b15
movl $0x4755, %edx           # imm = 0x4755
callq 0x2110