// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: scan_single_wcigar(bit_extz_t*, long, long, long, long, long, long, long, long, long, long)
int64_t scan_single_wcigar(bit_extz_t *ez, int64_t csi, int64_t cei, int64_t cps, int64_t cpe, int64_t cts, int64_t cte, 
int64_t tar_ps, int64_t tar_pe, int64_t tar_ts, int64_t tar_te)
{
    int64_t ci = csi, pi = cps, ti = cts, ws, we;
    int64_t tot_err[2] = {0}, err[2] = {0}, ts1, te1; uint32_t cl; 
    uint16_t c, sset = 0, eset = 0;
    while (ci < cei) {
        ci = pop_trace(&(ez->cigar), ci, &c, &cl);
        ws = pi; 
        if(c <= 1) {
            pi+=cl; ti+=cl;
        } else if(c == 2) {///more p
            pi+=cl;
        } else if(c == 3) {
            ti+=cl;
        }
        we = pi;
        // if(cps == 4489 && cpe == 16192) {
        //     fprintf(stderr, "[M::%s::ci->%ld] wp::[%ld, %ld), pi::%ld, ti::%ld, c::%u, cl::%u, tot_err::%ld\n", 
        //     __func__, ci, ws, we, pi, ti, c, cl, tot_err[0]); 
        // }
        if(we < tar_ps) {///not we <= ps
            if(c != 0) tot_err[0] += cl;
            continue;
        }
        if(ws > tar_pe) {///not ws >= pe
            if(c != 0) tot_err[0] += cl;
            break;
        }

        if(!sset) {
            if((tar_ps>=ws) && (tar_ps<we)) {
                if(c <= 1) {
                    ts1 = ti - (we - tar_ps); 
                    if(ts1 == tar_ts) {
                        if(c == 1) err[0] = tot_err[0] + (tar_ps - ws); 
                        else err[0] = tot_err[0];
                        sset = 1; 
                    }
                } else if(c == 2) {///more p
                    // ts1 = ti - (we - tar_ps); 
                    ts1 = ti;
                    if(ts1 == tar_ts) {
                        err[0] = tot_err[0] + (tar_ps - ws); 
                        sset = 1; 
                    }
                } else if(c == 3) {
                    ts1 = ti; 
                    if(ts1 == tar_ts) {
                        err[0] = tot_err[0] + cl; 
                        sset = 1; 
                    }
                }
            } 

            if((c == 3) && (tar_ps>=ws) && (tar_ps<=we)) {
                ts1 = ti - cl; 
                if(ts1 == tar_ts) {
                    err[0] = tot_err[0]; sset = 1; 
                }
            }
        }

        if(!eset) {
            if((tar_pe>ws) && (tar_pe<=we)) {
                if(c <= 1) {
                    te1 = ti - (we - tar_pe); 
                    if(te1 == tar_te) {
                        if(c == 1) err[1] = tot_err[0] + (tar_pe - ws);
                        else err[1] = tot_err[0];
                        eset = 1; 
                    }
                } else if(c == 2) {///more p
                    // te1 = ti - (we - tar_pe); 
                    te1 = ti;
                    if(te1 == tar_te) {
                        err[1] = tot_err[0] + (tar_pe - ws); 
                        eset = 1; 
                    }
                } else if(c == 3) {
                    te1 = ti-cl; 
                    if(te1 == tar_te) {
                        err[1] = tot_err[0]; 
                        eset = 1; 
                    }
                }
            }
            if((c == 3) && (tar_pe>=ws) && (tar_pe<=we)) {
                te1 = ti; 
                if(te1 == tar_te) {
                    err[1] = tot_err[0] + cl; 
                    eset = 1; 
                }
            }
            if(eset) break;
        }

        if(c != 0) tot_err[0] += cl;
        // fprintf(stderr, "[M::%s::ci->%ld] tot_err::%ld, c::%u\n", __func__, ci, tot_err[0], c); 
    }
    return err[1] - err[0];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
cmpq %rdx, %rsi
jge 0x7e409
movq %rcx, %r11
movq 0x40(%rsp), %rbx
movq 0xa8(%rdi), %r14
movq 0xb8(%rdi), %r15
movl $0x0, -0x10(%rsp)
xorl %ebp, %ebp
xorl %edi, %edi
xorl %r13d, %r13d
movq %r11, %rcx
movl %esi, %eax
movzwl (%r15,%rax,2), %r10d
movl %r10d, %r12d
shrl $0xe, %r12d
movl %r10d, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
incl %esi
cmpq %rsi, %r14
jbe 0x7e1a4
movzwl (%r15,%rsi,2), %eax
movl %eax, %r11d
shrl $0xe, %r11d
cmpl %r12d, %r11d
jne 0x7e1a2
andl $0x3fff, %eax           # imm = 0x3FFF
addl %eax, %r8d
jmp 0x7e180
movl %esi, %esi
movl %r8d, %r8d
testw %r10w, %r10w
js 0x7e1b6
leaq (%rcx,%r8), %r11
addq %r8, %r9
jmp 0x7e1d2
cmpw $0x3, %r12w
je 0x7e1cc
movzwl %r12w, %eax
cmpl $0x2, %eax
jne 0x7e1cf
leaq (%rcx,%r8), %r11
jmp 0x7e1d2
addq %r8, %r9
movq %rcx, %r11
cmpq %rbx, %r11
jge 0x7e1fd
movzwl %r10w, %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl $0x0, %eax
cmovaeq %r8, %rax
addq %rax, %r13
movq %r11, %rcx
cmpq %rdx, %rsi
jl 0x7e168
jmp 0x7e40d
cmpq %rcx, 0x48(%rsp)
jl 0x7e40d
movq %rdi, -0x8(%rsp)
cmpw $0x0, -0x10(%rsp)
je 0x7e273
movw $0x1, %ax
movl %eax, -0x10(%rsp)
movq 0x48(%rsp), %rdi
movq %rdi, %rax
subq %rcx, %rax
setle %cl
cmpq %rdi, %r11
setl %dil
orb %cl, %dil
movb $0x1, %cl
jne 0x7e38e
testw %r10w, %r10w
js 0x7e2d7
movq 0x48(%rsp), %rdi
addq %r9, %rdi
subq %r11, %rdi
cmpq 0x58(%rsp), %rdi
jne 0x7e38e
xorl %ecx, %ecx
cmpw $0x1, %r12w
cmovneq %rcx, %rax
addq %r13, %rax
movq %rax, %rbp
jmp 0x7e38e
movq %rbx, %rax
subq %rcx, %rax
movq %rax, -0x10(%rsp)
setl %dil
cmpq %rbx, %r11
setle %al
orb %dil, %al
je 0x7e304
movl $0x0, -0x10(%rsp)
movq -0x8(%rsp), %rdi
cmpq %rcx, %rbx
jl 0x7e338
cmpw $0x3, %r12w
jne 0x7e338
movq %r9, %rax
subq %r8, %rax
cmpq 0x50(%rsp), %rax
cmoveq %r13, %rdi
movq %rdi, -0x8(%rsp)
movl $0x1, %eax
movl -0x10(%rsp), %edi
cmovel %eax, %edi
movl %edi, -0x10(%rsp)
jmp 0x7e21d
cmpw $0x3, %r12w
je 0x7e37c
movzwl %r12w, %edi
cmpl $0x2, %edi
jne 0x7e38e
cmpq 0x58(%rsp), %r9
jne 0x7e38e
addq %r13, %rax
xorl %ecx, %ecx
jmp 0x7e26b
testw %r10w, %r10w
js 0x7e342
leaq (%r9,%rbx), %rax
subq %r11, %rax
cmpq 0x50(%rsp), %rax
jne 0x7e36f
cmpw $0x1, %r12w
movl $0x0, %eax
movq -0x10(%rsp), %rdi
cmovneq %rax, %rdi
addq %r13, %rdi
movq %rdi, -0x8(%rsp)
jmp 0x7e215
movq %rdi, -0x8(%rsp)
jmp 0x7e21d
cmpw $0x3, %r12w
je 0x7e3df
movzwl %r12w, %eax
cmpl $0x2, %eax
jne 0x7e36f
cmpq 0x50(%rsp), %r9
jne 0x7e36f
movq -0x10(%rsp), %rax
addq %r13, %rax
movq %rax, -0x8(%rsp)
jmp 0x7e215
movl $0x0, -0x10(%rsp)
jmp 0x7e21d
movq %r9, %rax
subq %r8, %rax
cmpq 0x58(%rsp), %rax
setne %cl
cmoveq %r13, %rbp
cmpq 0x48(%rsp), %r11
setl %al
cmpw $0x3, %r12w
setne %dil
cmpq 0x58(%rsp), %r9
setne %r12b
orb %dil, %r12b
orb %al, %r12b
addq %r13, %r8
testb %r12b, %r12b
cmoveq %r8, %rbp
cmpb $0x1, %r12b
jne 0x7e41e
testb %cl, %cl
movq -0x8(%rsp), %rdi
je 0x7e40d
movzwl %r10w, %eax
cmpl $0x4000, %eax           # imm = 0x4000
cmovaeq %r8, %r13
cmpq %rdx, %rsi
jl 0x7e165
jmp 0x7e40d
cmpq 0x50(%rsp), %r9
movq -0x8(%rsp), %rdi
jne 0x7e3fc
leaq (%r8,%r13), %rdi
movw $0x1, %ax
movl %eax, -0x10(%rsp)
jmp 0x7e29a
movl $0x0, -0x10(%rsp)
jmp 0x7e29a
xorl %edi, %edi
xorl %ebp, %ebp
subq %rdi, %rbp
movq %rbp, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x8(%rsp), %rdi
jmp 0x7e40d