// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r8, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0x70(%rsp), %r10
movq 0x50(%rsp), %rcx
decq %rdx
addq $0xa8, %r12
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movq %rcx, %rbp
cmpq %r15, %rdx
jl 0x4b69b
movq %r10, %r13
movq %r12, %rdi
movl %edx, %esi
leaq 0x16(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq 0x62f1e
movzwl 0x16(%rsp), %esi
cmpl $0x1, %esi
ja 0x4b4d2
movl 0xc(%rsp), %edx
subq %rdx, %r14
movq %rbp, %rcx
subq %rdx, %rcx
movq %r13, %r10
jmp 0x4b4f5
cmpl $0x3, %esi
movq %r13, %r10
je 0x4b4eb
cmpl $0x2, %esi
jne 0x4b4e6
movl 0xc(%rsp), %ecx
subq %rcx, %r14
movq %rbp, %rcx
jmp 0x4b4f5
movl 0xc(%rsp), %edx
movq %rbp, %rcx
subq %rdx, %rcx
cmpq 0x68(%rsp), %rbp
jl 0x4b690
movslq %eax, %rdx
cmpq %rcx, %r10
jge 0x4b51f
testl %esi, %esi
movl 0xc(%rsp), %eax
movl $0x0, %esi
cmovneq %rax, %rsi
addq %rsi, %rbx
jmp 0x4b493
movb $0x1, %al
cmpw $0x0, 0x10(%rsp)
je 0x4b539
movw $0x1, %di
movl %edi, 0x10(%rsp)
movb $0x1, %dil
jmp 0x4b5e7
movq %r10, %rdi
subq %rcx, %rdi
setle %r8b
cmpq %r10, %rbp
setl %r9b
orb %r8b, %r9b
je 0x4b554
xorl %r8d, %r8d
jmp 0x4b5a0
cmpl $0x2, %esi
je 0x4b6d6
cmpw $0x1, %si
ja 0x4b585
leaq (%r14,%rdi), %r8
cmpq 0x60(%rsp), %r8
jne 0x4b54f
cmpl $0x1, %esi
movl $0x0, %r8d
cmovneq %r8, %rdi
movw $0x1, %r8w
movq %rdi, %rbx
jmp 0x4b5a0
cmpl $0x3, %esi
sete %r8b
cmpq %r14, 0x60(%rsp)
sete %r9b
andb %r8b, %r9b
cmovneq %rdi, %rbx
movzbl %r9b, %r8d
cmpl $0x2, %esi
jne 0x4b5d7
cmpq %r10, %rbp
jne 0x4b5d7
cmpq %rcx, %r10
jne 0x4b5d7
movq 0x60(%rsp), %r9
movq %r9, %rdi
subq %r14, %rdi
movl %r8d, %r10d
jl 0x4b5da
movl 0xc(%rsp), %r8d
addq %r14, %r8
cmpq %r9, %r8
cmovgq %rdi, %rbx
pushq $0x1
popq %rdi
cmovgl %edi, %r10d
jmp 0x4b5da
movl %r8d, %r10d
movl %r10d, 0x10(%rsp)
testw %r10w, %r10w
sete %dil
movq 0x68(%rsp), %r11
movq %r11, %r9
subq %rcx, %r9
setl %r10b
movq %rbp, %r8
subq %r11, %r8
setle %r11b
orb %r10b, %r11b
jne 0x4b62f
cmpl $0x2, %esi
je 0x4b6b7
cmpw $0x1, %si
movq %r13, %r10
ja 0x4b67d
addq %r14, %r9
cmpq 0x58(%rsp), %r9
jne 0x4b632
xorl %eax, %eax
cmpl $0x1, %esi
cmovneq %rax, %r8
addq %r8, %rbx
jmp 0x4b632
movq %r13, %r10
cmpl $0x2, %esi
jne 0x4b65b
cmpq 0x68(%rsp), %rbp
jne 0x4b65b
cmpq %rcx, 0x68(%rsp)
jne 0x4b65b
cmpq 0x58(%rsp), %r14
jg 0x4b65b
movl 0xc(%rsp), %r8d
addq %r14, %r8
cmpq 0x58(%rsp), %r8
jg 0x4b6ad
testb %al, %al
je 0x4b69b
testl %esi, %esi
movl 0xc(%rsp), %eax
movl $0x0, %esi
cmoveq %rsi, %rax
testb %dil, %dil
cmoveq %rsi, %rax
addq %rax, %rbx
jmp 0x4b493
cmpl $0x3, %esi
jne 0x4b632
cmpq 0x58(%rsp), %r14
jne 0x4b632
addq %r8, %rbx
xorl %eax, %eax
jmp 0x4b632
testl %esi, %esi
je 0x4b69b
movl 0xc(%rsp), %eax
addq %rax, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq 0x58(%rsp), %rbx
addq %r8, %rbx
jmp 0x4b69b
leaq 0x19b869(%rip), %rdi    # 0x1e6f27
leaq 0x197fde(%rip), %rsi    # 0x1e36a3
leaq 0x19b8fa(%rip), %rcx    # 0x1e6fc6
movl $0x5c71, %edx           # imm = 0x5C71
callq 0x2110
leaq 0x19b84a(%rip), %rdi    # 0x1e6f27
leaq 0x197fbf(%rip), %rsi    # 0x1e36a3
leaq 0x19b8db(%rip), %rcx    # 0x1e6fc6
movl $0x5c4f, %edx           # imm = 0x5C4F
callq 0x2110