// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, 0x50(%rsp)
movq %rcx, %rbx
movq %rdi, 0x38(%rsp)
movq 0x10(%rdi), %r15
leaq (%r15,%r15), %rax
cmpq %rax, 0x8(%r8)
movq %r8, 0x8(%rsp)
jae 0x70265
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(%r8)
movq 0x10(%r8), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
cmpq %rax, 0x10(%rbx)
jae 0x702cf
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, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, (%rbx)
xorl %r13d, %r13d
movq %rbx, 0x20(%rsp)
movq %r13, 0x8(%rbx)
movq %r13, (%r8)
testq %r15, %r15
jle 0x7071d
movq 0x98(%rsp), %rdi
movl %edi, %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, 0x2c(%rsp)
xorl %r14d, %r14d
movq %r15, 0x48(%rsp)
movq 0x38(%rsp), %rax
movq (%rax), %rax
imulq $0xa8, %r14, %rcx
leaq (%rax,%rcx), %r9
movq 0x48(%rax,%rcx), %r11
movl %r13d, 0x28(%rax,%rcx)
movl $0xffffffff, 0x20(%rax,%rcx) # imm = 0xFFFFFFFF
movl %r13d, 0x30(%rax,%rcx)
testq %r11, %r11
je 0x70711
movq %r9, 0x18(%rsp)
jle 0x70564
movq %r14, 0x30(%rsp)
movq $-0x80000000, %r10      # imm = 0x80000000
xorl %r15d, %r15d
xorl %ebx, %ebx
movq $-0x80000000, %r13      # imm = 0x80000000
movq $-0x80000000, %rax      # imm = 0x80000000
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %rbp      # imm = 0x80000000
movq %r11, 0x40(%rsp)
movq %rbx, %r14
movq 0x58(%r9), %rcx
movslq (%rcx,%r15), %rdx
leaq 0x1(%rbp), %rsi
cmpq %rdx, %rsi
jne 0x70397
movslq 0x8(%rcx,%r15), %rdx
incq %rax
cmpq %rdx, %rax
je 0x70535
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x7052a
addq %rdi, %r12
subq %rdi, %rbp
cmpq %r12, %rbp
jl 0x7052a
shlq $0x21, %r12
movq 0x20(%rsp), %rbx
addq 0x8(%rbx), %r12
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
movq %r10, 0x10(%rsp)
jne 0x70407
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movq %r12, (%rdi,%rax,8)
shlq $0x21, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
orq %rax, %rbp
addq 0x8(%rbx), %rbp
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x7046c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movq %rbp, (%rcx,%rax,8)
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x704c6
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%rbx)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movq 0x30(%rsp), %rcx
movl %ecx, 0xc(%rax,%rdx)
movl %r10d, 0x10(%rax,%rdx)
movl %r13d, 0x14(%rax,%rdx)
movl %r10d, (%rax,%rdx)
movq 0x58(%r9), %rcx
shlq $0x5, %r10
movl (%rcx,%r10), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl $0x0, 0x8(%rax,%rdx)
movl 0x18(%rax,%rdx), %ecx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %ecx
addl 0x2c(%rsp), %ecx
movl %ecx, 0x18(%rax,%rdx)
movq 0x98(%rsp), %rdi
movq 0x58(%r9), %rcx
movslq (%rcx,%r15), %r12
movq %r14, %r10
movslq 0xc(%rcx,%r15), %rax
movslq 0x4(%rcx,%r15), %rbp
leaq 0x1(%r14), %rbx
addq $0x20, %r15
movq %r14, %r13
cmpq %rbx, %r11
jne 0x70372
decl %ebx
movq 0x48(%rsp), %r15
xorl %r13d, %r13d
movq 0x30(%rsp), %r14
jmp 0x7057e
movl $0x80000000, %ebx       # imm = 0x80000000
movq $-0x80000000, %rbp      # imm = 0x80000000
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %r10      # imm = 0x80000000
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x70711
addq %rdi, %r12
subq %rdi, %rbp
cmpq %r12, %rbp
jl 0x70711
shlq $0x21, %r12
movq 0x20(%rsp), %rax
addq 0x8(%rax), %r12
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
movq %r10, 0x10(%rsp)
jne 0x705e9
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq %r14, 0x30(%rsp)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movq %r12, (%rdi,%rax,8)
shlq $0x21, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
orq %rax, %rbp
movq 0x20(%rsp), %rax
addq 0x8(%rax), %rbp
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x70653
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movq %rbp, (%rcx,%rax,8)
movq 0x20(%rsp), %r14
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x706ad
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r14)
movq (%r14), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq (%r14), %rax
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movq 0x30(%rsp), %r14
movl %r14d, 0xc(%rax,%rdx)
movl %r10d, 0x10(%rax,%rdx)
movl %ebx, 0x14(%rax,%rdx)
movl %r10d, (%rax,%rdx)
movq 0x58(%r9), %rcx
shlq $0x5, %r10
movl (%rcx,%r10), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl $0x0, 0x8(%rax,%rdx)
movl 0x18(%rax,%rdx), %ecx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %ecx
addl 0x2c(%rsp), %ecx
movl %ecx, 0x18(%rax,%rdx)
movq 0x98(%rsp), %rdi
incq %r14
cmpq %r15, %r14
jne 0x70302
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movq 0x8(%rsp), %r8
movq (%r8), %r14
testq %r14, %r14
jle 0x70832
xorl %ebp, %ebp
leaq (,%r14,8), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x10(%r8), %rdi
movq (%rdi,%r15,8), %rax
movq %rax, %r13
shrq $0x21, %r13
movabsq $0x100000000, %rcx      # imm = 0x100000000
testq %rcx, %rax
jne 0x707bb
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x7079f
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
leaq 0x1(%rbx), %r12
movq (%r8), %rax
movq 0x10(%r8), %rcx
movl (%rcx,%r15,8), %edx
leaq 0x1(%rax), %rsi
movq %rsi, (%r8)
movq %rdx, (%rcx,%rax,8)
jmp 0x707c2
leaq -0x1(%rbx), %r12
incq %r13
cmpq %rbp, %r13
seta %al
cmpq $0x2, %rbx
setge %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x70820
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%r8), %rsi
addq 0x10(%rsp), %rsi
movq (%r8), %rdx
subq %r14, %rdx
subq $0x8, %rsp
movq %rbp, %rcx
movq %r13, %r8
movq %rbx, %r9
pushq 0xa8(%rsp)
pushq 0x60(%rsp)
movq 0x38(%rsp), %rax
pushq (%rax)
callq 0x6e9ca
movq 0x28(%rsp), %r8
addq $0x20, %rsp
addq %r14, %rax
movq %rax, (%r8)
incq %r15
movq %r13, %rbp
movq %r12, %rbx
cmpq %r15, %r14
jne 0x70752
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq