// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x7f772
movq $0x0, -0x8(%rbp)
jmp 0x7fd7a
movq $-0x80000000, -0x58(%rbp) # imm = 0x80000000
movq $-0x80000000, -0x60(%rbp) # imm = 0x80000000
movq $-0x80000000, -0x48(%rbp) # imm = 0x80000000
movq $-0x80000000, -0x50(%rbp) # imm = 0x80000000
movq $-0x80000000, -0x38(%rbp) # imm = 0x80000000
movq $-0x80000000, -0x40(%rbp) # imm = 0x80000000
leaq -0xcc(%rbp), %rdi
xorl %esi, %esi
movl $0x1c, %edx
callq 0x2100
movq $0x0, -0x68(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x7fb2d
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq -0x38(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x7f85d
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq -0x48(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x7f85d
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0xc(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x7fb1a
cmpq $-0x80000000, -0x40(%rbp) # imm = 0x80000000
je 0x7fa9f
movl $0x0, -0xc0(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0xbc(%rbp)
movq -0x58(%rbp), %rax
movl %eax, -0xb8(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0xcc(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0xc8(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xb4(%rbp), %eax
andl $0xc0000000, %eax       # imm = 0xC0000000
orl $0x0, %eax
movl %eax, -0xb4(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jl 0x7f93a
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jg 0x7f92a
jmp 0x7f93a
jmp 0x7f92c
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x7f8ee
cmpq $0x0, -0x70(%rbp)
jge 0x7f949
movq $0x0, -0x70(%rbp)
jmp 0x7f94b
movq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x7fa97
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x78(%rbp), %rax
jl 0x7f99c
movq -0x88(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x7f9a7
movq -0x78(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0x7f9d2
movq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x7f9dd
movq -0x80(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jle 0x7fa12
movq -0xa0(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x7fa1d
xorl %eax, %eax
movq %rax, -0xe8(%rbp)
jmp 0x7fa1d
movq -0xe8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x7fa75
movq -0xb0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
leaq -0xcc(%rbp), %rcx
callq 0x6b720
movq %rax, %rcx
movq -0xf0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x90(%rbp), %rax
jl 0x7fa84
jmp 0x7fa97
jmp 0x7fa86
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x7f94b
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0xc(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x7fb1c
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x7f7c5
cmpq $-0x80000000, -0x40(%rbp) # imm = 0x80000000
je 0x7fd6f
movl $0x0, -0xc0(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0xbc(%rbp)
movq -0x58(%rbp), %rax
movl %eax, -0xb8(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0xcc(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0xc8(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xb4(%rbp), %eax
andl $0xc0000000, %eax       # imm = 0xC0000000
orl $0x0, %eax
movl %eax, -0xb4(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jl 0x7fc0a
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jg 0x7fbfa
jmp 0x7fc0a
jmp 0x7fbfc
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x7fbbe
cmpq $0x0, -0x70(%rbp)
jge 0x7fc19
movq $0x0, -0x70(%rbp)
jmp 0x7fc1b
movq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x7fd67
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x78(%rbp), %rax
jl 0x7fc6c
movq -0x88(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x7fc77
movq -0x78(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0x7fca2
movq -0x90(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x7fcad
movq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jle 0x7fce2
movq -0xa0(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x7fced
xorl %eax, %eax
movq %rax, -0x108(%rbp)
jmp 0x7fced
movq -0x108(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x7fd45
movq -0xb0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
leaq -0xcc(%rbp), %rcx
callq 0x6b720
movq %rax, %rcx
movq -0x110(%rbp), %rax
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x90(%rbp), %rax
jl 0x7fd54
jmp 0x7fd67
jmp 0x7fd56
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x7fc1b
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw (%rax,%rax)