// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_xerr(overlap_region*, unsigned long*, long)
int64_t cal_xerr(overlap_region *z, uint64_t *a, int64_t a_n)
{
    int64_t i, zwn, q[2], t[2], w[2], a_i, a_z, as, ae, is, ie, os, oe, ovlp, err = 0; ul_ov_t m; 
    zwn = z->w_list.n; if(!zwn) return 0;
    q[0] = q[1] = t[0] = t[1] = w[0] = w[1] = INT32_MIN; memset(&m, 0, sizeof(m));
    for (i = a_i = 0; i < zwn; i++) {
        // fprintf(stderr, "[M::%s]\tw::[%d,\t%d)\n", __func__, z->w_list.a[i].x_start, z->w_list.a[i].x_end+1);
        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) {
                ovlp_id(m) = 0; ///ovlp id
                ovlp_min_wid(m) = w[0]; ///beg id of windows
                ovlp_max_wid(m) = w[1]; ///end id of windows
                ovlp_cur_wid(m) = w[0]; ///cur id of windows
                ovlp_cur_xoff(m) = z->w_list.a[w[0]].x_start; ///cur xpos
                ovlp_cur_coff(m) = 0; ///cur cigar off in cur window
                ovlp_bd(m) = 0;

                is = q[0]; ie = q[1]+1; ///[is, ie)
                // fprintf(stderr, "+[M::%s]\tis::%ld\tie::%ld\n", __func__, is, ie);
                for (a_z = a_i; a_z >= 0; a_z--) {
                    as = a[a_z]>>32; ae = (uint32_t)a[a_z];
                    if(ae <= is) break;
                }
                if(a_z < 0) a_z = 0;
                for (; a_z < a_n; a_z++) {
                    as = a[a_z]>>32; ae = (uint32_t)a[a_z];
                    os = MAX(is, as); oe = MIN(ie, ae);
                    ovlp = ((oe>os)? (oe-os):0);
                    if(ovlp) {
                        // fprintf(stderr, "+[M::%s]\tos::%ld\toe::%ld\n", __func__, os, oe);
                        err = err + extract_sub_cigar_err(z, os, oe, &m);
                    }
                    if(as >= ie) break;
                }
                a_i = a_z;
            }
            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) {
        ovlp_id(m) = 0; ///ovlp id
        ovlp_min_wid(m) = w[0]; ///beg id of windows
        ovlp_max_wid(m) = w[1]; ///end id of windows
        ovlp_cur_wid(m) = w[0]; ///cur id of windows
        ovlp_cur_xoff(m) = z->w_list.a[w[0]].x_start; ///cur xpos
        ovlp_cur_coff(m) = 0; ///cur cigar off in cur window
        ovlp_bd(m) = 0;

        is = q[0]; ie = q[1]+1; 
        // fprintf(stderr, "-[M::%s]\tis::%ld\tie::%ld\n", __func__, is, ie);
        for (a_z = a_i; a_z >= 0; a_z--) {
            as = a[a_z]>>32; ae = (uint32_t)a[a_z];
            if(ae <= is) break;
        }
        if(a_z < 0) a_z = 0;
        for (; a_z < a_n; a_z++) {
            as = a[a_z]>>32; ae = (uint32_t)a[a_z];
            os = MAX(is, as); oe = MIN(ie, ae);
            ovlp = ((oe>os)? (oe-os):0);
            if(ovlp) {
                // if(os == 22758 && oe == 22764) {
                //     fprintf(stderr, "-[M::%s]\tos::%ld\toe::%ld\n", __func__, os, oe);
                // } 
                err = err + extract_sub_cigar_err(z, os, oe, &m);
            }
            if(as >= ie) break;
        }
        a_i = a_z;
    }
    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, 0x8(%rsp)
movq 0x48(%rdi), %r14
testq %r14, %r14
je 0x759c8
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
testq %r14, %r14
jle 0x759c8
movq %rdx, %r8
movq %rsi, %r13
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq $-0x80000000, %rcx      # imm = 0x80000000
xorl %r9d, %r9d
xorl %ebx, %ebx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $-0x80000000, %r10      # imm = 0x80000000
movq $-0x80000000, %r11      # imm = 0x80000000
xorl %edi, %edi
movq %r13, 0x48(%rsp)
movq %r14, 0x40(%rsp)
movq %rdi, %r12
shlq $0x5, %rdi
movq %rdi, (%rsp)
movslq (%rax,%rdi), %rdi
leaq 0x1(%r11), %rbp
cmpq %rdi, %rbp
jne 0x757d1
movq (%rsp), %rdi
addq %rax, %rdi
movslq 0x8(%rdi), %rdi
incq %rsi
cmpq %rdi, %rsi
jne 0x757d1
movq (%rsp), %rdx
jmp 0x758ca
cmpq $-0x80000000, %r10      # imm = 0x80000000
je 0x758bf
movl %r15d, 0x1c(%rsp)
movl %ecx, 0x20(%rsp)
movl %edx, 0x24(%rsp)
movl %ecx, 0x10(%rsp)
shlq $0x5, %rcx
movl (%rax,%rcx), %ecx
movl %ecx, 0x14(%rsp)
movl %r15d, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
testq %rbx, %rbx
js 0x75829
movl (%r13,%rbx,8), %ecx
cmpq %r10, %rcx
jle 0x75829
leaq -0x1(%rbx), %rcx
testq %rbx, %rbx
movq %rcx, %rbx
jg 0x7580c
movq $-0x1, %rbx
testq %rbx, %rbx
cmovleq %r15, %rbx
cmpq %r8, %rbx
jge 0x758bf
movq %r12, 0x38(%rsp)
movq %rbp, 0x50(%rsp)
movq (%r13,%rbx,8), %r15
movl %r15d, %edx
shrq $0x20, %r15
cmpq %r15, %r10
movq %r15, %rsi
cmovgq %r10, %rsi
cmpq %rdx, %rbp
cmovlq %rbp, %rdx
cmpq %rsi, %rdx
jle 0x7589e
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r8, %r13
movq %r9, %rbp
movq %r10, %r14
movq %r11, %r12
callq 0x6b7ed
movq %r12, %r11
movq 0x38(%rsp), %r12
movq %r14, %r10
movq %rbp, %r9
movq 0x50(%rsp), %rbp
movq %r13, %r8
movq 0x48(%rsp), %r13
addq %rax, %r9
cmpq %r11, %r15
jg 0x758ae
incq %rbx
cmpq %rbx, %r8
jne 0x75843
movq %r8, %rbx
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rsp), %r14
xorl %r15d, %r15d
movq (%rsp), %rdx
movslq (%rax,%rdx), %r10
movq %r12, %rcx
movslq 0xc(%rax,%rdx), %rsi
movslq 0x4(%rax,%rdx), %r11
leaq 0x1(%r12), %rdi
movq %r12, %rdx
cmpq %r14, %rdi
jne 0x7579d
cmpq $-0x80000000, %r10      # imm = 0x80000000
je 0x759cb
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
movl %ecx, 0x20(%rsp)
movl %r12d, 0x24(%rsp)
movl %ecx, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movq 0x58(%rdx), %rdx
shlq $0x5, %rcx
movl (%rdx,%rcx), %ecx
movl %ecx, 0x14(%rsp)
movl %eax, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
testq %rbx, %rbx
js 0x75947
movl (%r13,%rbx,8), %eax
cmpq %r10, %rax
jle 0x75947
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x7592a
movq $-0x1, %rbx
xorl %r14d, %r14d
testq %rbx, %rbx
cmovgq %rbx, %r14
cmpq %r8, %r14
jge 0x759cb
leaq 0x1(%r11), %rcx
decq %r8
movq %r10, (%rsp)
movq %rcx, 0x38(%rsp)
movq (%r13,%r14,8), %r15
movl %r15d, %edx
shrq $0x20, %r15
cmpq %r15, %r10
movq %r15, %rsi
cmovgq %r10, %rsi
cmpq %rdx, %rcx
cmovlq %rcx, %rdx
cmpq %rsi, %rdx
jle 0x759b5
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r8, %r12
movq %r9, %rbp
movq %r11, %rbx
callq 0x6b7ed
movq 0x38(%rsp), %rcx
movq %rbx, %r11
movq (%rsp), %r10
movq %rbp, %r9
movq %r12, %r8
addq %rax, %r9
cmpq %r11, %r15
jg 0x759cb
leaq 0x1(%r14), %rax
cmpq %r14, %r8
movq %rax, %r14
jne 0x75966
jmp 0x759cb
xorl %r9d, %r9d
movq %r9, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq