// File: /workspace/github_cpp_makefile_star2_O1/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 0x7e9cf
movq %rcx, %r11
movq 0x40(%rsp), %rbx
movq 0xa8(%rdi), %r14
movq 0xb8(%rdi), %r15
movl $0x0, -0x10(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
xorl %r13d, %r13d
movq %r11, %rcx
movl %esi, %eax
movzwl (%r15,%rax,2), %r10d
movl %r10d, %ebp
shrl $0xe, %ebp
movl %r10d, %eax
andl $0x3fff, %eax           # imm = 0x3FFF
incl %esi
cmpq %rsi, %r14
jbe 0x7e7bb
movzwl (%r15,%rsi,2), %r8d
movl %r8d, %r11d
shrl $0xe, %r11d
cmpl %ebp, %r11d
jne 0x7e7bb
andl $0x3fff, %r8d           # imm = 0x3FFF
addl %r8d, %eax
jmp 0x7e797
movl %eax, %r8d
testw %r10w, %r10w
js 0x7e7cd
leaq (%rcx,%r8), %r11
addq %r8, %r9
jmp 0x7e7e7
cmpw $0x3, %bp
je 0x7e7e1
movzwl %bp, %eax
cmpl $0x2, %eax
jne 0x7e7e4
leaq (%rcx,%r8), %r11
jmp 0x7e7e7
addq %r8, %r9
movq %rcx, %r11
movl %esi, %esi
cmpq %rbx, %r11
jge 0x7e814
movzwl %r10w, %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl $0x0, %eax
cmovaeq %r8, %rax
addq %rax, %r13
movq %r11, %rcx
cmpq %rdx, %rsi
jl 0x7e782
jmp 0x7e9d4
cmpq %rcx, 0x48(%rsp)
jl 0x7e9d4
cmpw $0x0, -0x10(%rsp)
movq %rdi, -0x8(%rsp)
je 0x7e839
movw $0x1, %ax
movl %eax, -0x10(%rsp)
jmp 0x7e90b
movq %rbx, %rax
subq %rcx, %rax
movq %rax, -0x10(%rsp)
setl %dil
cmpq %rbx, %r11
setle %al
orb %dil, %al
je 0x7e862
movl $0x0, -0x10(%rsp)
movq -0x8(%rsp), %rdi
jmp 0x7e8d5
testw %r10w, %r10w
js 0x7e89a
leaq (%r9,%rbx), %rax
subq %r11, %rax
cmpq 0x50(%rsp), %rax
movq -0x8(%rsp), %rdi
jne 0x7e8cd
cmpw $0x1, %bp
movl $0x0, %eax
movq -0x10(%rsp), %rdi
cmovneq %rax, %rdi
addq %r13, %rdi
movw $0x1, %ax
movl %eax, -0x10(%rsp)
jmp 0x7e8d5
cmpw $0x3, %bp
je 0x7e8bb
movzwl %bp, %eax
cmpl $0x2, %eax
movq -0x8(%rsp), %rdi
jne 0x7e8cd
cmpq 0x50(%rsp), %r9
jne 0x7e8cd
movq -0x10(%rsp), %rdi
jmp 0x7e88d
cmpq 0x50(%rsp), %r9
movq -0x8(%rsp), %rdi
jne 0x7e8cd
leaq (%r8,%r13), %rdi
jmp 0x7e890
movl $0x0, -0x10(%rsp)
cmpq %rcx, %rbx
jl 0x7e906
cmpw $0x3, %bp
jne 0x7e906
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 0x7e90b
movq %rdi, -0x8(%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 0x7e97f
testw %r10w, %r10w
js 0x7e951
movq 0x48(%rsp), %rdi
addq %r9, %rdi
subq %r11, %rdi
cmpq 0x58(%rsp), %rdi
jne 0x7e97f
xorl %ecx, %ecx
cmpw $0x1, %bp
cmovneq %rcx, %rax
addq %r13, %rax
movq %rax, %r12
jmp 0x7e97f
cmpw $0x3, %bp
je 0x7e96d
movzwl %bp, %edi
cmpl $0x2, %edi
jne 0x7e97f
cmpq 0x58(%rsp), %r9
jne 0x7e97f
addq %r13, %rax
xorl %ecx, %ecx
jmp 0x7e94c
movq %r9, %rax
subq %r8, %rax
cmpq 0x58(%rsp), %rax
setne %cl
cmoveq %r13, %r12
cmpq 0x48(%rsp), %r11
setl %al
cmpw $0x3, %bp
setne %dil
cmpq 0x58(%rsp), %r9
setne %bpl
orb %dil, %bpl
orb %al, %bpl
addq %r13, %r8
testb %bpl, %bpl
cmoveq %r8, %r12
cmpb $0x1, %bpl
jne 0x7e9e5
testb %cl, %cl
movq -0x8(%rsp), %rdi
je 0x7e9d4
movzwl %r10w, %eax
cmpl $0x4000, %eax           # imm = 0x4000
cmovaeq %r8, %r13
cmpq %rdx, %rsi
jl 0x7e77f
jmp 0x7e9d4
xorl %edi, %edi
xorl %r12d, %r12d
subq %rdi, %r12
movq %r12, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x8(%rsp), %rdi
jmp 0x7e9d4