// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2100
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2100
movw $0x0, -0x98(%rbp)
movw $0x0, -0x9a(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x8fc96
movq -0x8(%rbp), %rdi
addq $0xa8, %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
leaq -0x96(%rbp), %rdx
leaq -0x94(%rbp), %rcx
callq 0xa1ee0
movl %eax, %eax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movzwl -0x96(%rbp), %eax
cmpl $0x1, %eax
jg 0x8f8fb
movl -0x94(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x94(%rbp), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x8f935
movzwl -0x96(%rbp), %eax
cmpl $0x2, %eax
jne 0x8f917
movl -0x94(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x8f933
movzwl -0x96(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f931
movl -0x94(%rbp), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x8f933
jmp 0x8f935
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0x8f966
movzwl -0x96(%rbp), %eax
cmpl $0x0, %eax
je 0x8f961
movl -0x94(%rbp), %eax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x8f891
movq -0x50(%rbp), %rax
cmpq 0x20(%rbp), %rax
jle 0x8f98f
movzwl -0x96(%rbp), %eax
cmpl $0x0, %eax
je 0x8f98a
movl -0x94(%rbp), %eax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x8fc96
cmpw $0x0, -0x98(%rbp)
jne 0x8fafd
movq 0x18(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x8faa7
movq 0x18(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x8faa7
movzwl -0x96(%rbp), %eax
cmpl $0x1, %eax
jg 0x8fa1f
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x18(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq 0x28(%rbp), %rax
jne 0x8fa1a
movzwl -0x96(%rbp), %eax
cmpl $0x1, %eax
jne 0x8fa09
movq -0x70(%rbp), %rax
movq 0x18(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
jmp 0x8fa11
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movw $0x1, -0x98(%rbp)
jmp 0x8faa5
movzwl -0x96(%rbp), %eax
cmpl $0x2, %eax
jne 0x8fa61
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq 0x28(%rbp), %rax
jne 0x8fa5f
movq -0x70(%rbp), %rax
movq 0x18(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movw $0x1, -0x98(%rbp)
jmp 0x8faa3
movzwl -0x96(%rbp), %eax
cmpl $0x3, %eax
jne 0x8faa1
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq 0x28(%rbp), %rax
jne 0x8fa9f
movq -0x70(%rbp), %rax
movl -0x94(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movw $0x1, -0x98(%rbp)
jmp 0x8faa1
jmp 0x8faa3
jmp 0x8faa5
jmp 0x8faa7
movzwl -0x96(%rbp), %eax
cmpl $0x3, %eax
jne 0x8fafb
movq 0x18(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x8fafb
movq 0x18(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x8fafb
movq -0x48(%rbp), %rax
movl -0x94(%rbp), %ecx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq 0x28(%rbp), %rax
jne 0x8faf9
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movw $0x1, -0x98(%rbp)
jmp 0x8fafb
jmp 0x8fafd
cmpw $0x0, -0x9a(%rbp)
jne 0x8fc77
movq 0x20(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x8fc15
movq 0x20(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x8fc15
movzwl -0x96(%rbp), %eax
cmpl $0x1, %eax
jg 0x8fb8d
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq 0x30(%rbp), %rax
jne 0x8fb88
movzwl -0x96(%rbp), %eax
cmpl $0x1, %eax
jne 0x8fb77
movq -0x70(%rbp), %rax
movq 0x20(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
jmp 0x8fb7f
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movw $0x1, -0x9a(%rbp)
jmp 0x8fc13
movzwl -0x96(%rbp), %eax
cmpl $0x2, %eax
jne 0x8fbcf
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq 0x30(%rbp), %rax
jne 0x8fbcd
movq -0x70(%rbp), %rax
movq 0x20(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movw $0x1, -0x9a(%rbp)
jmp 0x8fc11
movzwl -0x96(%rbp), %eax
cmpl $0x3, %eax
jne 0x8fc0f
movq -0x48(%rbp), %rax
movl -0x94(%rbp), %ecx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq 0x30(%rbp), %rax
jne 0x8fc0d
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movw $0x1, -0x9a(%rbp)
jmp 0x8fc0f
jmp 0x8fc11
jmp 0x8fc13
jmp 0x8fc15
movzwl -0x96(%rbp), %eax
cmpl $0x3, %eax
jne 0x8fc69
movq 0x20(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x8fc69
movq 0x20(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x8fc69
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq 0x30(%rbp), %rax
jne 0x8fc67
movq -0x70(%rbp), %rax
movl -0x94(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movw $0x1, -0x9a(%rbp)
jmp 0x8fc69
cmpw $0x0, -0x9a(%rbp)
je 0x8fc75
jmp 0x8fc96
jmp 0x8fc77
movzwl -0x96(%rbp), %eax
cmpl $0x0, %eax
je 0x8fc91
movl -0x94(%rbp), %eax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x8f891
movq -0x78(%rbp), %rax
subq -0x80(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)