// File: /workspace/github_cpp_makefile_star2_O3/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), %rcx
testq %rcx, %rcx
je 0x98d35
movq 0x58(%r12), %rax
movq %rcx, %rdx
shlq $0x5, %rdx
movslq -0x1c(%rax,%rdx), %rbp
cmpl $0x0, -0x8(%rax,%rdx)
sete %al
jmp 0x98d3c
movl 0x4(%r12), %ebp
movb $0x1, %al
cmpq %r14, %rbp
jbe 0x98d6e
cmpq %r13, %rbp
jbe 0x98daa
testq %rbx, %rbx
je 0x98f73
testb %al, %al
je 0x98e7c
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r15d, -0x4(%rax,%rcx)
movl %ebx, -0x8(%rax,%rcx)
jmp 0x98f73
jae 0x98e02
cmpq 0x50(%r12), %rcx
jne 0x98dd2
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), %rcx
jmp 0x98dd7
testq %rbx, %rbx
je 0x98e69
testb %al, %al
je 0x98eb2
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r15d, -0x4(%rax,%rcx)
movl %ebx, -0x8(%rax,%rcx)
jmp 0x98e72
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax,%rcx)
movq $0x0, 0x18(%rax,%rcx)
movl %ebp, (%rax,%rcx)
movl %r14d, 0x4(%rax,%rcx)
movq 0x48(%r12), %rcx
cmpq 0x50(%r12), %rcx
jne 0x98e38
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), %rcx
jmp 0x98e3d
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax,%rcx)
movl %r15d, 0x1c(%rax,%rcx)
movl %r14d, (%rax,%rcx)
movl %r13d, 0x4(%rax,%rcx)
movl %ebx, 0x18(%rax,%rcx)
jmp 0x98f73
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r13d, -0x1c(%rax,%rcx)
jmp 0x98f73
cmpq 0x50(%r12), %rcx
jne 0x98ee8
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), %rcx
jmp 0x98eed
cmpq 0x50(%r12), %rcx
jne 0x98f34
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), %rcx
jmp 0x98f39
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax,%rcx)
movl $0x0, 0x14(%rax,%rcx)
movl %r15d, 0x1c(%rax,%rcx)
movl %ebx, 0x18(%rax,%rcx)
movl %r14d, (%rax,%rcx)
movq 0x48(%r12), %rdx
movq 0x58(%r12), %rsi
shlq $0x5, %rdx
movl -0x3c(%rsi,%rdx), %edi
movl %edi, 0x4(%rax,%rcx)
movl %r14d, -0x3c(%rsi,%rdx)
jmp 0x98f73
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax,%rcx)
movl %r15d, 0x1c(%rax,%rcx)
movl %ebx, 0x18(%rax,%rcx)
movl %r14d, (%rax,%rcx)
movl %r13d, 0x4(%rax,%rcx)
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