// File: /workspace/github_cpp_makefile_star2_O1/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
testq %rdx, %rdx
je 0x6c5db
movq %r9, %r14
movq %r8, %r13
movq %rcx, %r8
movq %rdx, %rbx
movq %rdi, %r12
movq 0x98(%rsp), %rdx
movq $0x0, (%rdx)
cmpq %r9, 0x8(%rdx)
movq %rsi, 0x30(%rsp)
movq %rcx, 0x18(%rsp)
jae 0x6c392
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(%rdx)
movq 0x10(%rdx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x98(%rsp), %rdx
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %rsi
movq %rax, 0x10(%rdx)
movq %r14, 0x10(%rsp)
movq 0xa0(%rsp), %rdi
cmpq $0x1, %rbx
movq %rbx, 0x50(%rsp)
movq %rbx, %r9
adcq $0x0, %r9
movq 0x90(%rsp), %r10
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
movq (%rsi,%r14,8), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movl 0xc(%r10,%rcx), %eax
movl 0x10(%r10,%rcx), %r11d
imulq $0xa8, %rax, %rax
movq 0x58(%r12,%rax), %rax
shlq $0x5, %r11
movslq (%rax,%r11), %r11
movl 0x14(%r10,%rcx), %ecx
shlq $0x5, %rcx
movslq 0x4(%rax,%rcx), %r15
incq %r15
cmpq %r8, %r11
ja 0x6c488
cmpq %r13, %r15
jb 0x6c488
movq %r12, %rbp
movq %rdi, %r12
movq 0x8(%rdx), %rax
cmpq %rax, (%rdx)
jne 0x6c46c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x3, %rsi
movq %rbx, %r13
movq %r9, %rbx
callq 0x2370
movq 0x98(%rsp), %rdx
movq 0x18(%rsp), %r8
movq 0x90(%rsp), %r10
movq %rbx, %r9
movq %r13, %rbx
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %rsi
movq %rax, 0x10(%rdx)
movq (%rsi,%r14,8), %rax
movq (%rdx), %rcx
movq 0x10(%rdx), %r11
leaq 0x1(%rcx), %rdi
movq %rdi, (%rdx)
movq %rax, (%r11,%rcx,8)
movq %r12, %rdi
movq %rbp, %r12
cmpq %r13, %r15
adcq $0x0, %rbx
incq %r14
cmpq %r14, %r9
jne 0x6c3c1
movq %rbx, 0x48(%rsp)
movq (%rdx), %rbp
cmpq 0x10(%rsp), %rbp
jne 0x6c7ea
testq %rbp, %rbp
je 0x6c766
movq %rbp, 0x28(%rsp)
movq %r9, 0x40(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x10(%rsp)
movq $-0x1, %rbp
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x10(%rdx), %rax
movl (%rax,%r15,8), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rbx
addq %rax, %rbx
leaq (%r10,%rbx), %rcx
movl 0xc(%r10,%rbx), %eax
imulq $0xa8, %rax, %rdi
addq %r12, %rdi
movq %r8, %rsi
movq 0x20(%rsp), %rdx
callq 0x6bc0a
movq %rax, %r14
movq 0x21dacb(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
movq 0x90(%rsp), %rax
movl 0xc(%rax,%rbx), %eax
imulq $0xa8, %rax, %rax
movl 0x10(%r12,%rax), %ecx
incl %ecx
movq %r14, (%rsp)
leaq 0x1bb5f9(%rip), %rsi    # 0x227b36
leaq 0x1bb623(%rip), %rdx    # 0x227b67
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x22b0
testq %r14, %r14
js 0x6c7cb
xorl %eax, %eax
movq 0x10(%rsp), %rcx
cmpq %rcx, %r14
sete %al
addq %rax, %r13
cmpq %rcx, %r14
cmovlq %r14, %rcx
movq %rcx, 0x10(%rsp)
cmovlq %r15, %rbp
movl $0x1, %eax
cmovlq %rax, %r13
shlq $0x20, %r14
movq 0x98(%rsp), %rdx
movq 0x10(%rdx), %rax
orq %r14, (%rax,%r15,8)
incq %r15
cmpq %r15, 0x28(%rsp)
movq 0x90(%rsp), %r10
movq 0x18(%rsp), %r8
jne 0x6c4d8
cmpq $0x1, %r13
jne 0x6c5e2
movl $0x1, %r15d
testq %rbp, %rbp
je 0x6c627
movq 0x10(%rdx), %rax
movq (%rax,%rbp,8), %rcx
movq (%rax), %rsi
movq %rsi, (%rax,%rbp,8)
movq %rcx, (%rax)
jmp 0x6c627
xorl %eax, %eax
jmp 0x6c7bc
jl 0x6c624
movq 0x10(%rdx), %rax
xorl %ecx, %ecx
xorl %r15d, %r15d
movq (%rax,%rcx,8), %rdi
movq %rdi, %rsi
shrq $0x20, %rsi
cmpq 0x10(%rsp), %rsi
jne 0x6c613
cmpq %r15, %rcx
je 0x6c610
movq (%rax,%r15,8), %rsi
movq %rsi, (%rax,%rcx,8)
movq %rdi, (%rax,%r15,8)
incq %r15
incq %rcx
cmpq 0x28(%rsp), %rcx
jae 0x6c627
cmpq %r13, %r15
jl 0x6c5ed
jmp 0x6c627
xorl %r15d, %r15d
movq 0x10(%rdx), %rax
movq 0x28(%rsp), %rbx
cmpq $0x1, %rbx
adcq $0x0, %rbx
xorl %ecx, %ecx
movl (%rax,%rcx,8), %edi
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl 0xc(%r10,%rsi), %esi
movl %esi, (%rax,%rcx,8)
imulq $0xa8, %rsi, %rsi
movl %ecx, 0x20(%r12,%rsi)
incq %rcx
cmpq %rcx, %rbx
jne 0x6c63a
movq %rbx, 0x38(%rsp)
testq %r15, %r15
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %rbp
movq 0xa0(%rsp), %rbx
je 0x6c6c8
xorl %r14d, %r14d
movq 0x10(%rdx), %rax
movl (%rax,%r14,8), %eax
imulq $0xa8, %rax, %r13
addq %r12, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq 0x6c1a5
movq %r13, %rdi
movq 0x20(%rsp), %r13
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x6bffc
movq 0x98(%rsp), %rdx
incq %r14
cmpq %r14, %r15
jne 0x6c683
movq 0x28(%rsp), %rbx
cmpq %rbx, %r15
movq %r13, %r14
movq 0x10(%rsp), %r9
jae 0x6c72b
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 %r9, %rcx
testq %rax, %rax
movl $0x0, %eax
cmovsq %rax, %rcx
movq %rbp, %r13
movq %rbp, %rsi
movq %r14, %rdx
movq %r9, %rbp
callq 0x6bffc
movq %rbp, %r9
movq %r13, %rbp
movq 0x98(%rsp), %rdx
incq %r15
cmpq %r15, %rbx
jne 0x6c6da
movq 0x10(%rdx), %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %r9
movq 0x90(%rsp), %r10
movq %r14, %r13
movq 0x38(%rsp), %rdi
movl (%rax,%rcx,8), %edx
imulq $0xa8, %rdx, %rdx
movl $0xffffffff, 0x20(%r12,%rdx) # imm = 0xFFFFFFFF
incq %rcx
cmpq %rcx, %rdi
jne 0x6c74b
cmpq $0x0, 0x48(%rsp)
movq 0x50(%rsp), %rax
je 0x6c7bc
xorl %edi, %edi
xorl %eax, %eax
movq (%rsi,%rdi,8), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%r10,%rdx), %r8d
movl 0x14(%r10,%rdx), %edx
imulq $0xa8, %r8, %r8
movq 0x58(%r12,%r8), %r8
shlq $0x5, %rdx
movslq 0x4(%r8,%rdx), %rdx
incq %rdx
cmpq %r13, %rdx
jb 0x6c7b4
movq %rcx, (%rsi,%rax,8)
incq %rax
incq %rdi
cmpq %rdi, %r9
jne 0x6c777
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bb3a6(%rip), %rdi    # 0x227b78
leaq 0x1b8e6a(%rip), %rsi    # 0x225643
leaq 0x1bb2d5(%rip), %rcx    # 0x227ab5
movl $0x4765, %edx           # imm = 0x4765
callq 0x2110
leaq 0x1bb2b8(%rip), %rdi    # 0x227aa9
leaq 0x1b8e4b(%rip), %rsi    # 0x225643
leaq 0x1bb2b6(%rip), %rcx    # 0x227ab5
movl $0x4755, %edx           # imm = 0x4755
callq 0x2110