// File: /workspace/github_cpp_makefile_star2_O2/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 0x62b44
movq 0x58(%r12), %rax
movq %rcx, %rdx
shlq $0x5, %rdx
movslq -0x1c(%rax,%rdx), %rbp
cmpl $0x0, -0x8(%rax,%rdx)
sete %al
jmp 0x62b4b
movl 0x4(%r12), %ebp
movb $0x1, %al
cmpq %r14, %rbp
jbe 0x62b7d
cmpq %r13, %rbp
jbe 0x62bba
testq %rbx, %rbx
je 0x62d74
testb %al, %al
je 0x62c84
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r15d, -0x4(%rax,%rcx)
movl %ebx, -0x8(%rax,%rcx)
jmp 0x62d74
pushq $0x2
popq %rdi
jae 0x62c0f
cmpq 0x50(%r12), %rcx
jne 0x62be2
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rdi, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
pushq $0x2
popq %rdi
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rcx
jmp 0x62be7
testq %rbx, %rbx
je 0x62c71
testb %al, %al
je 0x62cb8
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r15d, -0x4(%rax,%rcx)
movl %ebx, -0x8(%rax,%rcx)
jmp 0x62c7a
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
andq $0x0, 0x18(%rax,%rcx)
movups %xmm0, 0x8(%rax,%rcx)
movl %ebp, (%rax,%rcx)
movl %r14d, 0x4(%rax,%rcx)
movq 0x48(%r12), %rcx
cmpq 0x50(%r12), %rcx
jne 0x62c40
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rdi, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rcx
jmp 0x62c45
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 0x62d74
movq 0x58(%r12), %rax
shlq $0x5, %rcx
movl %r13d, -0x1c(%rax,%rcx)
jmp 0x62d74
cmpq 0x50(%r12), %rcx
jne 0x62cec
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
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 0x62cf1
cmpq 0x50(%r12), %rcx
jne 0x62d35
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
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 0x62d3a
movq 0x58(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r12)
shlq $0x5, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax,%rcx)
andl $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 0x62d74
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