// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0xc16bf
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
ja 0xc1884
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0xc17ab
jmp 0xc16dd
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0xc1746
movq -0x8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xc170b
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x48(%rbp)
jmp 0xc1716
movl $0x2, %eax
movq %rax, -0x48(%rbp)
jmp 0xc1716
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x40(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x40(%rbp), %rax
movl $0x0, 0x18(%rax)
jmp 0xc17ad
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0xc1816
movq -0x8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xc17db
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x50(%rbp)
jmp 0xc17e6
movl $0x2, %eax
movq %rax, -0x50(%rbp)
jmp 0xc17e6
movq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0xc1b81
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0xc1a1b
cmpq $0x0, -0x28(%rbp)
je 0xc19ea
cmpq $0x0, -0x38(%rbp)
je 0xc19a2
jmp 0xc18aa
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0xc1913
movq -0x8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xc18d8
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0xc18e3
movl $0x2, %eax
movq %rax, -0x58(%rbp)
jmp 0xc18e3
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rdx
subq $0x2, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0xc19e8
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xc1a16
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xc1b7f
cmpq $0x0, -0x28(%rbp)
je 0xc1b7d
cmpq $0x0, -0x38(%rbp)
jne 0xc1a6b
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0xc1b7b
jmp 0xc1a6d
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0xc1ad6
movq -0x8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xc1a9b
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0xc1aa6
movl $0x2, %eax
movq %rax, -0x60(%rbp)
jmp 0xc1aa6
movq -0x60(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x2, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x48(%rdx), %rdx
subq $0x2, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0xc1b7d
jmp 0xc1b7f
jmp 0xc1b81
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)