// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
cmpq %rsi, %rdx
jle 0x7ec9a
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %r9
movq 0x50(%rsp), %r12
decq %rdx
movq 0xb8(%rdi), %rbx
xorl %r10d, %r10d
xorl %edi, %edi
xorl %eax, %eax
xorl %r14d, %r14d
movq %r10, 0x10(%rsp)
movq %rax, (%rsp)
movq %r14, %r15
movq %r12, %r13
movslq %edx, %rax
movzwl (%rbx,%rax,2), %r14d
movl %r14d, %ebp
shrl $0xe, %ebp
movl %r14d, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
testl %edx, %edx
jle 0x7ea7a
leal -0x1(%rdx), %r11d
movzwl (%rbx,%r11,2), %eax
movl %eax, %r12d
shrl $0xe, %r12d
cmpl %ebp, %r12d
jne 0x7ea7f
andl $0x3fff, %eax           # imm = 0x3FFF
addl %eax, %r10d
decl %edx
jg 0x7ea51
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x7ea7f
decl %edx
movl %edx, %r11d
movl %r10d, %r10d
testw %r14w, %r14w
js 0x7ea93
subq %r10, %r8
movq %r13, %r12
subq %r10, %r12
jmp 0x7eaa7
cmpw $0x3, %bp
je 0x7ea8b
movzwl %bp, %eax
cmpl $0x2, %eax
jne 0x7eaa4
subq %r10, %r8
movq %r13, %r12
cmpq %r9, %r13
jl 0x7eca5
movslq %r11d, %rdx
cmpq %r12, %rcx
jge 0x7eade
movzwl %r14w, %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl $0x0, %eax
cmovaeq %r10, %rax
addq %rax, %r15
movq %r12, %r13
cmpq %rsi, %rdx
jge 0x7ea35
jmp 0x7eca5
movq %rdi, 0x8(%rsp)
movq %r9, %r11
subq %r12, %r11
setl %cl
movq %r13, %rax
subq %r9, %rax
setle %dil
orb %cl, %dil
je 0x7eb0a
xorl %r11d, %r11d
movq 0x70(%rsp), %rcx
movq 0x58(%rsp), %rdi
jmp 0x7eb5b
cmpw $0x2, %bp
movq 0x58(%rsp), %rdi
je 0x7ecbb
testw %r14w, %r14w
movq 0x70(%rsp), %rcx
js 0x7eb3c
addq %r8, %r11
cmpq %rdi, %r11
jne 0x7eb58
cmpw $0x1, %bp
movl $0x0, %r11d
cmovneq %r11, %rax
jmp 0x7eb4a
xorl %r11d, %r11d
cmpw $0x3, %bp
jne 0x7eb5b
cmpq %rdi, %r8
jne 0x7eb5b
addq %r15, %rax
movw $0x1, %r11w
movq %rax, (%rsp)
jmp 0x7eb5b
xorl %r11d, %r11d
cmpw $0x2, %bp
jne 0x7eb86
cmpq %r12, %r9
jl 0x7eb86
cmpq %rdi, %r8
jg 0x7eb86
leaq (%r8,%r10), %rax
cmpq %rdi, %rax
jle 0x7eb86
movq %r15, %r11
subq %rdi, %r11
addq %rax, %r11
movq %r11, (%rsp)
movw $0x1, %r11w
testw %r11w, %r11w
jne 0x7eca0
movw $0x1, %r11w
cmpw $0x0, 0x10(%rsp)
je 0x7ebce
movq (%rsp), %rax
movzwl %r14w, %edi
cmpl $0x4000, %edi           # imm = 0x4000
movl $0x0, %r14d
cmovaeq %r10, %r14
addq %r15, %r14
movl %r11d, %r10d
cmpq %rsi, %rdx
movq 0x8(%rsp), %rdi
jge 0x7ea26
jmp 0x7eca9
movq %rcx, %r11
subq %r12, %r11
movq %rcx, %rdi
setle %cl
movq %r13, %rax
subq %rdi, %rax
setl %dil
orb %cl, %dil
je 0x7ebf8
xorl %r11d, %r11d
movq 0x70(%rsp), %rcx
movq 0x8(%rsp), %rdi
jmp 0x7ec4b
cmpw $0x2, %bp
movq 0x8(%rsp), %rdi
je 0x7ecda
testw %r14w, %r14w
movq 0x70(%rsp), %rcx
js 0x7ec2b
addq %r8, %r11
cmpq 0x60(%rsp), %r11
jne 0x7ec48
cmpw $0x1, %bp
movl $0x0, %edi
cmovneq %rdi, %rax
jmp 0x7ec3b
xorl %r11d, %r11d
cmpw $0x3, %bp
jne 0x7ec4b
cmpq 0x60(%rsp), %r8
jne 0x7ec4b
addq %r15, %rax
movw $0x1, %r11w
movq %rax, %rdi
jmp 0x7ec4b
xorl %r11d, %r11d
movq %rdi, 0x8(%rsp)
cmpq %rcx, %r13
jl 0x7eb9d
cmpw $0x2, %bp
movq 0x60(%rsp), %rdi
movq (%rsp), %rax
jne 0x7eba1
cmpq %rdi, %r8
jg 0x7eba1
leaq (%r8,%r10), %r13
cmpq %rdi, %r13
jle 0x7eba1
movq %r15, %r11
subq %rdi, %r11
addq %r13, %r11
movq %r11, 0x8(%rsp)
movw $0x1, %r11w
jmp 0x7eba1
xorl %edi, %edi
xorl %eax, %eax
jmp 0x7eca9
movq 0x8(%rsp), %rdi
movq (%rsp), %rax
subq %rdi, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1aa205(%rip), %rdi    # 0x228ec7
leaq 0x1a697a(%rip), %rsi    # 0x225643
leaq 0x1aa1fe(%rip), %rcx    # 0x228ece
movl $0x5be7, %edx           # imm = 0x5BE7
callq 0x2110
leaq 0x1aa1e6(%rip), %rdi    # 0x228ec7
leaq 0x1a695b(%rip), %rsi    # 0x225643
leaq 0x1aa1df(%rip), %rcx    # 0x228ece
movl $0x5c07, %edx           # imm = 0x5C07
callq 0x2110