// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %r8, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdx), %r8
testq %r8, %r8
je 0x7927b
xorl %r14d, %r14d
leaq (,%r8,8), %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r8, 0x18(%rsp)
movq 0x10(%r12), %rdi
movq (%rdi,%r14,8), %rax
movq %rax, %rbp
shrq $0x21, %rbp
btq $0x20, %rax
jb 0x7920c
movq 0x8(%r12), %rax
cmpq %rax, (%r12)
jne 0x791ed
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
callq 0x2370
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r12)
leaq 0x1(%r13), %rbx
movq (%r12), %rax
movq 0x10(%r12), %rcx
movl (%rcx,%r14,8), %edx
leaq 0x1(%rax), %rsi
movq %rsi, (%r12)
movq %rdx, (%rcx,%rax,8)
jmp 0x79213
leaq -0x1(%r13), %rbx
incq %rbp
cmpq %r15, %rbp
seta %al
cmpq $0x2, %r13
setge %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x79269
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%r12), %rsi
addq 0x28(%rsp), %rsi
movq (%r12), %rdx
subq %r8, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
pushq 0x30(%rsp)
movq 0x10(%rsp), %rax
pushq (%rax)
callq 0x6c809
movq 0x28(%rsp), %r8
addq $0x10, %rsp
addq %r8, %rax
movq %rax, (%r12)
incq %r14
movq %rbp, %r15
movq %rbx, %r13
cmpq %r14, %r8
jne 0x791a3
movq %r8, (%r12)
movq 0x20(%rsp), %rbx
movq $0x0, (%rbx)
movq 0x10(%rsp), %r14
movq 0x10(%r14), %rax
addq %rax, %rax
cmpq %rax, 0x8(%rbx)
jae 0x792ee
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, 0x10(%rbx)
movq 0x10(%rbx), %r12
movq 0x10(%r14), %rax
leaq (%r12,%rax,8), %r15
testq %rax, %rax
je 0x79478
movq (%r14), %rax
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %rbp
imulq $0xa8, %rcx, %rdi
leaq (%rax,%rdi), %rsi
movl $0xffffffff, 0x20(%rax,%rdi) # imm = 0xFFFFFFFF
movl $0x0, 0x30(%rax,%rdi)
movl 0x28(%rax,%rdi), %edi
testq %rdi, %rdi
je 0x79365
movq 0x58(%rsi), %r9
movq 0x48(%rsi), %r10
movl 0x30(%rsi), %r8d
shlq $0x5, %rdi
shlq $0x5, %r10
addq %r10, %r9
addq $0x1c, %r9
xorl %r10d, %r10d
addl (%r9,%r10), %r8d
addq $0x20, %r10
cmpq %r10, %rdi
jne 0x79354
movl %r8d, 0x30(%rsi)
movq 0x8(%rbp), %rdi
cmpq %rdi, %rdx
setb %r8b
jae 0x79464
movq (%rbp), %r9
leaq (%rdx,%rdx,8), %r10
leaq (%r10,%r10,2), %r10
addq %rdx, %r10
movl 0xc(%r9,%r10), %r11d
cmpq %r11, %rcx
jbe 0x793b6
leaq (%r9,%r10), %r8
addq $0x28, %r8
leaq -0x1(%rdi), %r9
cmpq %rdx, %r9
je 0x79411
movl (%r8), %r10d
addq $0x1c, %r8
incq %rdx
cmpq %r10, %rcx
ja 0x7939b
cmpq %rdi, %rdx
setb %r8b
testb %r8b, %r8b
je 0x79464
movq (%rbp), %r8
leaq (%rdx,%rdx,8), %r9
leaq (%r9,%r9,2), %r9
addq %rdx, %r9
movl 0xc(%r8,%r9), %r10d
cmpq %r10, %rcx
jne 0x79464
cmpq %rdi, %rdx
jae 0x79416
movl 0xc(%r8,%r9), %r10d
cmpq %r10, %rcx
jne 0x79416
leaq -0x1(%rdi), %r10
leaq (%r8,%r9), %r11
addq $0x28, %r11
incq %rdx
cmpq %rdi, %rdx
jae 0x79473
movl (%r11), %ebx
addq $0x1c, %r11
cmpq %rbx, %rcx
je 0x793f7
leaq -0x1(%rdx), %r10
jmp 0x79419
movq %rdi, %rdx
jmp 0x79464
movq %rdx, %r10
addq %r8, %r9
movq 0x58(%rsi), %rsi
movl 0x10(%r9), %edi
shlq $0x5, %rdi
movslq (%rsi,%rdi), %rdi
leaq (%r10,%r10,8), %r9
leaq (%r9,%r9,2), %r9
addq %r10, %r9
movl 0x14(%r8,%r9), %r8d
shlq $0x5, %r8
movslq 0x4(%rsi,%r8), %rsi
incq %rsi
shlq $0x20, %rdi
orq %r13, %rdi
movq %rdi, (%r15,%r13,8)
movq %rcx, %rdi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, (%r12,%r13,8)
incq %r13
incq %rcx
cmpq 0x10(%r14), %rcx
jb 0x79312
jmp 0x7947b
movq %rdi, %rdx
jmp 0x79419
xorl %r13d, %r13d
leaq (%r15,%r13,8), %rsi
movq %r15, %rdi
callq 0x45d47
testq %r13, %r13
je 0x7953f
movq (%r14), %rcx
xorl %edx, %edx
xorl %eax, %eax
movq (%r15,%rdx,8), %rsi
movl %esi, %edi
movq (%r12,%rdi,8), %r9
movq %r9, %rdi
shrq $0x20, %rdi
imulq $0xa8, %rdi, %rdi
movl 0x30(%rcx,%rdi), %r8d
testl %r8d, %r8d
je 0x79523
shrq $0x20, %rsi
movl %r9d, %eax
addq %rcx, %rdi
xorl %r9d, %r9d
movq (%r15,%r9,8), %r10
movq %r10, %r11
shrq $0x20, %r11
cmpq %rax, %r11
jae 0x79521
movl %r10d, %r10d
movq (%r12,%r10,8), %r10
movl %r10d, %ebx
cmpl %esi, %ebx
jbe 0x79519
cmpq %rbx, %rax
cmovbq %rax, %rbx
cmpq %r11, %rsi
cmovaq %rsi, %r11
subq %r11, %rbx
jbe 0x79519
cmpq $0x200, %rbx            # imm = 0x200
jb 0x79519
shrq $0x20, %r10
imulq $0xa8, %r10, %r10
movl 0x30(%rcx,%r10), %r10d
addl $0x4, %r10d
cmpl %r10d, %r8d
jbe 0x79531
incq %r9
cmpq %r9, %r13
jne 0x794c6
xorl %eax, %eax
incq %rdx
cmpq %r13, %rdx
jne 0x79497
jmp 0x79541
movl $0x1, 0x20(%rdi)
movl $0x1, %eax
jmp 0x79523
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq