// File: /workspace/github_cpp_makefile_star2_O3/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
cmpq %rsi, %rdx
jle 0x7e693
movq %rdx, %rcx
movq 0x78(%rsp), %r12
movq 0x60(%rsp), %r10
decq %rcx
movq 0xb8(%rdi), %rdi
xorl %eax, %eax
xorl %edx, %edx
xorl %r9d, %r9d
xorl %r15d, %r15d
movq %rdx, 0x20(%rsp)
movq %r15, %r14
movq %r10, %r13
movslq %ecx, %rdx
movzwl (%rdi,%rdx,2), %ebx
movl %ebx, %ebp
shrl $0xe, %ebp
movl %ebx, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
testl %ecx, %ecx
jle 0x7e4ab
leal -0x1(%rcx), %edx
movzwl (%rdi,%rdx,2), %r11d
movl %r11d, %r15d
shrl $0xe, %r15d
cmpl %ebp, %r15d
jne 0x7e4af
andl $0x3fff, %r11d          # imm = 0x3FFF
addl %r11d, %r10d
decl %ecx
jg 0x7e482
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x7e4af
decl %ecx
movl %ecx, %edx
movl %r10d, %r15d
testw %bx, %bx
js 0x7e4c2
subq %r15, %r8
movq %r13, %r10
subq %r15, %r10
jmp 0x7e4d6
cmpw $0x3, %bp
je 0x7e4ba
movzwl %bp, %ecx
cmpl $0x2, %ecx
jne 0x7e4d3
subq %r15, %r8
movq %r13, %r10
cmpq %r12, %r13
jl 0x7e698
movslq %edx, %rcx
cmpq %r10, 0x80(%rsp)
jge 0x7e50d
movzwl %bx, %edx
cmpl $0x4000, %edx           # imm = 0x4000
cmovbq %rax, %r15
addq %r15, %r14
movq %r10, %r13
cmpq %rsi, %rcx
jge 0x7e468
jmp 0x7e698
movq %r9, 0x10(%rsp)
movq %r12, %r11
subq %r10, %r11
setl %dl
movq %r13, %r9
subq %r12, %r9
movq %r9, 0x18(%rsp)
setle %r9b
orb %dl, %r9b
cmpw $0x2, %bp
sete %dl
testb %r9b, %r9b
je 0x7e568
movl %edx, 0xc(%rsp)
cmpw $0x2, %bp
movq 0x10(%rsp), %r9
movq 0x68(%rsp), %rdx
jne 0x7e5ae
cmpq %r10, %r12
jl 0x7e5ae
cmpq %rdx, %r8
jg 0x7e5ae
movq %rdx, %r11
leaq (%r8,%r15), %rdx
cmpq %r11, %rdx
jle 0x7e5ae
jmp 0x7e6aa
cmpw $0x2, %bp
movq 0x68(%rsp), %rdx
je 0x7e6cf
testw %bx, %bx
movq 0x10(%rsp), %r9
js 0x7e597
addq %r8, %r11
cmpq %rdx, %r11
je 0x7e6b7
movl $0x0, 0xc(%rsp)
jmp 0x7e5ae
movl $0x0, 0xc(%rsp)
cmpw $0x3, %bp
jne 0x7e5ae
cmpq %rdx, %r8
je 0x7e6c5
movw $0x1, %r11w
cmpw $0x0, 0x20(%rsp)
jne 0x7e675
movq 0x80(%rsp), %rdx
movq %rdx, %r11
subq %r10, %r11
setle %r9b
movq %r13, %r12
subq %rdx, %r12
setl %dl
orb %r9b, %dl
movq 0x70(%rsp), %rdx
je 0x7e61f
cmpq 0x80(%rsp), %r13
jl 0x7e668
cmpw $0x2, %bp
movq 0x10(%rsp), %r9
movq 0x78(%rsp), %r12
jne 0x7e646
cmpq %rdx, %r8
jg 0x7e646
movq %rdx, %r11
leaq (%r8,%r15), %rdx
cmpq %r11, %rdx
jle 0x7e646
movq %r14, %r9
subq %r11, %r9
addq %rdx, %r9
movw $0x1, %r11w
jmp 0x7e675
cmpb $0x0, 0xc(%rsp)
jne 0x7e6ee
testw %bx, %bx
movq 0x10(%rsp), %r9
js 0x7e64b
addq %r8, %r11
cmpq %rdx, %r11
jne 0x7e663
cmpw $0x1, %bp
cmovneq %rax, %r12
jmp 0x7e656
xorl %r11d, %r11d
jmp 0x7e675
cmpw $0x3, %bp
jne 0x7e5e4
cmpq %rdx, %r8
jne 0x7e5e4
addq %r14, %r12
movq %r12, %r9
movw $0x1, %r11w
jmp 0x7e670
xorl %r11d, %r11d
jmp 0x7e670
xorl %r11d, %r11d
movq 0x10(%rsp), %r9
movq 0x78(%rsp), %r12
movzwl %bx, %edx
cmpl $0x4000, %edx           # imm = 0x4000
cmovbq %rax, %r15
addq %r14, %r15
movl %r11d, %edx
cmpq %rsi, %rcx
jge 0x7e45d
jmp 0x7e698
xorl %r9d, %r9d
xorl %eax, %eax
subq %r9, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq 0x68(%rsp), %r14
addq %rdx, %r14
movq %r14, %rax
jmp 0x7e698
xorl %eax, %eax
cmpw $0x1, %bp
cmoveq 0x18(%rsp), %rax
jmp 0x7e6ca
movq 0x18(%rsp), %rax
addq %r14, %rax
jmp 0x7e698
leaq 0x1b6901(%rip), %rdi    # 0x234fd7
leaq 0x1b3076(%rip), %rsi    # 0x231753
leaq 0x1b68fa(%rip), %rcx    # 0x234fde
movl $0x5be7, %edx           # imm = 0x5BE7
callq 0x2110
leaq 0x1b68e2(%rip), %rdi    # 0x234fd7
leaq 0x1b3057(%rip), %rsi    # 0x231753
leaq 0x1b68db(%rip), %rcx    # 0x234fde
movl $0x5c07, %edx           # imm = 0x5C07
callq 0x2110