// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: push_rphase(overlap_region*, unsigned long, unsigned long, unsigned long, unsigned long)
inline void push_rphase(overlap_region *z, uint64_t zs, uint64_t ze, uint64_t zerr, uint64_t is_pri)
{
    uint64_t pe, p_pri; window_list *p; 
    pe = z->x_pos_s; p_pri = 0;
    if(z->w_list.n) {
        pe = z->w_list.a[z->w_list.n-1].x_end;
        p_pri = z->w_list.a[z->w_list.n-1].cidx;
    }

    // fprintf(stderr, "+[M::%s] zs::%lu, ze::%lu, zerr::%lu, pe::%lu, is_pri::%lu, p_pri::%lu, wn::%u\n", 
    // __func__, zs, ze, zerr, pe, is_pri, p_pri, (uint32_t)z->w_list.n);

    if(pe <= zs) {///not overlap with [zs, ze)
        if(pe < zs) {
            kv_pushp(window_list, z->w_list, &p); memset(p, 0, sizeof((*p)));
            p->clen = 0; p->x_start = pe; p->x_end = zs; p->cidx = 0;
        }
        kv_pushp(window_list, z->w_list, &p); memset(p, 0, sizeof((*p)));
        p->clen = zerr; p->x_start = zs; p->x_end = ze; p->cidx = is_pri;
    } else {
        if(pe <= ze) {///prefix-suffix overlap
            if(is_pri) {
                if(p_pri) {
                    kv_pushp(window_list, z->w_list, &p); memset(p, 0, sizeof((*p)));
                    p->clen = zerr; p->cidx = is_pri;
                    p->x_start = zs; p->x_end = ze;
                    z->w_list.a[z->w_list.n-2].x_end = zs;///trim the previous primary window
                } else {
                    p = &(z->w_list.a[z->w_list.n-1]);
                    p->clen = zerr; p->cidx = is_pri;
                    p->x_end = ze;
                }
            } else {///if current is not primary
                p = &(z->w_list.a[z->w_list.n-1]);
                p->x_end = ze;
            }
        } else {//pe > ze; [zs, ze) is contained within [ps, pe)
            if(is_pri) {///is_pri == 0, no need to do anything
                if(!p_pri) {
                    p = &(z->w_list.a[z->w_list.n-1]);
                    p->clen = zerr; p->cidx = is_pri;
                } else {
                    kv_pushp(window_list, z->w_list, &p); memset(p, 0, sizeof((*p)));
                    p->clen = zerr; p->cidx = is_pri; 
                    p->x_start = zs; p->x_end = z->w_list.a[z->w_list.n-2].x_end;
                    z->w_list.a[z->w_list.n-2].x_end = zs;///trim the previous primary window
                }
            }
        }
    }
    // fprintf(stderr, "-[M::%s] zs::%lu, ze::%lu, zerr::%lu, pe::%lu, is_pri::%lu, p_pri::%lu, wn::%u, x::[%d, %d), sec::%u\n", 
    // __func__, zs, ze, zerr, pe, is_pri, p_pri, (uint32_t)z->w_list.n, 
    // z->w_list.a[z->w_list.n-1].x_start, z->w_list.a[z->w_list.n-1].x_end, z->w_list.a[z->w_list.n-1].clen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x981ba
movq 0x58(%r12), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
movslq -0x1c(%rcx,%rdx), %rbp
cmpl $0x0, -0x8(%rcx,%rdx)
sete %cl
jmp 0x981c1
movl 0x4(%r12), %ebp
movb $0x1, %cl
cmpq %r14, %rbp
jbe 0x981f7
cmpq %r13, %rbp
jbe 0x982cc
testq %rbx, %rbx
je 0x983f9
testb %cl, %cl
je 0x98300
movq 0x58(%r12), %rcx
shlq $0x5, %rax
movl %r15d, -0x4(%rcx,%rax)
movl %ebx, -0x8(%rcx,%rax)
jmp 0x983f9
jae 0x9825e
movq 0x50(%r12), %rcx
cmpq %rcx, %rax
jne 0x9822b
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rax
movq 0x58(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rcx,%rax)
movups %xmm0, 0x10(%rcx,%rax)
movl %ebp, (%rcx,%rax)
movl %r14d, 0x4(%rcx,%rax)
movl $0x0, 0x18(%rcx,%rax)
movq 0x50(%r12), %rax
cmpq %rax, 0x48(%r12)
jne 0x98292
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rax
movq 0x58(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movl %r15d, 0x1c(%rcx,%rax)
movl %r14d, (%rcx,%rax)
movl %r13d, 0x4(%rcx,%rax)
movl %ebx, 0x18(%rcx,%rax)
jmp 0x983f9
testq %rbx, %rbx
je 0x982ed
testb %cl, %cl
je 0x9837f
movq 0x58(%r12), %rcx
shlq $0x5, %rax
movl %r15d, -0x4(%rcx,%rax)
movl %ebx, -0x8(%rcx,%rax)
jmp 0x982f6
movq 0x58(%r12), %rcx
shlq $0x5, %rax
movl %r13d, -0x1c(%rcx,%rax)
jmp 0x983f9
movq 0x50(%r12), %rcx
cmpq %rcx, %rax
jne 0x98332
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rax
movq 0x58(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movl %r15d, 0x1c(%rcx,%rax)
movl %ebx, 0x18(%rcx,%rax)
movl %r14d, (%rcx,%rax)
movq 0x48(%r12), %rdx
movq 0x58(%r12), %rsi
shlq $0x5, %rdx
movl -0x3c(%rsi,%rdx), %edi
movl %edi, 0x4(%rcx,%rax)
movl %r14d, -0x3c(%rsi,%rdx)
jmp 0x983f9
movq 0x50(%r12), %rcx
cmpq %rcx, %rax
jne 0x983b1
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rax
movq 0x58(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movl %r15d, 0x1c(%rcx,%rax)
movl %ebx, 0x18(%rcx,%rax)
movl %r14d, (%rcx,%rax)
movl %r13d, 0x4(%rcx,%rax)
movq 0x48(%r12), %rax
movq 0x58(%r12), %rcx
shlq $0x5, %rax
movl %r14d, -0x3c(%rcx,%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq