// File: 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
movq %rsp, %rbp
subq $0x90, %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)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2100
movw $0x0, -0x84(%rbp)
movw $0x0, -0x86(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jl 0x90127
movq -0x8(%rbp), %rdi
addq $0xa8, %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
leaq -0x82(%rbp), %rdx
leaq -0x8c(%rbp), %rcx
callq 0xc1f00
cltq
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movzwl -0x82(%rbp), %eax
cmpl $0x1, %eax
jg 0x8fda1
movl -0x8c(%rbp), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x8fde5
movzwl -0x82(%rbp), %eax
cmpl $0x2, %eax
jne 0x8fdc2
movl -0x8c(%rbp), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x8fde3
movzwl -0x82(%rbp), %eax
cmpl $0x3, %eax
jne 0x8fde1
movl -0x8c(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x8fde3
jmp 0x8fde5
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0x8fe16
movzwl -0x82(%rbp), %eax
cmpl $0x0, %eax
je 0x8fe11
movl -0x8c(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x90127
movq -0x50(%rbp), %rax
cmpq 0x30(%rbp), %rax
jle 0x8fe3f
movzwl -0x82(%rbp), %eax
cmpl $0x0, %eax
je 0x8fe3a
movl -0x8c(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x8fd2d
cmpw $0x0, -0x84(%rbp)
jne 0x8ffab
movq 0x28(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x8ff33
movq 0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x8ff33
movzwl -0x82(%rbp), %eax
cmpl $0x2, %eax
je 0x8fe77
jmp 0x8fe96
leaq 0x2f5d21(%rip), %rdi    # 0x385b9f
leaq 0x2f2496(%rip), %rsi    # 0x38231b
movl $0x5be7, %edx           # imm = 0x5BE7
leaq 0x2f5d15(%rip), %rcx    # 0x385ba6
callq 0x2130
movzwl -0x82(%rbp), %eax
cmpl $0x1, %eax
jg 0x8fef3
movq -0x40(%rbp), %rax
movq 0x28(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x18(%rbp), %rax
jne 0x8fef1
movzwl -0x82(%rbp), %eax
cmpl $0x1, %eax
jne 0x8fee0
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0x8fee8
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movw $0x1, -0x84(%rbp)
jmp 0x8ff31
movzwl -0x82(%rbp), %eax
cmpl $0x3, %eax
jne 0x8ff2f
movq -0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x18(%rbp), %rax
jne 0x8ff2d
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movw $0x1, -0x84(%rbp)
jmp 0x8ff2f
jmp 0x8ff31
jmp 0x8ff33
movzwl -0x82(%rbp), %eax
cmpl $0x2, %eax
jne 0x8ff9a
movq 0x28(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x8ff9a
movq 0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x8ff9a
movq 0x18(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x8ff98
movq 0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x8c(%rbp), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jge 0x8ff98
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x8c(%rbp), %edx
addq %rdx, %rcx
subq 0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movw $0x1, -0x84(%rbp)
jmp 0x8ff9a
cmpw $0x0, -0x84(%rbp)
je 0x8ffa9
jmp 0x90127
jmp 0x8ffab
cmpw $0x0, -0x86(%rbp)
jne 0x90108
movq 0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x9009f
movq 0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x9009f
movzwl -0x82(%rbp), %eax
cmpl $0x2, %eax
je 0x8ffe3
jmp 0x90002
leaq 0x2f5bb5(%rip), %rdi    # 0x385b9f
leaq 0x2f232a(%rip), %rsi    # 0x38231b
movl $0x5c07, %edx           # imm = 0x5C07
leaq 0x2f5ba9(%rip), %rcx    # 0x385ba6
callq 0x2130
movzwl -0x82(%rbp), %eax
cmpl $0x1, %eax
jg 0x9005f
movq -0x40(%rbp), %rax
movq 0x30(%rbp), %rcx
subq -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq 0x20(%rbp), %rax
jne 0x9005d
movzwl -0x82(%rbp), %eax
cmpl $0x1, %eax
jne 0x9004c
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
jmp 0x90054
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movw $0x1, -0x86(%rbp)
jmp 0x9009d
movzwl -0x82(%rbp), %eax
cmpl $0x3, %eax
jne 0x9009b
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq 0x20(%rbp), %rax
jne 0x90099
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movw $0x1, -0x86(%rbp)
jmp 0x9009b
jmp 0x9009d
jmp 0x9009f
movzwl -0x82(%rbp), %eax
cmpl $0x2, %eax
jne 0x90106
movq 0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x90106
movq 0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x90106
movq 0x20(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x90104
movq 0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x8c(%rbp), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jge 0x90104
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x8c(%rbp), %edx
addq %rdx, %rcx
subq 0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movw $0x1, -0x86(%rbp)
jmp 0x90106
jmp 0x90108
movzwl -0x82(%rbp), %eax
cmpl $0x0, %eax
je 0x90122
movl -0x8c(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x8fd2d
movq -0x70(%rbp), %rax
subq -0x68(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)