// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: rphase_rr(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, kv_ul_ov_t*, asg64_v*, asg64_v*, long, long, mask_ul_ov_t*)
void rphase_rr(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, int64_t ulid, int64_t bd, mask_ul_ov_t *mk)
{
    int64_t on = ol->length, k, i, zwn, q[2], t[2], w[2]; 
    uint64_t m; overlap_region *z; ul_ov_t *cp;
    kv_resize(uint64_t, *idx, (ol->length<<1)); 
    kv_resize(ul_ov_t, *c_idx, ol->length);
    for (k = idx->n = c_idx->n = 0; k < on; k++) {
        z = &(ol->list[k]); zwn = z->w_list.n; 
        z->align_length = 0; z->overlapLen = (uint32_t)-1;
        z->non_homopolymer_errors = 0;
        if(!zwn) continue;
        q[0] = q[1] = t[0] = t[1] = w[0] = w[1] = INT32_MIN;
        for (i = 0; i < zwn; i++) {
            if((z->w_list.a[i].x_start==(q[1]+1)) && ((z->w_list.a[i].y_start==(t[1]+1)))) {
                q[1] = z->w_list.a[i].x_end;
                t[1] = z->w_list.a[i].y_end;
                w[1] = i;
            } else {
                if(q[0] != INT32_MIN) {
                    q[0] += bd; q[1] -= bd;
                    if(q[1] >= q[0]) {
                        m = ((uint64_t)q[0])<<1; m <<= 32; 
                        m += c_idx->n; kv_push(uint64_t, *idx, m);
                        m = (((uint64_t)q[1])<<1)+1; m <<= 32; 
                        m += c_idx->n; kv_push(uint64_t, *idx, m);

                        kv_pushp(ul_ov_t, *c_idx, &cp);
                        ovlp_id(*cp) = k; ///ovlp id
                        ovlp_min_wid(*cp) = w[0]; ///beg id of windows
                        ovlp_max_wid(*cp) = w[1]; ///end id of windows
                        ovlp_cur_wid(*cp) = w[0]; ///cur id of windows
                        ovlp_cur_xoff(*cp) = z->w_list.a[w[0]].x_start; ///cur xpos
                        ovlp_cur_coff(*cp) = 0; ///cur cigar off in cur window
                        ovlp_bd(*cp) = bd;
                    }
                }

                q[0] = z->w_list.a[i].x_start; q[1] = z->w_list.a[i].x_end;
                t[0] = z->w_list.a[i].y_start; t[1] = z->w_list.a[i].y_end;
                w[0] = i; w[1] = i;
            }
        }
        if(q[0] != INT32_MIN) {
            q[0] += bd; q[1] -= bd;
            if(q[1] >= q[0]) {
                m = ((uint64_t)q[0])<<1; m <<= 32; 
                m += c_idx->n; kv_push(uint64_t, *idx, m);
                m = (((uint64_t)q[1])<<1)+1; m <<= 32; 
                m += c_idx->n; kv_push(uint64_t, *idx, m);

                kv_pushp(ul_ov_t, *c_idx, &cp);
                ovlp_id(*cp) = k; ///ovlp id
                ovlp_min_wid(*cp) = w[0]; ///beg id of windows
                ovlp_max_wid(*cp) = w[1]; ///end id of windows
                ovlp_cur_wid(*cp) = w[0]; ///cur id of windows
                ovlp_cur_xoff(*cp) = z->w_list.a[w[0]].x_start; ///cur xpos
                ovlp_cur_coff(*cp) = 0; ///cur cigar off in cur window
                ovlp_bd(*cp) = bd;
            }
        }
    }
    radix_sort_bc64(idx->a, idx->a+idx->n);

    int64_t srt_n = idx->n, 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]));
        }
        if((end > beg) && (old_dp >= 2)) {
            idx->n = srt_n + gen_region_phase_robust_rr(ol->list, idx->a+srt_n, idx->n-srt_n, beg, end, old_dp, c_idx->a, buf, mk);
        }
        beg = end;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x38(%rsp)
movq %rcx, %r9
movq %rdi, 0x28(%rsp)
movq 0x10(%rdi), %rax
leaq (%rax,%rax), %rcx
movq %rax, %rbx
cmpq %rcx, 0x8(%r8)
movq %r8, (%rsp)
movq %r9, 0x8(%rsp)
jae 0x3e539
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r8)
movq 0x10(%r8), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x88(%rsp), %rdi
cmpq %rax, 0x10(%r9)
jae 0x3e5a2
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
incq %rcx
movq %rcx, 0x10(%r9)
movq (%r9), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq 0x88(%rsp), %rdi
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, (%r9)
andq $0x0, 0x8(%r9)
andq $0x0, (%r8)
movl %edi, %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, 0x14(%rsp)
xorl %eax, %eax
testq %rbx, %rbx
cmovleq %rax, %rbx
movq %rbx, 0x40(%rsp)
xorl %r10d, %r10d
cmpq 0x40(%rsp), %r10
je 0x3e95b
movq 0x28(%rsp), %rax
movq (%rax), %rax
imulq $0xa8, %r10, %rcx
movq 0x48(%rax,%rcx), %rbp
andl $0x0, 0x28(%rax,%rcx)
leaq (%rax,%rcx), %r11
orl $-0x1, 0x20(%rax,%rcx)
andl $0x0, 0x30(%rax,%rcx)
testq %rbp, %rbp
je 0x3e953
movl $0x0, %eax
cmovleq %rax, %rbp
movq $-0x80000000, %rbx      # imm = 0x80000000
xorl %r14d, %r14d
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rcx      # imm = 0x80000000
movq $-0x80000000, %r15      # imm = 0x80000000
xorl %r13d, %r13d
movq %r10, 0x20(%rsp)
movq %r11, 0x18(%rsp)
cmpq %r13, %rbp
je 0x3e7fb
movq %rcx, 0x30(%rsp)
movq 0x58(%r11), %rcx
movslq (%rcx,%r14), %rax
leaq 0x1(%rbx), %rsi
cmpq %rax, %rsi
jne 0x3e675
movslq 0x8(%rcx,%r14), %rsi
incq %rdx
cmpq %rsi, %rdx
jne 0x3e675
movq %r12, %rax
movq %r15, %rsi
jmp 0x3e7dc
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x3e7d9
addq %rdi, %r12
subq %rdi, %rbx
movq %r13, %rsi
cmpq %r12, %rbx
jl 0x3e7dc
shlq $0x21, %r12
addq 0x8(%r9), %r12
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x3e6d7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r12, (%rax,%rcx,8)
shlq $0x21, %rbx
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %rbx
addq 0x8(%r9), %rbx
movq (%r8), %rcx
cmpq 0x8(%r8), %rcx
jne 0x3e72e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %rbx, (%rax,%rcx,8)
movq (%r9), %rax
movq 0x8(%r9), %rcx
cmpq 0x10(%r9), %rcx
jne 0x3e774
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r9)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, (%r9)
movq 0x8(%r9), %rcx
movq 0x88(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r11
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r9)
imulq $0x1c, %rcx, %rcx
movl %r10d, 0xc(%rax,%rcx)
movl %r15d, 0x10(%rax,%rcx)
movq 0x30(%rsp), %rdx
movl %edx, 0x14(%rax,%rcx)
movl %r15d, (%rax,%rcx)
movq 0x58(%r11), %rdx
shlq $0x5, %r15
movl (%rdx,%r15), %edx
movl %edx, 0x4(%rax,%rcx)
andl $0x0, 0x8(%rax,%rcx)
movl 0x18(%rax,%rcx), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
addl 0x14(%rsp), %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x58(%r11), %rcx
movslq (%rcx,%r14), %rax
movq %r13, %rsi
movslq 0xc(%rcx,%r14), %rdx
movslq 0x4(%rcx,%r14), %rbx
movq %r13, %rcx
incq %r13
addq $0x20, %r14
movq %rax, %r12
movq %rsi, %r15
jmp 0x3e63e
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x3e953
addq %rdi, %r12
subq %rdi, %rbx
cmpq %r12, %rbx
jl 0x3e953
movq %rcx, %r14
shlq $0x21, %r12
addq 0x8(%r9), %r12
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x3e85d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r12, (%rax,%rcx,8)
shlq $0x21, %rbx
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %rbx
addq 0x8(%r9), %rbx
movq (%r8), %rcx
cmpq 0x8(%r8), %rcx
jne 0x3e8b4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %rbx, (%rax,%rcx,8)
movq (%r9), %rax
movq 0x8(%r9), %rcx
cmpq 0x10(%r9), %rcx
jne 0x3e8fa
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r9)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, (%r9)
movq 0x8(%r9), %rcx
movq 0x88(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r9)
imulq $0x1c, %rcx, %rcx
movl %r10d, 0xc(%rax,%rcx)
movl %r15d, 0x10(%rax,%rcx)
movl %r14d, 0x14(%rax,%rcx)
movl %r15d, (%rax,%rcx)
movq 0x58(%rsi), %rdx
shlq $0x5, %r15
movl (%rdx,%r15), %edx
movl %edx, 0x4(%rax,%rcx)
andl $0x0, 0x8(%rax,%rcx)
movl 0x18(%rax,%rcx), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
addl 0x14(%rsp), %edx
movl %edx, 0x18(%rax,%rcx)
incq %r10
jmp 0x3e5c7
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1905d
movq (%rsp), %r8
movq (%r8), %r14
xorl %r15d, %r15d
testq %r14, %r14
movl $0x0, %eax
cmovgq %r14, %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpq %r15, 0x30(%rsp)
je 0x3ea6a
movq 0x10(%r8), %rax
movq (%rax,%r15,8), %rcx
movq %rcx, %r13
shrq $0x21, %r13
movabsq $0x100000000, %rdx      # imm = 0x100000000
testq %rdx, %rcx
jne 0x3e9fc
movq (%r8), %rdx
cmpq 0x8(%r8), %rdx
jne 0x3e9e9
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x10(%r8)
movl (%rax,%r15,8), %ecx
movq (%r8), %rdx
leaq 0x1(%rbp), %r12
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r8)
movq %rcx, (%rax,%rdx,8)
jmp 0x3ea03
leaq -0x1(%rbp), %r12
incq %r13
cmpq %rbx, %r13
seta %cl
cmpq $0x2, %rbp
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3ea5c
movq 0x28(%rsp), %rcx
movq (%rcx), %rdi
leaq (%rax,%r14,8), %rsi
movq (%r8), %rdx
subq %r14, %rdx
subq $0x8, %rsp
movq %rbx, %rcx
movq %r13, %r8
movq %rbp, %r9
pushq 0x98(%rsp)
pushq 0x48(%rsp)
movq 0x20(%rsp), %rax
pushq (%rax)
callq 0x3cc55
movq 0x20(%rsp), %r8
addq $0x20, %rsp
addq %r14, %rax
movq %rax, (%r8)
incq %r15
movq %r12, %rbp
movq %r13, %rbx
jmp 0x3e98a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq