// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: rphase_hl(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, kv_ul_ov_t*, asg64_v*, asg64_v*, asg64_v*, long, long, long, mask_ul_ov_t*, idx_emask_t*, double)
void rphase_hl(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 bd, 
int64_t rlen, mask_ul_ov_t *mk, idx_emask_t *mm, double len_diff)
{
    uint64_t on0 = ol->length, k, i, l, m0, m1, sec, zwn, m, mmov; 
    overlap_region *z, h; ma_ug_t *ug = uref->ug;

    for (k = i = 0; k < ol->length; k++) {
        z = &(ol->list[k]); 
        z->align_length = 0; 
        z->non_homopolymer_errors = 0;
        z->overlapLen = (uint32_t)-1;
        z->x_pos_strand = 0;
        // fprintf(stderr, "i[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\n", 
        // __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        // rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        // ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors);
        if(!(z->w_list.n)) continue;///no base-level; cis
        if(k != i) {
            h = ol->list[k];
            ol->list[k] = ol->list[i];
            ol->list[i] = h;
        }
        i++;
    }
    ol->length = i; ///[ol->length, on0) keeps cis overlaps without the base-level alignment
    
    rphase_rr(ol, uref, uopt, c_idx, idx, buf, ulid, bd, NULL); 
    ///could use idx && buf here
    for (k = idx->n = buf->n = 0; k < ol->length; k++) {
        z = &(ol->list[k]); 
        ///sort overlaps by sec error
        for (i = sec = 0; i < z->align_length; i++) {
            sec += z->w_list.a[z->w_list.n+i].clen;
        }
        z->non_homopolymer_errors = sec; sec = sec - (sec&3);///normalize by 4
        kv_push(uint64_t, *buf, (((uint64_t)sec)<<32)|((uint64_t)k));

        ///sort overlaps by yid for filtering
        kv_push(uint64_t, *idx, (((uint64_t)z->y_id)<<32)|((uint64_t)k));

        // fprintf(stderr, "+[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\tn_sec::%lu\n", 
        // __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        // rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        // ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors, sec);
    }
    radix_sort_bc64(idx->a, idx->a+idx->n); ///for filtering

    radix_sort_bc64(buf->a, buf->a+buf->n);///sort by error; smaller error first
    for (l = 0, k = 1; k <= buf->n; k++) {
        if(k == buf->n || (buf->a[l]>>32) == (buf->a[k]>>32)) {///with equal number of normalized errors
            if(((k - l) > 1) && (k < buf->n)) {
                for (i = l; i < k; i++) {
                    // fprintf(stderr, "[M::%s]\tl::%lu\tk::%lu\tbuf->a[i]>>32::%lu\n", __func__, l, k, buf->a[i]>>32);
                    z = &(ol->list[(uint32_t)buf->a[i]]);
                    m0 = z->x_pos_e+1-z->x_pos_s;
                    m1 = z->y_pos_e+1-z->y_pos_s;
                    sec = MIN(m0, m1); sec = ((uint32_t)-1)-((uint32_t)sec);
                    buf->a[i] = (((uint64_t)sec)<<32)|((uint32_t)buf->a[i]);
                }
                radix_sort_bc64(buf->a+l, buf->a+k);///sort by lenght; longer first
            }
            l = k;
        }
    }
    
    mk->idx.n = mk->srt.n = 0; c_idx->n = 0;
    mmov = 1000000; if(mmov > (ol->length*32)) mmov = (ol->length*32);
    for (k = 0; k < buf->n && c_idx->n <= mmov; k++) {///need to add overlap that directly connected in the graph
        z = &(ol->list[(uint32_t)buf->a[k]]); z->overlapLen = c_idx->n; 

        // fprintf(stderr, "\n-[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\n", 
        // __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        // rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        // ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors);

        m0 = push_emask_flt(&(mm->a[z->y_id]), idx->a, idx->n, z->y_id, c_idx);


        // fprintf(stderr, "-[M::%s]\tm0::%lu\n", __func__, m0);
        // prt_masks(ug, c_idx->a+c_idx->n-m0, m0);


        m1 = gen_src_shared_interval_simple(z->y_id, ug, idx->a, idx->n, c_idx);


        // fprintf(stderr, "-[M::%s]\tm1::%lu\n", __func__, m1);
        // prt_masks(ug, c_idx->a+c_idx->n-m1, m1);


        dedup_src_shared1(ug, c_idx, m0, m1, mm);
        z->x_pos_strand = c_idx->n - z->overlapLen;
        if(!(z->x_pos_strand)) z->overlapLen = (uint32_t)-1;

        // fprintf(stderr, "-[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\tm0::%lu\tm1::%lu\tcan_n::%u\n", 
        // __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        // rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        // ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors, m0, m1, z->x_pos_strand);
        // fprintf(stderr, "-[M::%s]\tcan_n::%u\n", __func__, z->x_pos_strand);
        // if(z->x_pos_strand) prt_masks(ug, c_idx->a+z->overlapLen, z->x_pos_strand);
    }

    ///if no mask overlaps, some overlaps may be still masked using the overlap within the graph
    if(((c_idx->n) || (ol->length > 1)) && mask_ovlps(uref, uopt, ug, ol, mk, c_idx, idx, buf1, mm, len_diff, rlen, bd)) {
        // for (k = 0; k < mk->srt.n; k++) {
        //     fprintf(stderr, "*[M::%s]\tutg%.6u%c\txl::%u\tqerr::%u\t%c\tutg%.6u%c\tyl::%u\tterr::%u\tel::%u\n", 
        //     __func__, 
        //     ol->list[mk->srt.a[k].qn].y_id + 1, "lc"[ug->u.a[ol->list[mk->srt.a[k].qn].y_id].circ], 
        //     ug->u.a[ol->list[mk->srt.a[k].qn].y_id].len, mk->srt.a[k].qs, 
        //     "+-"[mk->srt.a[k].rev],
        //     ol->list[mk->srt.a[k].tn].y_id + 1, "lc"[ug->u.a[ol->list[mk->srt.a[k].tn].y_id].circ], 
        //     ug->u.a[ol->list[mk->srt.a[k].tn].y_id].len, mk->srt.a[k].ts, 
        //     mk->srt.a[k].el);
        // }
        


        rphase_rr(ol, uref, uopt, c_idx, idx, buf, ulid, bd, mk); 
        for (k = 0; k < ol->length; k++) {
            if(((uint32_t)mk->idx.a[k]) > (mk->idx.a[k]>>32)) {///it has masks
                refine_rphase(ug, rlen, ol->list, ol->length, k, mk->srt.a+(mk->idx.a[k]>>32), ((uint32_t)mk->idx.a[k])-(mk->idx.a[k]>>32), idx);
            }
        }
    }
    // assert(on0 <= ol->length);
    ol->length = on0;

    for (k = 0; k < ol->length; k++) {
        z = &(ol->list[k]); 
        if(z->x_pos_strand == (uint32_t)-1) {
        // fprintf(stderr, ">[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\n", 
        //     __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        //     rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        //     ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors);

            z->x_pos_strand = 0; continue;
        }
        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; z->x_pos_strand = 0;
        assert(zwn <= z->overlapLen);///zwn is the length with secondary-best alignment
        z->align_length = z->overlapLen - zwn;
        
        // fprintf(stderr, ">[M::%s]\tutg%.6u%c\txl::%ld\tx::[%u,\t%u)\t%c\tyl::%u\ty::[%u,\t%u)\tsec::%u\tsec_len::%lu\n", 
        // __func__, z->y_id + 1, "lc"[ug->u.a[z->y_id].circ], 
        // rlen, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
        // ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors, zwn);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm0, 0x38(%rsp)
movq %r9, %r12
movq %r8, (%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x30(%rsp)
movq %rdi, %rbx
movq 0x10(%rdi), %rax
movq %rsi, 0x28(%rsp)
movq (%rsi), %rcx
movq %rcx, 0x20(%rsp)
xorl %r14d, %r14d
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq %rax, %rbp
jae 0x460a3
movq (%rbx), %r15
andl $0x0, 0x28(%r15,%r14)
andl $0x0, 0x30(%r15,%r14)
orl $-0x1, 0x20(%r15,%r14)
andl $0x0, 0xc(%r15,%r14)
cmpq $0x0, 0x48(%r15,%r14)
je 0x46094
cmpq %r13, %rbp
je 0x46091
leaq (%r15,%r14), %rsi
movq %rsi, 0x18(%rsp)
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
callq 0x2160
movq %r13, 0x10(%rsp)
imulq $0xa8, 0x10(%rsp), %r13
addq %r13, %r15
movl $0xa8, %edx
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq (%rbx), %r13
movl $0xa8, %edx
movq %r13, %rdi
movq 0x10(%rsp), %r13
leaq 0x50(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rax
incq %r13
incq %rbp
addq $0xa8, %r14
jmp 0x46007
movq %r13, 0x10(%rbx)
subq $0x20, %rsp
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movq %rbx, %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r14
movq %r14, %r8
movq %r12, %r9
callq 0x3e4a1
addq $0x20, %rsp
andq $0x0, (%r12)
andq $0x0, (%r14)
xorl %r15d, %r15d
pushq $0x2
popq %r13
cmpq 0x10(%rbx), %r15
jae 0x461cd
movq (%rbx), %rax
imulq $0xa8, %r15, %rcx
leaq (%rax,%rcx), %rbp
movl 0x28(%rax,%rcx), %eax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq %rcx, %rax
je 0x4612d
movq 0x48(%rbp), %rdx
shlq $0x5, %rdx
addq 0x58(%rbp), %rdx
movl 0x1c(%rcx,%rdx), %edx
addq %rdx, %r14
addq $0x20, %rcx
jmp 0x4610f
movl %r14d, 0x30(%rbp)
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0x46166
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
andl $-0x4, %r14d
shlq $0x20, %r14
orq %r15, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %r14, (%rax,%rcx,8)
movq (%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x461b0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movl 0x10(%rbp), %edx
shlq $0x20, %rdx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
incq %r15
jmp 0x460ea
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1905d
movq (%r12), %rax
movq 0x10(%r12), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1905d
pushq $0x1
popq %r14
xorl %eax, %eax
movq (%r12), %rcx
cmpq %rcx, %r14
ja 0x4629d
je 0x46217
movq 0x10(%r12), %rdx
movq (%rdx,%r14,8), %rsi
xorq (%rdx,%rax,8), %rsi
shrq $0x20, %rsi
jne 0x4628f
movq %r14, %rdx
subq %rax, %rdx
cmpq $0x2, %rdx
setb %dl
cmpq %rcx, %r14
setae %cl
orb %dl, %cl
movq %r14, %rcx
jne 0x46292
movq %rax, %rcx
cmpq %r14, %rcx
jae 0x46278
movq (%rbx), %rdx
movq 0x10(%r12), %rsi
movl (%rsi,%rcx,8), %edi
imulq $0xa8, %rdi, %rdi
movl 0x4(%rdx,%rdi), %r8d
movl 0x14(%rdx,%rdi), %r9d
subl 0x8(%rdx,%rdi), %r8d
addl $-0x2, %r8d
subl 0x18(%rdx,%rdi), %r9d
addl $-0x2, %r9d
cmpl %r9d, %r8d
cmoval %r8d, %r9d
movl %r9d, 0x4(%rsi,%rcx,8)
incq %rcx
jmp 0x46234
movq 0x10(%r12), %rcx
leaq (%rcx,%rax,8), %rdi
leaq (%rcx,%r14,8), %rsi
callq 0x1905d
movq %r14, %rcx
jmp 0x46292
movq %rax, %rcx
incq %r14
movq %rcx, %rax
jmp 0x461f5
movq 0x150(%rsp), %rax
andq $0x0, 0x20(%rax)
andq $0x0, (%rax)
movq 0x8(%rsp), %rax
andq $0x0, 0x8(%rax)
movq 0x10(%rbx), %rcx
shlq $0x5, %rcx
movl $0xf4240, %eax          # imm = 0xF4240
cmpq %rax, %rcx
cmovaeq %rax, %rcx
movq %rcx, 0x48(%rsp)
xorl %eax, %eax
xorl %edx, %edx
movq 0x8(%rsp), %r15
cmpq (%r12), %rdx
jae 0x46399
cmpq 0x48(%rsp), %rax
ja 0x463a9
movq (%rbx), %rbp
movq 0x10(%r12), %rcx
movq %rdx, 0x10(%rsp)
movl (%rcx,%rdx,8), %ecx
imulq $0xa8, %rcx, %r14
movl %eax, 0x20(%rbp,%r14)
movl 0x10(%rbp,%r14), %ecx
imulq $0x18, %rcx, %rdi
movq 0x158(%rsp), %rax
addq (%rax), %rdi
movq (%rsp), %r13
movq (%r13), %rdx
movq 0x10(%r13), %rsi
movq %r15, %r8
callq 0x41780
movl %eax, %eax
movq %rax, 0x18(%rsp)
movl 0x10(%rbp,%r14), %edi
movq (%r13), %rcx
movq 0x10(%r13), %rdx
movq 0x20(%rsp), %r13
movq %r13, %rsi
movq %r15, %r8
callq 0x17e06c
movl %eax, %ecx
movq %r13, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x158(%rsp), %r8
callq 0x41cbc
movq 0x8(%r15), %rax
movl 0x20(%rbp,%r14), %ecx
movl %eax, %edx
subl %ecx, %edx
movl %edx, 0xc(%rbp,%r14)
jne 0x4638c
addq %r14, %rbp
orl $-0x1, 0x20(%rbp)
movq 0x10(%rsp), %rdx
incq %rdx
jmp 0x462da
testq %rax, %rax
jne 0x463a9
cmpq $0x2, 0x10(%rbx)
jb 0x46485
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rbx, %rcx
movq 0x158(%rsp), %r8
movq 0x10(%rsp), %r9
movsd 0x40(%rsp), %xmm0
pushq 0x148(%rsp)
pushq 0x158(%rsp)
pushq 0x170(%rsp)
pushq 0x150(%rsp)
pushq 0x28(%rsp)
callq 0x44875
addq $0x30, %rsp
testq %rax, %rax
je 0x46485
subq $0x20, %rsp
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %r12, %r9
callq 0x3e4a1
addq $0x20, %rsp
xorl %r15d, %r15d
cmpq 0x10(%rbx), %r15
jae 0x46485
movq 0x150(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r15,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
subq %rcx, %rax
jbe 0x46480
movq (%rbx), %rdx
imulq $0x1c, %rcx, %r9
movq 0x150(%rsp), %rcx
addq 0x18(%rcx), %r9
movq %r15, %r8
pushq (%rsp)
pushq %rax
callq 0x45a86
popq %rax
popq %rcx
incq %r15
jmp 0x4643e
movq 0x40(%rsp), %rdi
movq %rdi, 0x10(%rbx)
xorl %eax, %eax
cmpq %rdi, %rax
jae 0x46547
movq (%rbx), %rdx
imulq $0xa8, %rax, %rsi
leaq (%rdx,%rsi), %rcx
cmpl $-0x1, 0xc(%rdx,%rsi)
je 0x4653b
movl 0x8(%rcx), %edx
subl 0x4(%rcx), %edx
incl %edx
movl %edx, 0x20(%rcx)
andl $0x0, 0x30(%rcx)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
movl 0x28(%rcx), %r8d
cmpq %r8, %rsi
jae 0x46520
movq 0x48(%rcx), %r8
movq 0x58(%rcx), %r9
shlq $0x5, %r8
leaq (%r9,%rdi), %r10
movups (%r8,%r10), %xmm0
movups 0x10(%r8,%r10), %xmm1
movups %xmm1, 0x10(%r9,%rdi)
movups %xmm0, (%r9,%rdi)
movq 0x58(%rcx), %r8
movl 0x1c(%r8,%rdi), %r9d
testl %r9d, %r9d
je 0x46517
addl %r9d, 0x30(%rcx)
movslq 0x4(%r8,%rdi), %r9
movslq (%r8,%rdi), %r8
subq %r8, %r9
addq %r9, %rdx
incq %rsi
addq $0x20, %rdi
jmp 0x464c7
movq %rsi, 0x48(%rcx)
andl $0x0, 0xc(%rcx)
movl 0x20(%rcx), %esi
cmpq %rsi, %rdx
ja 0x46559
subl %edx, %esi
movl %esi, 0x28(%rcx)
movq 0x10(%rbx), %rdi
jmp 0x4653f
andl $0x0, 0xc(%rcx)
incq %rax
jmp 0x46490
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19fb52(%rip), %rdi    # 0x1e60b2
leaq 0x19d13c(%rip), %rsi    # 0x1e36a3
leaq 0x1a0324(%rip), %rcx    # 0x1e6892
movl $0x5597, %edx           # imm = 0x5597
callq 0x2110