// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: region_phase_adv(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, kv_ul_ov_t*, asg64_v*, asg64_v*, asg64_v*, long)
void region_phase_adv(overlap_region_alloc* ol, const ul_idx_t *uref, const ug_opt_t *uopt, kv_ul_ov_t *c_idx, asg64_v* idx, asg64_v* buf, asg64_v* buf1, int64_t ulid)
{
    int64_t on = ol->length, k, i, zwn, q[2], t[2], w[2]; 
    uint64_t m; overlap_region *z; ul_ov_t *cp;
    kv_resize(uint64_t, *idx, (ol->length<<1)); 
    kv_resize(ul_ov_t, *c_idx, ol->length);
    for (k = idx->n = c_idx->n = 0; k < on; k++) {
        z = &(ol->list[k]); zwn = z->w_list.n; 
        z->align_length = 0; z->overlapLen = (uint32_t)-1;
        z->non_homopolymer_errors = 0;
        if(!zwn) continue;
        q[0] = q[1] = t[0] = t[1] = w[0] = w[1] = INT32_MIN;
        for (i = 0; i < zwn; i++) {
            if((z->w_list.a[i].x_start==(q[1]+1)) && ((z->w_list.a[i].y_start==(t[1]+1)))) {
                q[1] = z->w_list.a[i].x_end;
                t[1] = z->w_list.a[i].y_end;
                w[1] = i;
            } else {
                if(q[0] != INT32_MIN) {
                    m = ((uint64_t)q[0])<<1; m <<= 32; 
                    m += c_idx->n; kv_push(uint64_t, *idx, m);
                    m = (((uint64_t)q[1])<<1)+1; m <<= 32; 
                    m += c_idx->n; kv_push(uint64_t, *idx, m);

                    kv_pushp(ul_ov_t, *c_idx, &cp);
                    ovlp_id(*cp) = k; ///ovlp id
                    ovlp_min_wid(*cp) = w[0]; ///beg id of windows
                    ovlp_max_wid(*cp) = w[1]; ///end id of windows
                    ovlp_cur_wid(*cp) = w[0]; ///cur id of windows
                    ovlp_cur_xoff(*cp) = z->w_list.a[w[0]].x_start; ///cur xpos
                    ovlp_cur_coff(*cp) = 0; ///cur cigar off in cur window
                }

                q[0] = z->w_list.a[i].x_start; q[1] = z->w_list.a[i].x_end;
                t[0] = z->w_list.a[i].y_start; t[1] = z->w_list.a[i].y_end;
                w[0] = i; w[1] = i;
            }
        }
        if(q[0] != INT32_MIN) {
            m = ((uint64_t)q[0])<<1; m <<= 32; 
            m += c_idx->n; kv_push(uint64_t, *idx, m);
            m = (((uint64_t)q[1])<<1)+1; m <<= 32; 
            m += c_idx->n; kv_push(uint64_t, *idx, m);

            kv_pushp(ul_ov_t, *c_idx, &cp);
            ovlp_id(*cp) = k; ///ovlp id
            ovlp_min_wid(*cp) = w[0]; ///beg id of windows
            ovlp_max_wid(*cp) = w[1]; ///end id of windows
            ovlp_cur_wid(*cp) = w[0]; ///cur id of windows
            ovlp_cur_xoff(*cp) = z->w_list.a[w[0]].x_start; ///cur xpos
            ovlp_cur_coff(*cp) = 0; ///cur cigar off in cur window
        }
    }
    radix_sort_bc64(idx->a, idx->a+idx->n);
    //this is used with gen_region_phase, now give up
    //gen_gov_idx(ol, uref, uopt, G_CHAIN_BW, N_GCHAIN_RATE, buf1);
    // for (m = 0; m < c_idx->n; m++) {
    //     fprintf(stderr, "+++[M::%s::utg%.6dl] q[%d, %d), t[%d, %d), wn::%d\n", __func__, 
    //     (int32_t)ol->list[ovlp_id(c_idx->a[m])].y_id+1,
    //     ol->list[ovlp_id(c_idx->a[m])].w_list.a[ovlp_min_wid(c_idx->a[m])].x_start,
    //     ol->list[ovlp_id(c_idx->a[m])].w_list.a[ovlp_max_wid(c_idx->a[m])].x_end+1,
    //     ol->list[ovlp_id(c_idx->a[m])].w_list.a[ovlp_min_wid(c_idx->a[m])].y_start,
    //     ol->list[ovlp_id(c_idx->a[m])].w_list.a[ovlp_max_wid(c_idx->a[m])].y_end+1,
    //     ovlp_max_wid(c_idx->a[m])+1-ovlp_min_wid(c_idx->a[m]));
    // }
    
    if(rphase_detect0(ol, c_idx, idx, buf, buf1)) {//refine phasing
        reacal_phase(ol, c_idx, idx, buf, buf1);
    }

    for (k = 0; k < on; k++) {
        z = &(ol->list[k]); 
        z->overlapLen = z->x_pos_e+1-z->x_pos_s; 
        z->non_homopolymer_errors = 0; zwn = 0;
        for (i = m = 0; i < z->align_length; i++) {
            z->w_list.a[m] = z->w_list.a[z->w_list.n+i];
            if(z->w_list.a[m].clen > 0) {
                z->non_homopolymer_errors += z->w_list.a[m].clen;
                zwn += z->w_list.a[m].x_end-z->w_list.a[m].x_start;
            } 
            m++;
        }
        z->w_list.n = m; 
        assert(zwn <= z->overlapLen);
        z->align_length = z->overlapLen - zwn;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r15
movq %rcx, 0x10(%rsp)
movq %rdi, %rbx
movq 0x10(%rdi), %r14
leaq (%r14,%r14), %rcx
movq %r14, %rax
movq %r8, 0x8(%rsp)
cmpq %rcx, 0x8(%r8)
jae 0x79461
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq 0x8(%rsp), %r12
movq %rcx, 0x8(%r12)
movq 0x10(%r12), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%r12)
movq 0x10(%rbx), %rax
movq 0x10(%rsp), %r12
cmpq %rax, 0x10(%r12)
jae 0x794c6
decq %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, 0x10(%r12)
movq (%r12), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, (%r12)
movq 0x90(%rsp), %rbp
xorl %ecx, %ecx
movq %rcx, 0x8(%r12)
movq 0x8(%rsp), %r12
movq %rcx, (%r12)
testq %r14, %r14
jle 0x79892
xorl %r9d, %r9d
xorl %edi, %edi
movq %rbx, 0x30(%rsp)
movq %r14, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movq (%rbx), %rdx
imulq $0xa8, %rdi, %rax
movq 0x48(%rdx,%rax), %r10
movl %ecx, 0x28(%rdx,%rax)
movl $0xffffffff, 0x20(%rdx,%rax) # imm = 0xFFFFFFFF
movl %ecx, 0x30(%rdx,%rax)
testq %r10, %r10
jle 0x7987d
addq %rax, %rdx
movq %rdx, 0x20(%rsp)
movq 0x58(%rdx), %rcx
movq $-0x80000000, %r8       # imm = 0x80000000
xorl %ebp, %ebp
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %rax      # imm = 0x80000000
movq $-0x80000000, %rbx      # imm = 0x80000000
movq $-0x80000000, %r13      # imm = 0x80000000
xorl %r15d, %r15d
movq %rdi, 0x28(%rsp)
movq %r10, 0x48(%rsp)
movslq (%rcx,%rbp), %rdx
leaq 0x1(%r13), %rsi
cmpq %rdx, %rsi
jne 0x7957f
movslq 0x8(%rcx,%rbp), %rdx
incq %rax
cmpq %rdx, %rax
je 0x796ce
cmpq $-0x80000000, %rbx      # imm = 0x80000000
je 0x796c7
movq 0x8(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
movq %r8, 0x18(%rsp)
jne 0x795d4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r9, 0x50(%rsp)
callq 0x2370
movq 0x50(%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shlq $0x21, %rbx
addq %rbx, %r9
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r9, (%rax,%rcx,8)
shlq $0x21, %r13
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %r13
movq 0x10(%rsp), %rbx
addq 0x8(%rbx), %r13
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x79630
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r13, (%rax,%rcx,8)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x79676
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r8
movq 0x48(%rsp), %r10
leaq 0x1(%rcx), %r9
movq %r9, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %edi, 0xc(%rax,%rdx)
movl %r8d, 0x10(%rax,%rdx)
movl %r12d, 0x14(%rax,%rdx)
movl %r8d, (%rax,%rdx)
movq 0x58(%rsi), %rcx
shlq $0x5, %r8
movl (%rcx,%r8), %esi
movl %esi, 0x4(%rax,%rdx)
movl $0x0, 0x8(%rax,%rdx)
movslq (%rcx,%rbp), %rbx
movq %r15, %r8
movslq 0xc(%rcx,%rbp), %rax
movslq 0x4(%rcx,%rbp), %r13
leaq 0x1(%r15), %r14
addq $0x20, %rbp
movq %r15, %r12
movq %r14, %r15
cmpq %r14, %r10
jne 0x79561
cmpq $-0x80000000, %rbx      # imm = 0x80000000
jne 0x79716
movq 0x30(%rsp), %rbx
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
movq 0x90(%rsp), %rbp
xorl %ecx, %ecx
jmp 0x7987d
movq 0x8(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
movq 0x90(%rsp), %rbp
movq %r8, 0x18(%rsp)
jne 0x79768
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r9, %r15
callq 0x2370
movq %r15, %r9
movq %rax, 0x10(%r12)
movq (%r12), %rcx
shlq $0x21, %r13
movabsq $0x100000000, %rdx      # imm = 0x100000000
orq %rdx, %r13
shlq $0x21, %rbx
addq %rbx, %r9
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %r9, (%rax,%rcx,8)
movq 0x10(%rsp), %rcx
addq 0x8(%rcx), %r13
movq (%r12), %rcx
cmpq 0x8(%r12), %rcx
jne 0x797ca
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
movq 0x30(%rsp), %rbx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %r13, (%rax,%rcx,8)
movq 0x10(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x79821
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r12)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rcx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r15
decq %r14
leaq 0x1(%rcx), %r9
movq %r9, 0x8(%r12)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %edi, 0xc(%rax,%rdx)
movl %r15d, 0x10(%rax,%rdx)
movl %r14d, 0x14(%rax,%rdx)
movl %r15d, (%rax,%rdx)
movq 0x58(%rsi), %rcx
shlq $0x5, %r15
movl (%rcx,%r15), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl $0x0, 0x8(%rax,%rdx)
movq 0x40(%rsp), %r14
xorl %ecx, %ecx
movq 0x38(%rsp), %r15
incq %rdi
cmpq %r14, %rdi
jne 0x794fb
movq 0x8(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x45373
movq %rbx, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x78c93
testq %rax, %rax
je 0x798d2
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x79042
testq %r14, %r14
jle 0x7998f
xorl %eax, %eax
movq (%rbx), %rdx
imulq $0xa8, %rax, %rsi
leaq (%rdx,%rsi), %rcx
movl 0x8(%rdx,%rsi), %edi
subl 0x4(%rdx,%rsi), %edi
incl %edi
movl %edi, 0x20(%rdx,%rsi)
movl $0x0, 0x30(%rdx,%rsi)
cmpl $0x0, 0x28(%rdx,%rsi)
je 0x79974
movq 0x58(%rcx), %rdi
xorl %r8d, %r8d
xorl %esi, %esi
xorl %edx, %edx
movq 0x48(%rcx), %r9
shlq $0x5, %r9
leaq (%rdi,%r8), %r10
movups (%r9,%r10), %xmm0
movups 0x10(%r9,%r10), %xmm1
movups %xmm1, 0x10(%rdi,%r8)
movups %xmm0, (%rdi,%r8)
movq 0x58(%rcx), %rdi
movl 0x1c(%rdi,%r8), %r9d
testl %r9d, %r9d
je 0x79956
addl %r9d, 0x30(%rcx)
movslq 0x4(%rdi,%r8), %r9
movslq (%rdi,%r8), %r10
subq %r10, %r9
addq %r9, %rdx
incq %rsi
movl 0x28(%rcx), %r9d
addq $0x20, %r8
cmpq %r9, %rsi
jb 0x79913
movl 0x20(%rcx), %edi
movq %rsi, 0x48(%rcx)
cmpq %rdi, %rdx
jle 0x7997e
jmp 0x7999e
movq $0x0, 0x48(%rcx)
xorl %edx, %edx
subl %edx, %edi
movl %edi, 0x28(%rcx)
incq %rax
cmpq %r14, %rax
jne 0x798dd
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ba7bd(%rip), %rdi    # 0x234162
leaq 0x1b7da7(%rip), %rsi    # 0x231753
leaq 0x1bb04b(%rip), %rcx    # 0x2349fe
movl $0x5676, %edx           # imm = 0x5676
callq 0x2110