// File: /workspace/github_cpp_makefile_star2_O1/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:
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x75eef
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r8
movq %rsi, %r13
movq %rdi, %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
testq %rax, %rax
jle 0x75ef7
movq %rax, 0x30(%rsp)
movq $-0x80000000, %rax      # imm = 0x80000000
xorl %r9d, %r9d
xorl %ebx, %ebx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %r11      # imm = 0x80000000
xorl %ecx, %ecx
movq %rbp, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq %rcx, %r14
movq 0x58(%rbp), %rcx
movq %r14, %rdi
shlq $0x5, %rdi
movq %rdi, 0x8(%rsp)
movslq (%rcx,%rdi), %rdi
leaq 0x1(%r11), %r15
cmpq %rdi, %r15
jne 0x75deb
movq 0x8(%rsp), %rdi
addq %rcx, %rdi
movslq 0x8(%rdi), %rdi
incq %rsi
cmpq %rdi, %rsi
jne 0x75deb
movq 0x8(%rsp), %rdx
jmp 0x75ed1
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x75ec1
xorl %esi, %esi
movl %esi, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
movl %edx, 0x24(%rsp)
movl %eax, 0x10(%rsp)
shlq $0x5, %rax
movl (%rcx,%rax), %eax
movl %eax, 0x14(%rsp)
movl %esi, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
testq %rbx, %rbx
js 0x75e43
movl (%r13,%rbx,8), %eax
cmpq %r12, %rax
jle 0x75e43
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x75e26
movq $-0x1, %rbx
testq %rbx, %rbx
cmovleq %rsi, %rbx
cmpq %r8, %rbx
jge 0x75ec1
movq %r14, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movq (%r13,%rbx,8), %r14
movl %r14d, %edx
shrq $0x20, %r14
cmpq %r14, %r12
movq %r14, %rsi
cmovgq %r12, %rsi
cmpq %rdx, %r15
cmovlq %r15, %rdx
cmpq %rsi, %rdx
jle 0x75eac
movq %rbp, %rdi
leaq 0x10(%rsp), %rcx
movq %r8, %r13
movq %r9, %r15
movq %r11, %rbp
callq 0x6bc0a
movq %rbp, %r11
movq %r15, %r9
movq 0x38(%rsp), %r15
movq %r13, %r8
movq 0x48(%rsp), %r13
movq 0x50(%rsp), %rbp
addq %rax, %r9
cmpq %r11, %r14
jg 0x75ebc
incq %rbx
cmpq %rbx, %r8
jne 0x75e59
movq %r8, %rbx
movq 0x40(%rsp), %r14
movq 0x58(%rbp), %rcx
movq 0x8(%rsp), %rdx
movslq (%rcx,%rdx), %r12
movq %r14, %rax
movslq 0xc(%rcx,%rdx), %rsi
movslq 0x4(%rcx,%rdx), %r11
leaq 0x1(%r14), %rcx
movq %r14, %rdx
cmpq 0x30(%rsp), %rcx
jne 0x75dad
jmp 0x75f17
xorl %r9d, %r9d
jmp 0x76005
movl $0x80000000, %r14d      # imm = 0x80000000
movq $-0x80000000, %r11      # imm = 0x80000000
movq $-0x80000000, %r12      # imm = 0x80000000
movq $-0x80000000, %rax      # imm = 0x80000000
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpq $-0x80000000, %r12      # imm = 0x80000000
je 0x75ff7
xorl %ecx, %ecx
movl %ecx, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
movl %r14d, 0x24(%rsp)
movl %eax, 0x10(%rsp)
movq 0x58(%rbp), %rdx
shlq $0x5, %rax
movl (%rdx,%rax), %eax
movl %eax, 0x14(%rsp)
movl %ecx, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
testq %rbx, %rbx
js 0x75f74
movl (%r13,%rbx,8), %eax
cmpq %r12, %rax
jle 0x75f74
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x75f57
movq $-0x1, %rbx
xorl %r15d, %r15d
testq %rbx, %rbx
cmovgq %rbx, %r15
cmpq %r8, %r15
jge 0x75ff7
leaq 0x1(%r11), %rcx
decq %r8
movq %r12, 0x8(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq (%r13,%r15,8), %r14
movl %r14d, %edx
shrq $0x20, %r14
cmpq %r14, %r12
movq %r14, %rsi
cmovgq %r12, %rsi
cmpq %rdx, %rcx
cmovlq %rcx, %rdx
cmpq %rsi, %rdx
jle 0x75fe6
movq %rbp, %rdi
leaq 0x10(%rsp), %rcx
movq %r9, %rbx
movq %r11, %r12
callq 0x6bc0a
movq 0x30(%rsp), %rcx
movq %r12, %r11
movq 0x8(%rsp), %r12
movq %rbx, %r9
movq 0x38(%rsp), %r8
addq %rax, %r9
cmpq %r11, %r14
jg 0x75ff7
leaq 0x1(%r15), %rax
cmpq %r15, %r8
movq %rax, %r15
jne 0x75f99
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movq %r9, %rax
retq