// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
movq %rdi, (%rsp)
movq 0x10(%rdi), %rax
movq %rsi, 0x28(%rsp)
movq (%rsi), %rcx
movq %rcx, 0x20(%rsp)
xorl %edx, %edx
testq %rax, %rax
movq %rax, 0x40(%rsp)
je 0x7872c
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
movq (%rsp), %rcx
movq (%rcx), %r13
movl %edx, 0x28(%r13,%rbx)
movl %edx, 0x30(%r13,%rbx)
movl $0xffffffff, 0x20(%r13,%rbx) # imm = 0xFFFFFFFF
movl %edx, 0xc(%r13,%rbx)
cmpq $0x0, 0x48(%r13,%rbx)
je 0x78717
cmpq %r14, %r15
je 0x78714
leaq (%rbx,%r13), %rbp
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
imulq $0xa8, %r14, %r12
addq %r12, %r13
movl $0xa8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2160
movq (%rsp), %r13
addq (%r13), %r12
movl $0xa8, %edx
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
callq 0x2160
xorl %edx, %edx
movq 0x10(%r13), %rax
incq %r14
incq %r15
addq $0xa8, %rbx
cmpq %rax, %r15
jb 0x78698
jmp 0x7872f
xorl %r14d, %r14d
movq (%rsp), %rax
movq %r14, 0x10(%rax)
movq %rax, %r14
subq $0x20, %rsp
xorl %ebx, %ebx
movq %rbx, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r15
movq %r15, %r8
movq 0x28(%rsp), %r12
movq %r12, %r9
callq 0x6fb5a
addq $0x20, %rsp
movq %rbx, (%r12)
movq %rbx, (%r15)
cmpq %rbx, 0x10(%r14)
je 0x7887c
movq %r12, %r8
xorl %r12d, %r12d
movl $0x2, %r13d
movq (%r14), %rax
imulq $0xa8, %r12, %rcx
leaq (%rax,%rcx), %rbp
movl 0x28(%rax,%rcx), %eax
testq %rax, %rax
je 0x787d3
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 0x787c2
jmp 0x787d5
xorl %ebx, %ebx
movl %ebx, 0x30(%rbp)
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x7880c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
andl $-0x4, %ebx
shlq $0x20, %rbx
orq %r12, %rbx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %rbx, (%rax,%rcx,8)
movq (%r15), %rax
movq 0x10(%r15), %rdi
cmpq 0x8(%r15), %rax
jne 0x78855
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rax
movl 0x10(%rbp), %ecx
shlq $0x20, %rcx
orq %r12, %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rcx, (%rdi,%rax,8)
incq %r12
cmpq 0x10(%r14), %r12
jb 0x78790
movq (%r15), %rax
jmp 0x78882
movq 0x10(%r15), %rdi
xorl %eax, %eax
leaq (%rdi,%rax,8), %rsi
callq 0x45373
movq 0x8(%rsp), %rbx
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45373
movq %rbx, %rdi
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x7895f
movl $0x1, %r12d
xorl %eax, %eax
cmpq %rcx, %r12
je 0x788ce
movq 0x10(%rdi), %rdx
movq (%rdx,%r12,8), %rsi
xorq (%rdx,%rax,8), %rsi
shrq $0x20, %rsi
jne 0x7894d
movq %r12, %rdx
subq %rax, %rdx
cmpq $0x2, %rdx
setb %dl
cmpq %rcx, %r12
setae %sil
orb %dl, %sil
movq %r12, %rdx
jne 0x78950
movq 0x10(%rdi), %rcx
cmpq %rax, %r12
jbe 0x78933
movq (%r14), %rdx
movq %rax, %rsi
movl (%rcx,%rsi,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(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x788f9
leaq (%rcx,%rax,8), %rdi
leaq (%rcx,%r12,8), %rsi
callq 0x45373
movq 0x8(%rsp), %rdi
movq (%rdi), %rcx
movq %r12, %rdx
jmp 0x78950
movq %rax, %rdx
incq %r12
movq %rdx, %rax
cmpq %rcx, %r12
jbe 0x788b7
xorl %ecx, %ecx
movq 0x150(%rsp), %rax
movq %rcx, 0x20(%rax)
movq %rcx, (%rax)
movq 0x10(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%r14), %rax
movq %rax, %rsi
shlq $0x5, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
cmpq %rdx, %rsi
cmovaeq %rdx, %rsi
movq %rsi, 0x48(%rsp)
cmpq %rcx, (%rdi)
je 0x78a70
xorl %eax, %eax
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
cmpq 0x48(%rsp), %rax
ja 0x78a7a
movq (%rsp), %rcx
movq (%rcx), %r15
movq 0x10(%rdi), %rcx
movl (%rcx,%r14,8), %ecx
imulq $0xa8, %rcx, %r13
movl %eax, 0x20(%r15,%r13)
movl 0x10(%r15,%r13), %ecx
leaq (%rcx,%rcx,2), %rdi
shlq $0x3, %rdi
movq 0x158(%rsp), %rax
addq (%rax), %rdi
movq 0x18(%rsp), %rbp
movq (%rbp), %rdx
movq 0x10(%rbp), %rsi
movq %rbx, %r8
callq 0x73640
movl %eax, %r12d
movl 0x10(%r15,%r13), %edi
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %rbx, %r8
callq 0x1c9bba
movl %eax, %ecx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x158(%rsp), %r8
callq 0x73cb9
movq 0x8(%rbx), %rax
movl 0x20(%r15,%r13), %ecx
movl %eax, %edx
subl %ecx, %edx
movl %edx, 0xc(%r15,%r13)
jne 0x78a52
addq %r13, %r15
movl $0xffffffff, 0x20(%r15) # imm = 0xFFFFFFFF
incq %r14
movq 0x8(%rsp), %rdi
cmpq (%rdi), %r14
jb 0x789a8
testq %rax, %rax
jne 0x78a7a
movq (%rsp), %rax
movq 0x10(%rax), %rax
cmpq $0x2, %rax
jb 0x78b77
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq 0x158(%rsp), %r8
movq 0x18(%rsp), %r9
movsd 0x40(%rsp), %xmm0
pushq 0x148(%rsp)
pushq 0x158(%rsp)
pushq 0x170(%rsp)
pushq 0x150(%rsp)
pushq 0x40(%rsp)
callq 0x76a74
movq 0x38(%rsp), %r9
addq $0x30, %rsp
testq %rax, %rax
je 0x78b77
subq $0x20, %rsp
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
callq 0x6fb5a
addq $0x20, %rsp
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x78b77
xorl %r15d, %r15d
movq (%rsp), %rbx
movq 0x150(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%r15,8), %rsi
movl %esi, %ecx
shrq $0x20, %rsi
subq %rsi, %rcx
jbe 0x78b6f
movq (%rbx), %rdx
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %r9
addq %rsi, %r9
movq 0x150(%rsp), %rax
addq 0x18(%rax), %r9
movq %r15, %r8
pushq 0x18(%rsp)
pushq %rcx
callq 0x77df1
addq $0x10, %rsp
movq 0x10(%rbx), %rax
incq %r15
cmpq %rax, %r15
jb 0x78b25
movq (%rsp), %r11
movq 0x40(%rsp), %rdi
movq %rdi, 0x10(%r11)
testq %rdi, %rdi
je 0x78c62
xorl %eax, %eax
movq (%r11), %rdx
imulq $0xa8, %rax, %rsi
leaq (%rdx,%rsi), %rcx
cmpl $-0x1, 0xc(%rdx,%rsi)
je 0x78c33
movl 0x8(%rcx), %edi
subl 0x4(%rcx), %edi
incl %edi
movl %edi, 0x20(%rcx)
movl $0x0, 0x30(%rcx)
cmpl $0x0, 0x28(%rcx)
je 0x78c3c
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 0x78c0e
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 0x78bcb
movl 0x20(%rcx), %edi
movq %rsi, 0x48(%rcx)
movl $0x0, 0xc(%rcx)
cmpq %rdi, %rdx
jbe 0x78c4d
jmp 0x78c74
movl $0x0, 0xc(%rcx)
jmp 0x78c56
movq $0x0, 0x48(%rcx)
movl $0x0, 0xc(%rcx)
xorl %edx, %edx
subl %edx, %edi
movl %edi, 0x28(%rcx)
movq 0x10(%r11), %rdi
incq %rax
cmpq %rdi, %rax
jb 0x78b8f
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bb4e7(%rip), %rdi    # 0x234162
leaq 0x1b8ad1(%rip), %rsi    # 0x231753
leaq 0x1bbcb9(%rip), %rcx    # 0x234942
movl $0x5597, %edx           # imm = 0x5597
callq 0x2110