// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: rphase_detect0(overlap_region_alloc*, kv_ul_ov_t*, asg64_v*, asg64_v*, asg64_v*)
uint64_t rphase_detect0(overlap_region_alloc* ol, kv_ul_ov_t *c_idx, asg64_v* idx, asg64_v* buf, asg64_v* suf)
{
    uint64_t srt_n = idx->n, i, k, h, qs, qe, ts, te, rr = 0, ovlp; overlap_region *z, *p;
    int64_t dp, old_dp, beg, end;
    for (i = k = 0, dp = old_dp = 0, beg = 0, end = -1; i < srt_n; ++i) {///[beg, end) but coordinates in idx is [, ]
        ///if idx->a.a[] is qe
        old_dp = dp;
        if ((idx->a[i]>>32)&1) {
            --dp; end = (idx->a[i]>>33)+1;
        }else {
            //meet a new overlap; the overlaps are pushed by the x_pos_s
            ++dp; end = (idx->a[i]>>33);
            kv_push(uint64_t, *idx, ((uint32_t)idx->a[i]));
        }
        // fprintf(stderr, "\n[M::%s::] beg::%ld, end::%ld, old_dp::%ld\n", __func__, beg, end, old_dp);
        if((end > beg) && (old_dp >= 2)) {
            // fprintf(stderr, "\n[M::%s::] beg::%ld, end::%ld, old_dp::%ld\n", __func__, beg, end, old_dp);
            // kv_resize(uint64_t, *buf, ((uint32_t)old_dp)<<1);
            // idx->n = srt_n + gen_region_phase(ol->list, idx->a+srt_n, idx->n-srt_n, beg, end, old_dp, c_idx->a, buf, buf1);   
            idx->n = srt_n + gen_region_phase_robust(ol->list, idx->a+srt_n, idx->n-srt_n, beg, end, old_dp, c_idx->a, buf);
        }
        beg = end;
    }
    idx->n = srt_n; 
    
    suf->n = 0; kv_resize(uint64_t, *suf, (ol->length<<1)); 
    uint64_t *sep = suf->a, *sid = suf->a + ol->length, sid_n; 
    for (k = h = sid_n = 0; k < ol->length; k++) {
        z = &(ol->list[k]); 
        z->overlapLen = (uint32_t)-1; z->non_homopolymer_errors = 0; 
        for (i = 0; i < z->align_length; i++) {
            z->non_homopolymer_errors += z->w_list.a[z->w_list.n+i].clen;
        }
        for (; h < c_idx->n && ovlp_id(c_idx->a[h]) < k; h++);
        if(h < c_idx->n && ovlp_id(c_idx->a[h]) == k) {
            qs = z->w_list.a[ovlp_min_wid(c_idx->a[h])].x_start;
            qe = z->w_list.a[ovlp_max_wid(c_idx->a[h])].x_end+1;
            for (; h < c_idx->n && ovlp_id(c_idx->a[h]) == k; h++) {
                qe = z->w_list.a[ovlp_max_wid(c_idx->a[h])].x_end+1;
            }
            sid[sid_n] = ((qs<<32)|sid_n);
            sep[sid_n] = ((k<<32)|qe);
            sid_n++;
            // kv_push(uint64_t, *suf, ((qs<<32)|h));
        }
    }
    radix_sort_bc64(sid, sid+sid_n);
    for (k = 0; k < sid_n; k++) {
        qs = sid[k]>>32; qe = ((uint32_t)sep[(uint32_t)sid[k]]); 
        z = &(ol->list[sep[(uint32_t)sid[k]]>>32]);
        if(z->non_homopolymer_errors == 0) continue;
        for (i = rr = 0; i < sid_n; i++) {
            ts = sid[i]>>32; te = ((uint32_t)sep[(uint32_t)sid[i]]); 
            p = &(ol->list[sep[(uint32_t)sid[i]]>>32]);
            if(ts >= qe) break;
            if(te <= qs) continue;
            ovlp = ((MIN(qe, te) > MAX(qs, ts))? (MIN(qe, te) - MAX(qs, ts)):0);
            if(ovlp < 512) continue;
            if(z->non_homopolymer_errors > p->non_homopolymer_errors + rphase_thres) continue;
            rr = 1; break;
        }
        if(rr) z->overlapLen = 1;
    }
    return rr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq (%rdx), %r8
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %ebx, %ebx
movq %r8, 0x10(%rsp)
cmpq %rbx, %r8
je 0x46677
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rcx
movq %rcx, %r12
shrq $0x21, %r12
btq $0x20, %rcx
jb 0x46618
movq (%r15), %rdx
cmpq 0x8(%r15), %rdx
jne 0x46605
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r9, %r13
callq 0x2370
movq %r13, %r9
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r15)
movl (%rax,%rbx,8), %ecx
movq (%r15), %rdx
leaq 0x1(%rbp), %r13
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movq %rcx, (%rax,%rdx,8)
jmp 0x4661f
leaq -0x1(%rbp), %r13
incq %r12
cmpq %r9, %r12
seta %cl
cmpq $0x2, %rbp
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x46669
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
leaq (%rax,%r8,8), %rsi
movq (%r15), %rdx
subq %r8, %rdx
movq %r9, %rcx
movq %r12, %r8
movq %rbp, %r9
pushq 0x20(%rsp)
pushq (%r14)
callq 0x3ae1a
movq 0x20(%rsp), %r8
addq $0x10, %rsp
addq %r8, %rax
movq %rax, (%r15)
incq %rbx
movq %r13, %rbp
movq %r12, %r9
jmp 0x465aa
movq %r8, (%r15)
movq 0x18(%rsp), %rbx
andq $0x0, (%rbx)
movq 0x8(%rsp), %rbp
movq 0x10(%rbp), %rdx
leaq (%rdx,%rdx), %rax
cmpq %rax, 0x8(%rbx)
jae 0x466f0
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, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %r15
movq %rax, 0x10(%rbx)
movq 0x10(%rbp), %rdx
jmp 0x466f4
movq 0x10(%rbx), %r15
leaq (%r15,%rdx,8), %r12
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r13d, %r13d
cmpq %rdx, %rax
jae 0x4680b
movq (%rbp), %rsi
imulq $0xa8, %rax, %r8
orl $-0x1, 0x20(%rsi,%r8)
leaq (%rsi,%r8), %rdi
andl $0x0, 0x30(%rsi,%r8)
movl 0x28(%rsi,%r8), %esi
shlq $0x5, %rsi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rsi
je 0x46752
movq 0x48(%rdi), %r10
shlq $0x5, %r10
addq 0x58(%rdi), %r10
addl 0x1c(%r8,%r10), %r9d
movl %r9d, 0x30(%rdi)
addq $0x20, %r8
jmp 0x46732
movq 0x8(%r14), %rsi
cmpq %rsi, %rcx
movq %rsi, %r9
cmovaq %rcx, %r9
imulq $0x1c, %rcx, %r8
leaq 0x10(%r8), %r11
addq $0xc, %r8
cmpq %rsi, %rcx
jae 0x46800
movq (%r14), %r10
movl -0x4(%r10,%r11), %ebx
cmpq %rbx, %rax
jbe 0x4678f
incq %rcx
addq $0x1c, %r11
addq $0x1c, %r8
jmp 0x4676c
jne 0x467c0
movq 0x58(%rdi), %rdi
movl (%r10,%r11), %edx
shlq $0x5, %rdx
movslq (%rdi,%rdx), %rdx
addq %r10, %r8
movq %rcx, %r9
cmpq %rsi, %r9
jae 0x467c8
movl (%r8), %r11d
cmpq %r11, %rax
jne 0x467c5
movq %r9, %rcx
incq %r9
addq $0x1c, %r8
jmp 0x467a7
movq %rcx, %r9
jmp 0x46800
movq %r9, %rsi
imulq $0x1c, %rcx, %rcx
movl 0x14(%r10,%rcx), %ecx
shlq $0x5, %rcx
movslq 0x4(%rdi,%rcx), %rcx
incq %rcx
shlq $0x20, %rdx
orq %r13, %rdx
movq %rdx, (%r12,%r13,8)
movq %rax, %rdx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%r15,%r13,8)
incq %r13
movq 0x10(%rbp), %rdx
movq %rsi, %r9
incq %rax
movq %r9, %rcx
jmp 0x466ff
leaq (%r12,%r13,8), %rsi
movq %r12, %rdi
callq 0x1905d
xorl %eax, %eax
pushq $0x1
popq %rcx
xorl %edx, %edx
cmpq %r13, %rdx
je 0x468cf
movq (%r12,%rdx,8), %rsi
movl %esi, %edi
movq (%r15,%rdi,8), %r10
movq (%rbp), %rdi
movq %r10, %r8
shrq $0x20, %r8
imulq $0xa8, %r8, %r8
movl 0x30(%rdi,%r8), %r9d
testl %r9d, %r9d
je 0x468ba
shrq $0x20, %rsi
movl %r10d, %eax
addq %rdi, %r8
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x468b8
movq (%r12,%r10,8), %r11
movq %r11, %rbx
shrq $0x20, %rbx
cmpq %rax, %rbx
jae 0x468b8
movl %r11d, %r11d
movq (%r15,%r11,8), %r11
movl %r11d, %r14d
cmpl %esi, %r14d
jbe 0x468b3
cmpq %r14, %rax
cmovbq %rax, %r14
cmpq %rbx, %rsi
cmovaq %rsi, %rbx
subq %rbx, %r14
jbe 0x468b3
cmpq $0x200, %r14            # imm = 0x200
jb 0x468b3
shrq $0x20, %r11
imulq $0xa8, %r11, %r11
movl 0x30(%rdi,%r11), %r11d
addl $0x4, %r11d
cmpl %r11d, %r9d
jbe 0x468c2
incq %r10
jmp 0x4685a
xorl %eax, %eax
incq %rdx
jmp 0x4681e
movl $0x1, 0x20(%r8)
movq %rcx, %rax
jmp 0x468ba
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq