// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x28, %rsp
movq %r9, %rbx
movq %rdx, %r14
movq %rdi, %r15
movq 0x68(%rsp), %rbp
addq $0xa8, %r15
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rcx, %r13
cmpq %r14, %rsi
jge 0x4b1a4
movq %r15, %rdi
leaq 0xe(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x53a7d
movzwl 0xe(%rsp), %edx
cmpl $0x1, %edx
ja 0x4afa3
movl 0x8(%rsp), %esi
leaq (%rsi,%r13), %rcx
addq %rsi, %rbx
jmp 0x4afc0
cmpl $0x3, %edx
je 0x4afb6
cmpl $0x2, %edx
jne 0x4afbd
movl 0x8(%rsp), %ecx
addq %r13, %rcx
jmp 0x4afc0
movl 0x8(%rsp), %ecx
addq %rcx, %rbx
movq %r13, %rcx
movl %eax, %esi
cmpq %rbp, %rcx
jge 0x4afdb
testl %edx, %edx
movl 0x8(%rsp), %eax
movl $0x0, %edx
cmovneq %rax, %rdx
addq %rdx, %r12
jmp 0x4af6e
cmpq %r13, 0x70(%rsp)
jl 0x4b1a4
movw $0x1, %r11w
cmpw $0x0, 0x20(%rsp)
je 0x4b051
movq 0x70(%rsp), %r9
movq %r9, %r8
subq %r13, %r8
setle %dil
cmpq %r9, %rcx
setl %r9b
orb %dil, %r9b
movb $0x1, %dil
je 0x4b01b
movq 0x18(%rsp), %rax
jmp 0x4b12f
cmpw $0x1, %dx
movq 0x18(%rsp), %rax
ja 0x4b0aa
movq 0x70(%rsp), %r9
addq %rbx, %r9
subq %rcx, %r9
cmpq 0x80(%rsp), %r9
jne 0x4b12f
xorl %edi, %edi
cmpl $0x1, %edx
cmovneq %rdi, %r8
addq %r12, %r8
jmp 0x4b0c3
movq %rbp, %rdi
subq %r13, %rdi
setl %r8b
cmpq %rbp, %rcx
setle %r9b
orb %r8b, %r9b
je 0x4b0c8
xorl %r11d, %r11d
cmpq %r13, %rbp
setl %dil
cmpl $0x3, %edx
setne %r8b
orb %dil, %r8b
jne 0x4aff3
movl 0x8(%rsp), %edi
movq %rbx, %r8
subq %rdi, %r8
cmpq 0x78(%rsp), %r8
movq 0x10(%rsp), %rdi
cmoveq %r12, %rdi
movq %rdi, 0x10(%rsp)
pushq $0x1
popq %rax
cmovel %eax, %r11d
jmp 0x4aff3
cmpl $0x3, %edx
je 0x4b115
cmpl $0x2, %edx
jne 0x4b12f
cmpq 0x80(%rsp), %rbx
jne 0x4b12f
addq %r12, %r8
xorl %edi, %edi
movq %r8, %rax
jmp 0x4b12f
cmpw $0x1, %dx
ja 0x4b0eb
leaq (%rbx,%rbp), %r8
subq %rcx, %r8
cmpq 0x78(%rsp), %r8
jne 0x4b10d
cmpl $0x1, %edx
movl $0x0, %r8d
cmovneq %r8, %rdi
jmp 0x4b100
cmpl $0x3, %edx
je 0x4b183
cmpl $0x2, %edx
jne 0x4b10d
cmpq 0x78(%rsp), %rbx
jne 0x4b10d
addq %r12, %rdi
movq %rdi, 0x10(%rsp)
jmp 0x4aff3
xorl %r11d, %r11d
jmp 0x4aff3
movl 0x8(%rsp), %edi
movq %rbx, %r8
subq %rdi, %r8
cmpq 0x80(%rsp), %r8
setne %dil
cmoveq %r12, %rax
cmpq 0x70(%rsp), %rcx
setl %r8b
cmpl $0x3, %edx
setne %r9b
cmpq 0x80(%rsp), %rbx
setne %r10b
orb %r9b, %r10b
orb %r8b, %r10b
movl 0x8(%rsp), %r8d
addq %r12, %r8
testb %r10b, %r10b
cmoveq %r8, %rax
cmpb $0x1, %r10b
jne 0x4b1a9
testb %dil, %dil
je 0x4b1a9
movq %rax, 0x18(%rsp)
testl %edx, %edx
cmovneq %r8, %r12
movl %r11d, %eax
movq %rax, 0x20(%rsp)
jmp 0x4af6e
cmpq 0x78(%rsp), %rbx
jne 0x4b067
movl 0x8(%rsp), %eax
addq %r12, %rax
movq %rax, 0x10(%rsp)
movw $0x1, %r11w
jmp 0x4b06a
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq