// File: chhylp123[P]hifiasm/Correct.cpp
// Function: reacal_phase(overlap_region_alloc*, kv_ul_ov_t*, asg64_v*, asg64_v*, asg64_v*)
void reacal_phase(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; 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 = rr = 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 = 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;
            if(p->non_homopolymer_errors > z->non_homopolymer_errors) continue;
            if(z->non_homopolymer_errors - p->non_homopolymer_errors <= rphase_thres) {
                z->overlapLen = p->overlapLen = 1; rr++;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x98(%rbp)
movq $-0x1, -0xa0(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x86ce7
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x86b6e
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x86c3f
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x86c0c
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x86bcb
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xc0(%rbp)
jmp 0x86bd9
movl $0x2, %eax
movq %rax, -0xc0(%rbp)
jmp 0x86bd9
movq -0xc0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x86c3f
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jle 0x86cc8
cmpq $0x2, -0x90(%rbp)
jl 0x86cc8
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
subq -0x30(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq -0xa0(%rbp), %r8
movq -0x90(%rbp), %r9
movq -0x10(%rbp), %rax
movq (%rax), %r10
movq -0x20(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x6cf40
movq -0xc8(%rbp), %rcx
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x86afe
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0x86de7
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
shlq %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x86de9
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x87073
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movq -0x78(%rbp), %rax
movl $0x0, 0x30(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x28(%rcx), %ecx
cmpq %rcx, %rax
jae 0x86ebd
movq -0x78(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
movq 0x48(%rcx), %rcx
addq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %ecx
movq -0x78(%rbp), %rax
addl 0x30(%rax), %ecx
movl %ecx, 0x30(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x86e77
jmp 0x86ebf
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0xc9(%rbp)
jae 0x86ef4
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpq -0x40(%rbp), %rax
setb %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x86f00
jmp 0x86f10
jmp 0x86f02
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x86ebf
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x87060
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpq -0x40(%rbp), %rax
jne 0x87060
movq -0x78(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x78(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
movl 0x14(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0xca(%rbp)
jae 0x86fc8
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpq -0x40(%rbp), %rax
sete %al
movb %al, -0xca(%rbp)
movb -0xca(%rbp), %al
testb $0x1, %al
jne 0x86fd4
jmp 0x8700f
movq -0x78(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
movl 0x14(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x86f93
movq -0x50(%rbp), %rdx
shlq $0x20, %rdx
orq -0xb8(%rbp), %rdx
movq -0xb0(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
shlq $0x20, %rdx
orq -0x58(%rbp), %rdx
movq -0xa8(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x87062
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x86e31
movq -0xb0(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2da20
movq $0x0, -0x70(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x8723e
movq -0xb0(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x50(%rbp)
movq -0xa8(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %rdx
movq -0x40(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x87132
jmp 0x8722d
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x8722b
movq -0xb0(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0xa8(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jb 0x871c5
jmp 0x8722b
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
ja 0x871d1
jmp 0x8721a
movq -0x80(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jbe 0x871e3
jmp 0x8721a
movq -0x78(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x80(%rbp), %rcx
subl 0x30(%rcx), %eax
cmpl $0x4, %eax
ja 0x87218
movq -0x80(%rbp), %rax
movl $0x1, 0x20(%rax)
movq -0x78(%rbp), %rax
movl $0x1, 0x20(%rax)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x8721a
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x8713a
jmp 0x8722d
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x870a4
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)