// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: scan_single_wcigar_toff_backward(bit_extz_t*, long, long, long, long, long, long, long, long, long, long)
int64_t scan_single_wcigar_toff_backward(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 e = 0, ps1, pe1; 
    uint16_t c, sset = 0, eset = 0, ff[2]; 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; ff[0] = ff[1] = 0;
        // 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) e += cl;
            break;
        }
        if(wts > tar_te) {///not ws >= pe
            if(c != 0) e += cl;
            continue;
        }

        if(!eset) {
            if((tar_te>wts) && (tar_te<=wte)) {
                assert(c != 2);
                if(c <= 1) {
                    pe1 = pi + (tar_te - wts); 
                    if(pe1 == tar_pe) {
                        if(c == 1) e = tar_te - wts;
                        else e = 0;
                        eset = 1; 
                    }
                } else if(c == 3) {///more t
                    pe1 = pi;
                    if(pe1 == tar_pe) {
                        e = tar_te - wts;
                        eset = 1; 
                    }
                }
            }

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


        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) e += (wte - tar_ts); 
                        sset = 1; 
                    }
                } else if(c == 3) {///more t
                    ps1 = pi; 
                    if(ps1 == tar_ps) {
                        e += (wte - tar_ts);
                        sset = 1; 
                    }
                }
            } 

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpq %rsi, %rdx
jle 0x7eff4
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %r9
movq 0x50(%rsp), %r11
decq %rdx
movq 0xb8(%rdi), %rdi
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movq %r11, %r13
movslq %edx, %r10
movzwl (%rdi,%r10,2), %r10d
movl %r10d, %r12d
shrl $0xe, %r12d
movl %r10d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
testl %edx, %edx
jle 0x7ed85
leal -0x1(%rdx), %ebx
movzwl (%rdi,%rbx,2), %ebp
movl %ebp, %r15d
shrl $0xe, %r15d
cmpl %r12d, %r15d
jne 0x7ed89
andl $0x3fff, %ebp           # imm = 0x3FFF
addl %ebp, %r11d
decl %edx
jg 0x7ed5e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x7ed89
decl %edx
movl %edx, %ebx
movl %r11d, %ebp
testw %r10w, %r10w
js 0x7ed9d
subq %rbp, %r8
movq %r13, %r11
subq %rbp, %r11
jmp 0x7edb3
cmpw $0x3, %r12w
je 0x7ed95
movzwl %r12w, %edx
cmpl $0x2, %edx
jne 0x7edb0
subq %rbp, %r8
movq %r13, %r11
cmpq %r9, %r13
jl 0x7eff8
movslq %ebx, %rdx
cmpq %r11, %rcx
jge 0x7eded
movzwl %r10w, %r10d
cmpl $0x4000, %r10d          # imm = 0x4000
movl $0x0, %r10d
cmovaeq %rbp, %r10
addq %r10, %rax
movq %r11, %r13
cmpq %rsi, %rdx
jge 0x7ed41
jmp 0x7f007
cmpw $0x0, 0x8(%rsp)
je 0x7ee05
movw $0x1, %bx
movl %ebx, 0x8(%rsp)
movb $0x1, %r15b
jmp 0x7eece
movq %rcx, %r15
subq %r11, %r15
setle %r14b
cmpq %rcx, %r13
setl %bl
orb %r14b, %bl
je 0x7ee24
movl $0x0, 0x8(%rsp)
jmp 0x7ee80
cmpw $0x2, %r12w
movq 0x60(%rsp), %r14
je 0x7f016
testw %r10w, %r10w
js 0x7ee62
leaq (%r8,%r15), %rbx
cmpq %r14, %rbx
jne 0x7ef7c
cmpw $0x1, %r12w
movl $0x0, %eax
cmovneq %rax, %r15
movw $0x1, %ax
movl %eax, 0x8(%rsp)
movq %r15, %rax
jmp 0x7ee85
cmpw $0x3, %r12w
sete %bl
cmpq %r8, %r14
sete %r14b
andb %bl, %r14b
cmovneq %r15, %rax
movzbl %r14b, %ebx
movl %ebx, 0x8(%rsp)
movq 0x60(%rsp), %r14
cmpw $0x2, %r12w
jne 0x7eeb9
cmpq %rcx, %r13
jne 0x7eeb9
cmpq %r11, %rcx
jne 0x7eeb9
movq %r14, %r15
subq %r8, %r15
jl 0x7eeb9
leaq (%r8,%rbp), %rbx
cmpq %r14, %rbx
cmovgq %r15, %rax
movl $0x1, %ebx
movl 0x8(%rsp), %r14d
cmovgl %ebx, %r14d
jmp 0x7eebe
movl 0x8(%rsp), %r14d
movl %r14d, 0x8(%rsp)
testw %r14w, %r14w
sete %r15b
xorl %r14d, %r14d
cmpw $0x0, 0xc(%rsp)
je 0x7eee5
movw $0x1, %bx
movl %ebx, 0xc(%rsp)
movb $0x1, %bl
jmp 0x7efcc
movb %r15b, 0x7(%rsp)
movq %r9, %r15
subq %r11, %r15
setl %r14b
movq %r13, %rbx
subq %r9, %rbx
movq %rbx, 0x10(%rsp)
setle %bl
orb %r14b, %bl
jne 0x7ef45
cmpw $0x2, %r12w
movq 0x58(%rsp), %r14
je 0x7f035
testw %r10w, %r10w
js 0x7ef54
addq %r8, %r15
cmpq %r14, %r15
jne 0x7ef89
cmpw $0x1, %r12w
movl $0x0, %ebx
movq 0x10(%rsp), %r15
cmovneq %rbx, %r15
addq %r15, %rax
movw $0x1, %bx
movl %ebx, 0xc(%rsp)
jmp 0x7ef91
movl $0x0, 0xc(%rsp)
movq 0x58(%rsp), %r14
jmp 0x7ef91
movl $0x0, 0xc(%rsp)
cmpw $0x3, %r12w
movb 0x7(%rsp), %r15b
jne 0x7ef96
cmpq %r14, %r8
jne 0x7ef96
addq 0x10(%rsp), %rax
movw $0x1, %bx
movl %ebx, 0xc(%rsp)
jmp 0x7ef96
movl $0x0, 0x8(%rsp)
jmp 0x7ee85
movl $0x0, 0xc(%rsp)
movb 0x7(%rsp), %r15b
cmpw $0x2, %r12w
jne 0x7efc0
cmpq %r9, %r13
jne 0x7efc0
cmpq %r11, %r9
jne 0x7efc0
cmpq %r14, %r8
jg 0x7efc0
leaq (%r8,%rbp), %rbx
subq %r14, %rbx
jle 0x7efc0
addq %rbx, %rax
movw $0x1, %bx
movl %ebx, 0xc(%rsp)
xorl %r14d, %r14d
cmpw $0x0, 0xc(%rsp)
sete %bl
testb %bl, %bl
je 0x7f007
movzwl %r10w, %r10d
cmpl $0x4000, %r10d          # imm = 0x4000
cmovbq %r14, %rbp
testb %r15b, %r15b
cmoveq %r14, %rbp
addq %rbp, %rax
cmpq %rsi, %rdx
jge 0x7ed3e
jmp 0x7f007
xorl %eax, %eax
jmp 0x7f007
movzwl %r10w, %ecx
cmpl $0x4000, %ecx           # imm = 0x4000
jb 0x7f007
addq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a9eaa(%rip), %rdi    # 0x228ec7
leaq 0x1a661f(%rip), %rsi    # 0x225643
leaq 0x1a9f3b(%rip), %rcx    # 0x228f66
movl $0x5c4f, %edx           # imm = 0x5C4F
callq 0x2110
leaq 0x1a9e8b(%rip), %rdi    # 0x228ec7
leaq 0x1a6600(%rip), %rsi    # 0x225643
leaq 0x1a9f1c(%rip), %rcx    # 0x228f66
movl $0x5c71, %edx           # imm = 0x5C71
callq 0x2110