// File: /workspace/github_cpp_makefile_star2_O2/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, (%rsp)
movq 0x48(%rdi), %rdi
testq %rdi, %rdi
je 0x4373d
movq %rdx, %r9
movq %rsi, %r10
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movups %xmm0, 0xc(%rax)
movaps %xmm0, (%rax)
xorl %eax, %eax
testq %rdi, %rdi
cmovleq %rax, %rdi
movq $-0x80000000, %r12      # imm = 0x80000000
xorl %ecx, %ecx
movq $-0x80000000, %rbp      # imm = 0x80000000
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rax      # imm = 0x80000000
xorl %r15d, %r15d
xorl %r11d, %r11d
movq %r9, 0x50(%rsp)
movq %r10, 0x48(%rsp)
movq %rdi, 0x38(%rsp)
cmpq %rdi, %rcx
je 0x436d6
movq %rcx, %r8
movq (%rsp), %rcx
movq 0x58(%rcx), %rcx
movq %r8, 0x40(%rsp)
shlq $0x5, %r8
movq %r8, 0x8(%rsp)
movslq (%rcx,%r8), %rdi
leaq 0x1(%r12), %r13
cmpq %rdi, %r13
jne 0x435d2
movq 0x8(%rsp), %r8
addq %rcx, %r8
movslq 0x8(%r8), %r8
incq %rsi
cmpq %r8, %rsi
jne 0x435d2
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
jmp 0x435eb
cmpq $-0x80000000, %rbp      # imm = 0x80000000
jne 0x435f5
movq %rdi, %rbp
movq 0x40(%rsp), %rdx
movq %rdx, %rax
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %r8
jmp 0x436c3
andl $0x0, 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)
andl $0x0, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
cmpq $-0x1, %r15
pushq $-0x1
popq %rax
cmovlq %r15, %rax
testq %r15, %r15
js 0x4363f
movl (%r10,%r15,8), %ecx
cmpq %rbp, %rcx
jle 0x4363c
decq %r15
jmp 0x43629
movq %r15, %rax
testq %rax, %rax
movl $0x0, %ecx
cmovleq %rcx, %rax
movq %rax, %r15
cmpq %r9, %r15
jge 0x436a5
movq (%r10,%r15,8), %r14
movl %r14d, %edx
shrq $0x20, %r14
cmpq %r14, %rbp
movq %r14, %rsi
cmovgq %rbp, %rsi
cmpq %rdx, %r13
cmovlq %r13, %rdx
cmpq %rsi, %rdx
jle 0x4369b
movq (%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r12, %rbx
movq %r11, %r12
callq 0x3a359
movq %r12, %r11
movq %rbx, %r12
movq 0x48(%rsp), %r10
movq 0x50(%rsp), %r9
addq %rax, %r11
cmpq %r12, %r14
jg 0x436a5
incq %r15
jmp 0x4364e
movq (%rsp), %rax
movq 0x58(%rax), %rcx
movq 0x8(%rsp), %r8
movslq (%rcx,%r8), %rbp
movq 0x40(%rsp), %rdx
movq %rdx, %rax
movq 0x38(%rsp), %rdi
movslq 0xc(%rcx,%r8), %rsi
movslq 0x4(%rcx,%r8), %r12
leaq 0x1(%rdx), %rcx
jmp 0x43582
cmpq $-0x80000000, %rbp      # imm = 0x80000000
je 0x437a4
andl $0x0, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
movl %edx, 0x24(%rsp)
movl %eax, 0x10(%rsp)
movq (%rsp), %rcx
movq 0x58(%rcx), %rcx
shlq $0x5, %rax
movl (%rcx,%rax), %eax
movl %eax, 0x14(%rsp)
andl $0x0, 0x18(%rsp)
andl $0xc0000000, 0x28(%rsp) # imm = 0xC0000000
leaq 0x1(%r12), %rbx
cmpq $-0x1, %r15
pushq $-0x1
popq %rax
cmovlq %r15, %rax
testq %r15, %r15
js 0x43745
movq %rax, %rcx
movl (%r10,%r15,8), %eax
cmpq %rbp, %rax
jle 0x43742
decq %r15
movq %rcx, %rax
jmp 0x43724
xorl %r11d, %r11d
jmp 0x437a4
movq %r15, %rax
xorl %r15d, %r15d
testq %rax, %rax
cmovgq %rax, %r15
cmpq %r9, %r15
jge 0x437a4
movq %r12, %r13
movq (%r10,%r15,8), %r12
movl %r12d, %edx
shrq $0x20, %r12
cmpq %r12, %rbp
movq %r12, %rsi
cmovgq %rbp, %rsi
cmpq %rdx, %rbx
cmovlq %rbx, %rdx
cmpq %rsi, %rdx
jle 0x43799
movq (%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r11, %r14
callq 0x3a359
movq %r14, %r11
movq 0x48(%rsp), %r10
movq 0x50(%rsp), %r9
addq %rax, %r11
incq %r15
cmpq %r13, %r12
movq %r13, %r12
jle 0x4374f
movq %r11, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq