// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movw $0x0, -0x74(%rbp)
movw $0x0, -0x76(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jl 0x90530
movq -0x8(%rbp), %rdi
addq $0xa8, %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
leaq -0x72(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0xc1f00
cltq
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movzwl -0x72(%rbp), %eax
cmpl $0x1, %eax
jg 0x9020c
movl -0x80(%rbp), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x80(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x90244
movzwl -0x72(%rbp), %eax
cmpl $0x2, %eax
jne 0x90227
movl -0x80(%rbp), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x90242
movzwl -0x72(%rbp), %eax
cmpl $0x3, %eax
jne 0x90240
movl -0x80(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x90242
jmp 0x90244
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movw $0x0, -0x78(%rbp)
movw $0x0, -0x7a(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0x9027b
movzwl -0x72(%rbp), %eax
cmpl $0x0, %eax
je 0x90276
movl -0x80(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x90530
movq -0x50(%rbp), %rax
cmpq 0x30(%rbp), %rax
jle 0x9029e
movzwl -0x72(%rbp), %eax
cmpl $0x0, %eax
je 0x90299
movl -0x80(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x901a7
cmpw $0x0, -0x76(%rbp)
jne 0x903cc
movq 0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x9036f
movq 0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x9036f
movzwl -0x72(%rbp), %eax
cmpl $0x2, %eax
je 0x902d0
jmp 0x902ef
leaq 0x2f58c8(%rip), %rdi    # 0x385b9f
leaq 0x2f203d(%rip), %rsi    # 0x38231b
movl $0x5c4f, %edx           # imm = 0x5C4F
leaq 0x2f5954(%rip), %rcx    # 0x385c3e
callq 0x2130
movzwl -0x72(%rbp), %eax
cmpl $0x1, %eax
jg 0x9033c
movq -0x40(%rbp), %rax
movq 0x30(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x20(%rbp), %rax
jne 0x9033a
movzwl -0x72(%rbp), %eax
cmpl $0x1, %eax
jne 0x9032c
movq 0x30(%rbp), %rax
subq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x90334
movq $0x0, -0x60(%rbp)
movw $0x1, -0x76(%rbp)
jmp 0x9036d
movzwl -0x72(%rbp), %eax
cmpl $0x3, %eax
jne 0x9036b
movq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x20(%rbp), %rax
jne 0x90369
movq 0x30(%rbp), %rax
subq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
movw $0x1, -0x76(%rbp)
jmp 0x9036b
jmp 0x9036d
jmp 0x9036f
movzwl -0x72(%rbp), %eax
cmpl $0x2, %eax
jne 0x903bd
movq 0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x903bd
movq 0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x903bd
movq 0x20(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x903bb
movq 0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x80(%rbp), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jge 0x903bb
movq 0x20(%rbp), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movw $0x1, -0x76(%rbp)
jmp 0x903bd
cmpw $0x0, -0x76(%rbp)
je 0x903ca
movw $0x1, -0x7a(%rbp)
jmp 0x903cc
cmpw $0x0, -0x74(%rbp)
jne 0x90502
movq 0x28(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x9049b
movq 0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x9049b
movzwl -0x72(%rbp), %eax
cmpl $0x2, %eax
je 0x903fe
jmp 0x9041d
leaq 0x2f579a(%rip), %rdi    # 0x385b9f
leaq 0x2f1f0f(%rip), %rsi    # 0x38231b
movl $0x5c71, %edx           # imm = 0x5C71
leaq 0x2f5826(%rip), %rcx    # 0x385c3e
callq 0x2130
movzwl -0x72(%rbp), %eax
cmpl $0x1, %eax
jg 0x90464
movq -0x40(%rbp), %rax
movq 0x28(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq 0x18(%rbp), %rax
jne 0x90462
movzwl -0x72(%rbp), %eax
cmpl $0x1, %eax
jne 0x9045c
movq -0x58(%rbp), %rax
subq 0x28(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movw $0x1, -0x74(%rbp)
jmp 0x90499
movzwl -0x72(%rbp), %eax
cmpl $0x3, %eax
jne 0x90497
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq 0x18(%rbp), %rax
jne 0x90495
movq -0x58(%rbp), %rax
subq 0x28(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movw $0x1, -0x74(%rbp)
jmp 0x90497
jmp 0x90499
jmp 0x9049b
movzwl -0x72(%rbp), %eax
cmpl $0x2, %eax
jne 0x904f3
movq 0x28(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x904f3
movq 0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x904f3
movq 0x18(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x904f1
movq 0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x80(%rbp), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jge 0x904f1
movq -0x40(%rbp), %rax
movl -0x80(%rbp), %ecx
addq %rcx, %rax
subq 0x18(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movw $0x1, -0x74(%rbp)
jmp 0x904f3
cmpw $0x0, -0x74(%rbp)
je 0x90500
movw $0x1, -0x78(%rbp)
jmp 0x90502
cmpw $0x0, -0x78(%rbp)
je 0x9050b
jmp 0x90530
cmpw $0x0, -0x7a(%rbp)
je 0x90517
jmp 0x901a7
movzwl -0x72(%rbp), %eax
cmpl $0x0, %eax
je 0x9052b
movl -0x80(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x901a7
movq -0x60(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)