// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: scan_single_wcigar_toff_backward_backup(bit_extz_t*, long, long, long, long, long, long, long, long, long, long)
int64_t scan_single_wcigar_toff_backward_backup(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 = cei-1, pi = cpe, ti = cte, wts, wte;
    int64_t tot_err = 0, err[2] = {0}, ps1, pe1; 
    uint16_t c, sset = 0, eset = 0; uint32_t cl; 
    while (ci >= csi) {
        ci = pop_trace_back(&(ez->cigar), ci, &c, &cl);
        wte = ti; 
        if(c <= 1) {
            pi-=cl; ti-=cl;
        } else if(c == 2) {///more p
            pi-=cl;
        } else if(c == 3) {
            ti-=cl;
        }
        wts = ti;
        // if(tar_ts == 0 && tar_te == 14233 && cts == 0 && cte == 14233) {
        //     fprintf(stderr, "[M::%s::ci->%ld] wt::[%ld, %ld), pi::%ld, ti::%ld, c::%u, cl::%u, tot_err::%ld, err[0]::%ld, err[1]::%ld\n", 
        //     __func__, ci, wts, wte, pi, ti, c, cl, tot_err[0], err[0], err[1]); 
        // }
        if(wte < tar_ts) {///not we <= ps
            if(c != 0) tot_err += cl;
            break;
        }
        if(wts > tar_te) {///not ws >= pe
            if(c != 0) tot_err += cl;
            continue;
        }

        if(!sset) {
            // if(tar_ts == 19030 && tar_te == 31898 && cts == 19030 && cte == 31983) {
            //     fprintf(stderr, "+[M::%s::ci->%ld] wt::[%ld, %ld), tar_t::[%ld, %ld)\n", 
            //     __func__, ci, wts, wte, tar_ts, tar_te); 
            // }
            if((tar_ts>=wts) && (tar_ts<wte)) {
                assert(c != 2);
                if(c <= 1) {
                    ps1 = pi + (tar_ts - wts); 
                    if(ps1 == tar_ps) {
                        if(c == 1) err[0] = tot_err + (wte - tar_ts); 
                        else err[0] = tot_err;
                        sset = 1; 
                    }
                } else if(c == 3) {///more t
                    ps1 = pi; 
                    if(ps1 == tar_ps) {
                        err[0] = tot_err + (wte - tar_ts);
                        sset = 1; 
                    }
                }
            } 

            if((c == 2) && (tar_ts>=wts) && (tar_ts<=wte)) {///more p
                // ps1 = pi; 
                // if(ps1 == tar_ps) {
                //     err[0] = tot_err[0] + cl; sset = 1; 
                // }
                if(tar_ps >= pi && tar_ps < pi + cl) {
                    err[0] = tot_err + (pi + cl - tar_ps);
                    sset = 1; 
                }
            }
            if(sset) break;
        }

        if(!eset) {
            if((tar_te>wts) && (tar_te<=wte)) {
                assert(c != 2);
                if(c <= 1) {
                    pe1 = pi + (tar_te - wts); 
                    // if(tar_ts == 19030 && tar_te == 31898 && cts == 19030 && cte == 31983) {
                    //     fprintf(stderr, "-[M::%s::ci->%ld] wt::[%ld, %ld), tar_t::[%ld, %ld), pe1::%ld, tar_pe::%ld\n", 
                    //     __func__, ci, wts, wte, tar_ts, tar_te, pe1, tar_pe); 
                    // }
                    if(pe1 == tar_pe) {
                        if(c == 1) err[1] = tot_err + (wte - tar_te);
                        else err[1] = tot_err;
                        eset = 1; 
                    }
                } else if(c == 3) {///more t
                    pe1 = pi;
                    if(pe1 == tar_pe) {
                        err[1] = tot_err + (wte - tar_te);
                        eset = 1; 
                    }
                }
            }

            if((c == 2) && (tar_te>=wts) && (tar_te<=wte)) {///more p
                // pe1 = pi; 
                // if(pe1 == tar_pe) {
                //     err[1] = tot_err[0] + cl; eset = 1; 
                // }
                if(tar_pe >= pi && tar_pe < pi + cl) {
                    err[1] = tot_err + (pi + cl - tar_pe);
                    eset = 1; 
                }
            }
        }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x78(%rsp), %rbp
movq 0x60(%rsp), %rcx
decq %rdx
addq $0xa8, %r15
xorl %eax, %eax
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movq %rcx, %r13
movq %rax, %r12
cmpq %r14, %rdx
jl 0x4b3fa
movq %r15, %rdi
movl %edx, %esi
leaq 0x16(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x62f1e
movzwl 0x16(%rsp), %esi
cmpl $0x1, %esi
ja 0x4b23a
movl 0x10(%rsp), %edx
subq %rdx, %rbx
jmp 0x4b254
cmpl $0x3, %esi
je 0x4b250
cmpl $0x2, %esi
jne 0x4b24b
movl 0x10(%rsp), %ecx
subq %rcx, %rbx
movq %r13, %rcx
jmp 0x4b25a
movl 0x10(%rsp), %edx
movq %r13, %rcx
subq %rdx, %rcx
cmpq %rbp, %r13
jl 0x4b3fa
movslq %eax, %rdx
cmpq %rcx, 0x80(%rsp)
jge 0x4b287
testl %esi, %esi
movl 0x10(%rsp), %eax
movl $0x0, %esi
cmovneq %rax, %rsi
addq %rsi, %r12
movq %rcx, %r13
jmp 0x4b20a
movq %rbp, %r8
subq %rcx, %r8
setl %al
movq %r13, %rdi
subq %rbp, %rdi
setle %r9b
orb %al, %r9b
movb $0x1, %al
jne 0x4b2de
cmpl $0x2, %esi
je 0x4b420
cmpw $0x1, %si
ja 0x4b2c8
addq %rbx, %r8
cmpq 0x68(%rsp), %r8
jne 0x4b2de
xorl %eax, %eax
cmpl $0x1, %esi
cmovneq %rax, %rdi
addq %r12, %rdi
jmp 0x4b2d9
cmpl $0x3, %esi
jne 0x4b2de
cmpq 0x68(%rsp), %rbx
jne 0x4b2de
addq %r12, %rdi
xorl %eax, %eax
movq %rdi, 0x18(%rsp)
cmpl $0x2, %esi
jne 0x4b301
cmpq %rcx, %rbp
jl 0x4b301
cmpq 0x68(%rsp), %rbx
jg 0x4b301
movl 0x10(%rsp), %edi
addq %rbx, %rdi
cmpq 0x68(%rsp), %rdi
jg 0x4b413
testb %al, %al
je 0x4b3fa
cmpw $0x0, 0xc(%rsp)
je 0x4b332
movw $0x1, %ax
movl %eax, 0xc(%rsp)
movl 0x10(%rsp), %eax
testl %esi, %esi
movl %eax, %esi
movl $0x0, %eax
cmovneq %rsi, %rax
addq %r12, %rax
jmp 0x4b204
movq 0x80(%rsp), %r9
movq %r9, %rdi
subq %rcx, %rdi
setle %r8b
movq %r13, %rax
subq %r9, %rax
setl %r9b
orb %r8b, %r9b
je 0x4b35d
movl $0x0, 0xc(%rsp)
jmp 0x4b3a8
cmpl $0x2, %esi
je 0x4b43f
cmpw $0x1, %si
ja 0x4b384
addq %rbx, %rdi
cmpq 0x70(%rsp), %rdi
jne 0x4b353
cmpl $0x1, %esi
movl $0x0, %edi
cmovneq %rdi, %rax
jmp 0x4b398
movl $0x0, 0xc(%rsp)
cmpl $0x3, %esi
jne 0x4b3a8
cmpq 0x70(%rsp), %rbx
jne 0x4b3a8
addq %r12, %rax
movw $0x1, %di
movl %edi, 0xc(%rsp)
movq %rax, 0x20(%rsp)
movl 0x10(%rsp), %eax
cmpq 0x80(%rsp), %r13
jl 0x4b31d
cmpw $0x2, %si
jne 0x4b31d
movq 0x70(%rsp), %r8
cmpq %r8, %rbx
jg 0x4b31d
leaq (%rbx,%rax), %rdi
cmpq %r8, %rdi
jle 0x4b31d
movq %r12, %r9
subq %r8, %r9
addq %rdi, %r9
movq %r9, 0x20(%rsp)
movw $0x1, %di
movl %edi, 0xc(%rsp)
jmp 0x4b31d
movq 0x18(%rsp), %rax
subq 0x20(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq 0x68(%rsp), %r12
addq %rdi, %r12
movq %r12, %rax
jmp 0x4b3ff
leaq 0x19bb00(%rip), %rdi    # 0x1e6f27
leaq 0x198275(%rip), %rsi    # 0x1e36a3
leaq 0x19baf9(%rip), %rcx    # 0x1e6f2e
movl $0x5be7, %edx           # imm = 0x5BE7
callq 0x2110
leaq 0x19bae1(%rip), %rdi    # 0x1e6f27
leaq 0x198256(%rip), %rsi    # 0x1e36a3
leaq 0x19bada(%rip), %rcx    # 0x1e6f2e
movl $0x5c07, %edx           # imm = 0x5C07
callq 0x2110