// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x30(%rsp)
movq %rdi, %rbp
movq 0x10(%rdi), %rax
movq %rsi, 0x28(%rsp)
movq (%rsi), %rcx
movq %rcx, 0x18(%rsp)
xorl %ecx, %ecx
movq %rax, 0x40(%rsp)
testq %rax, %rax
movq %rdi, (%rsp)
je 0x78c49
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%rbp), %r13
movl %ecx, 0x28(%r13,%r14)
movl %ecx, 0x30(%r13,%r14)
movl $0xffffffff, 0x20(%r13,%r14) # imm = 0xFFFFFFFF
movl %ecx, 0xc(%r13,%r14)
cmpq $0x0, 0x48(%r13,%r14)
je 0x78c33
cmpq %rbx, %r15
je 0x78c30
leaq (%r14,%r13), %rbp
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
imulq $0xa8, %rbx, %r12
addq %r12, %r13
movl $0xa8, %edx
movq %rbp, %rdi
movq (%rsp), %rbp
movq %r13, %rsi
callq 0x2160
addq (%rbp), %r12
movl $0xa8, %edx
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
callq 0x2160
xorl %ecx, %ecx
incq %rbx
incq %r15
addq $0xa8, %r14
cmpq 0x10(%rbp), %r15
jb 0x78bbb
jmp 0x78c4b
xorl %ebx, %ebx
movq %rbx, 0x10(%rbp)
subq $0x20, %rsp
xorl %ebx, %ebx
movq %rbx, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r14
movq %r14, %r8
movq 0x40(%rsp), %r15
movq %r15, %r9
callq 0x701e1
addq $0x20, %rsp
movq %rbx, (%r15)
movq %rbx, (%r14)
cmpq %rbx, 0x10(%rbp)
je 0x78d85
xorl %r12d, %r12d
movl $0x2, %r13d
movq (%rbp), %rax
imulq $0xa8, %r12, %rcx
leaq (%rax,%rcx), %rbp
movl 0x28(%rax,%rcx), %eax
testq %rax, %rax
je 0x78ce5
movq 0x48(%rbp), %rcx
movq 0x58(%rbp), %rdx
shlq $0x5, %rax
shlq $0x5, %rcx
addq %rdx, %rcx
addq $0x1c, %rcx
xorl %edx, %edx
xorl %ebx, %ebx
movl (%rcx,%rdx), %esi
addq %rsi, %rbx
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x78cd4
jmp 0x78ce7
xorl %ebx, %ebx
movl %ebx, 0x30(%rbp)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x78d13
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
andl $-0x4, %ebx
shlq $0x20, %rbx
orq %r12, %rbx
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rbx, (%rcx,%rax,8)
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x78d58
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl 0x10(%rbp), %eax
shlq $0x20, %rax
orq %r12, %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
incq %r12
movq (%rsp), %rbp
cmpq 0x10(%rbp), %r12
jb 0x78ca1
movq 0x150(%rsp), %rbx
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movq (%r15), %rcx
testq %rcx, %rcx
je 0x78e67
movl $0x1, %r12d
xorl %eax, %eax
cmpq %rcx, %r12
je 0x78dd8
movq 0x10(%r15), %rdx
movq (%rdx,%r12,8), %rsi
xorq (%rdx,%rax,8), %rsi
shrq $0x20, %rsi
jne 0x78e52
movq %r12, %rdx
subq %rax, %rdx
cmpq $0x2, %rdx
setb %dl
cmpq %rcx, %r12
setae %cl
orb %dl, %cl
movq %r12, %rdx
jne 0x78e55
cmpq %rax, %r12
jbe 0x78e3c
movq (%rbp), %rcx
movq 0x10(%r15), %rdx
movq %rax, %rsi
movl (%rdx,%rsi,8), %edi
imulq $0xa8, %rdi, %rdi
movl 0x4(%rcx,%rdi), %r8d
movl 0x14(%rcx,%rdi), %r9d
subl 0x8(%rcx,%rdi), %r8d
addl $-0x2, %r8d
subl 0x18(%rcx,%rdi), %r9d
addl $-0x2, %r9d
cmpl %r9d, %r8d
cmoval %r8d, %r9d
movl %r9d, 0x4(%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x78e02
movq 0x10(%r15), %rcx
leaq (%rcx,%rax,8), %rdi
leaq (%rcx,%r12,8), %rsi
callq 0x45d47
movq %r12, %rdx
jmp 0x78e55
movq %rax, %rdx
incq %r12
movq (%r15), %rcx
movq %rdx, %rax
cmpq %rcx, %r12
jbe 0x78dc1
movq %rbp, %rcx
xorl %eax, %eax
movq %rax, 0x20(%rbx)
movq %rax, (%rbx)
movq 0x8(%rsp), %rbp
movq %rax, 0x8(%rbp)
movq 0x10(%rcx), %rdx
shlq $0x5, %rdx
movl $0xf4240, %ecx          # imm = 0xF4240
cmpq %rcx, %rdx
cmovaeq %rcx, %rdx
movq %rdx, 0x48(%rsp)
cmpq %rax, (%r15)
je 0x78f60
xorl %ebx, %ebx
movq 0x18(%rsp), %r13
movq 0x8(%rbp), %rax
cmpq 0x48(%rsp), %rax
ja 0x78f60
movq (%rsp), %rcx
movq (%rcx), %r14
movq 0x10(%r15), %rcx
movl (%rcx,%rbx,8), %ecx
imulq $0xa8, %rcx, %r15
movl %eax, 0x20(%r14,%r15)
movl 0x10(%r14,%r15), %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x3, %rdi
movq 0x158(%rsp), %rax
addq (%rax), %rdi
movq 0x10(%rsp), %rbp
movq (%rbp), %rdx
movq 0x10(%rbp), %rsi
movq 0x8(%rsp), %r8
callq 0x73b79
movl %eax, %r12d
movl 0x10(%r14,%r15), %edi
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
movq 0x8(%rsp), %rbp
movq %r13, %rsi
movq %rbp, %r8
callq 0x1bfdf5
movl %eax, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq 0x158(%rsp), %r8
callq 0x741fd
movl 0x8(%rbp), %eax
subl 0x20(%r14,%r15), %eax
movl %eax, 0xc(%r14,%r15)
jne 0x78f4f
addq %r15, %r14
movl $0xffffffff, 0x20(%r14) # imm = 0xFFFFFFFF
incq %rbx
movq 0x20(%rsp), %r15
cmpq (%r15), %rbx
jb 0x78ea5
cmpq $0x0, 0x8(%rbp)
movq 0x10(%rsp), %r14
movq 0x150(%rsp), %r12
jne 0x78f83
movq (%rsp), %rax
cmpq $0x2, 0x10(%rax)
jb 0x79050
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r12, %r8
movq %rbp, %r9
movsd 0x40(%rsp), %xmm0
pushq 0x148(%rsp)
pushq 0x158(%rsp)
pushq 0x170(%rsp)
pushq 0x150(%rsp)
pushq %r14
callq 0x77056
addq $0x30, %rsp
testq %rax, %rax
je 0x79050
subq $0x20, %rsp
movq %r12, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rcx
movq %r14, %r8
movq %r15, %r9
callq 0x701e1
addq $0x20, %rsp
cmpq $0x0, 0x10(%rbx)
je 0x79050
xorl %r15d, %r15d
movq (%rsp), %rbx
movq 0x10(%r12), %rax
movq (%rax,%r15,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
subq %rcx, %rax
jbe 0x79047
movq (%rbx), %rdx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %r9
addq %rcx, %r9
addq 0x18(%r12), %r9
movq %r15, %r8
pushq %r14
pushq %rax
callq 0x78368
addq $0x10, %rsp
incq %r15
cmpq 0x10(%rbx), %r15
jb 0x79011
movq (%rsp), %r11
movq 0x40(%rsp), %rax
movq %rax, 0x10(%r11)
testq %rax, %rax
je 0x79126
xorl %eax, %eax
movq (%r11), %rdx
imulq $0xa8, %rax, %rsi
leaq (%rdx,%rsi), %rcx
cmpl $-0x1, 0xc(%rdx,%rsi)
je 0x790f4
movl 0x8(%rcx), %edx
subl 0x4(%rcx), %edx
incl %edx
movl %edx, 0x20(%rcx)
movl $0x0, 0x30(%rcx)
cmpl $0x0, 0x28(%rcx)
je 0x790fd
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
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 0x790e2
addl %r9d, 0x30(%rcx)
movslq 0x4(%r8,%rdi), %r9
movslq (%r8,%rdi), %r8
subq %r8, %r9
addq %r9, %rdx
incq %rsi
movl 0x28(%rcx), %r8d
addq $0x20, %rdi
cmpq %r8, %rsi
jb 0x7909b
jmp 0x79101
movl $0x0, 0xc(%rcx)
jmp 0x79119
xorl %edx, %edx
xorl %esi, %esi
movq %rsi, 0x48(%rcx)
movl $0x0, 0xc(%rcx)
movl 0x20(%rcx), %esi
cmpq %rsi, %rdx
ja 0x79138
subl %edx, %esi
movl %esi, 0x28(%rcx)
incq %rax
cmpq 0x10(%r11), %rax
jb 0x79068
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1aef13(%rip), %rdi    # 0x228052
leaq 0x1ac4fd(%rip), %rsi    # 0x225643
leaq 0x1af6e5(%rip), %rcx    # 0x228832
movl $0x5597, %edx           # imm = 0x5597
callq 0x2110