// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
cmpq %rsi, %rdx
jle 0x7e979
movq 0x60(%rsp), %rcx
movq 0x58(%rsp), %r14
movq 0x40(%rsp), %r13
decq %rdx
movq 0xb8(%rdi), %rdi
movl $0x0, (%rsp)
xorl %eax, %eax
movq %r13, %r12
movslq %edx, %r9
movzwl (%rdi,%r9,2), %ebp
movl %ebp, %r15d
shrl $0xe, %r15d
movl %ebp, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
testl %edx, %edx
jle 0x7e78d
leal -0x1(%rdx), %r11d
movzwl (%rdi,%r11,2), %r9d
movl %r9d, %ebx
shrl $0xe, %ebx
cmpl %r15d, %ebx
jne 0x7e792
andl $0x3fff, %r9d           # imm = 0x3FFF
addl %r9d, %r10d
decl %edx
jg 0x7e763
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x7e792
decl %edx
movl %edx, %r11d
movl %r10d, %r10d
testw %bp, %bp
js 0x7e7a5
subq %r10, %r8
movq %r12, %r13
subq %r10, %r13
jmp 0x7e7bb
cmpw $0x3, %r15w
je 0x7e79d
movzwl %r15w, %edx
cmpl $0x2, %edx
jne 0x7e7b8
subq %r10, %r8
movq %r12, %r13
cmpq %r14, %r12
jl 0x7e98a
movslq %r11d, %rdx
cmpq %r13, %rcx
jge 0x7e7f5
movzwl %bp, %r9d
cmpl $0x4000, %r9d           # imm = 0x4000
movl $0x0, %r9d
cmovaeq %r10, %r9
addq %r9, %rax
movq %r13, %r12
cmpq %rsi, %rdx
jge 0x7e746
jmp 0x7e97b
cmpw $0x0, (%rsp)
je 0x7e80d
movw $0x1, %cx
movl %ecx, (%rsp)
movb $0x1, 0x7(%rsp)
jmp 0x7e8cb
movq %rcx, %rbx
subq %r13, %rbx
setle %r9b
cmpq %rcx, %r12
setl %r11b
orb %r9b, %r11b
jne 0x7e858
cmpw $0x2, %r15w
je 0x7e9be
testw %bp, %bp
js 0x7e861
leaq (%r8,%rbx), %r9
cmpq 0x50(%rsp), %r9
jne 0x7e858
cmpw $0x1, %r15w
movl $0x0, %eax
cmovneq %rax, %rbx
movw $0x1, %ax
movl %eax, (%rsp)
movq %rbx, %rax
jmp 0x7e882
movl $0x0, (%rsp)
jmp 0x7e882
cmpw $0x3, %r15w
sete %r9b
cmpq %r8, 0x50(%rsp)
sete %r11b
andb %r9b, %r11b
cmovneq %rbx, %rax
movzbl %r11b, %r9d
movl %r9d, (%rsp)
cmpw $0x2, %r15w
jne 0x7e8bd
cmpq %rcx, %r12
jne 0x7e8bd
cmpq %r13, %rcx
jne 0x7e8bd
movq 0x50(%rsp), %rcx
movq %rcx, %r9
subq %r8, %r9
jl 0x7e8bd
leaq (%r8,%r10), %r11
cmpq %rcx, %r11
cmovgq %r9, %rax
movl $0x1, %ecx
movl (%rsp), %r9d
cmovgl %ecx, %r9d
movl %r9d, %ecx
jmp 0x7e8c0
movl (%rsp), %ecx
movl %ecx, (%rsp)
testw %cx, %cx
sete 0x7(%rsp)
movq %r14, %r9
subq %r13, %r9
setl %bl
movq %r12, %r11
subq %r14, %r11
setle %cl
orb %bl, %cl
jne 0x7e90d
cmpw $0x2, %r15w
movb $0x1, %cl
je 0x7e99f
testw %bp, %bp
js 0x7e964
addq %r8, %r9
cmpq 0x48(%rsp), %r9
jne 0x7e90f
xorl %ecx, %ecx
cmpw $0x1, %r15w
cmovneq %rcx, %r11
addq %r11, %rax
jmp 0x7e90f
movb $0x1, %cl
cmpw $0x2, %r15w
jne 0x7e932
cmpq %r14, %r12
jne 0x7e932
cmpq %r13, %r14
jne 0x7e932
cmpq 0x48(%rsp), %r8
jg 0x7e932
leaq (%r8,%r10), %r9
subq 0x48(%rsp), %r9
jg 0x7e99a
testb %cl, %cl
je 0x7e97b
movzwl %bp, %ecx
cmpl $0x4000, %ecx           # imm = 0x4000
movl $0x0, %ecx
cmovbq %rcx, %r10
cmpb $0x0, 0x7(%rsp)
cmoveq %rcx, %r10
addq %r10, %rax
cmpq %rsi, %rdx
movq 0x60(%rsp), %rcx
jge 0x7e743
jmp 0x7e97b
cmpw $0x3, %r15w
jne 0x7e90f
cmpq 0x48(%rsp), %r8
jne 0x7e90f
addq %r11, %rax
xorl %ecx, %ecx
jmp 0x7e90f
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl %bp, %ecx
cmpl $0x4000, %ecx           # imm = 0x4000
jb 0x7e97b
addq %r10, %rax
jmp 0x7e97b
addq %r9, %rax
jmp 0x7e97b
leaq 0x1b6631(%rip), %rdi    # 0x234fd7
leaq 0x1b2da6(%rip), %rsi    # 0x231753
leaq 0x1b66c2(%rip), %rcx    # 0x235076
movl $0x5c71, %edx           # imm = 0x5C71
callq 0x2110
leaq 0x1b6612(%rip), %rdi    # 0x234fd7
leaq 0x1b2d87(%rip), %rsi    # 0x231753
leaq 0x1b66a3(%rip), %rcx    # 0x235076
movl $0x5c4f, %edx           # imm = 0x5C4F
callq 0x2110