// File: chhylp123[P]hifiasm/Correct.cpp
// Function: retrieve_cigar_err_debug(bit_extz_t*, long, long)
int64_t retrieve_cigar_err_debug(bit_extz_t *ez, int64_t s, int64_t e)
{
    if(!ez->cigar.n) return 0;
    int64_t err = 0, xk = ez->ts; int64_t ws, we, os, oe, ovlp;
    uint32_t ck = 0, cl; uint16_t op;

    os = MAX(s, ez->ts); oe = MIN(e, ez->te+1);
    ovlp = ((oe>os)? (oe-os):0); 
    if(!ovlp) {
        fprintf(stderr, "[M::%s::] s::%ld, e::%ld, ez->ts::%u, ez->te::%u\n", 
        __func__, s, e, ez->ts, ez->te);
    }
    assert(ovlp);

    //some cigar will span s or e
    while (ck < ez->cigar.n && xk < e) {//[s, e)
        ws = xk; 
        ck = pop_trace(&(ez->cigar), ck, &op, &cl);
        if(op!=2) xk += cl;
        we = xk;
        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);
        if((op==2) && (ws>=s) && (ws<e)) {
            ovlp = cl;
        }
        if((!ovlp) || (!op)) continue;
        err += ovlp;
    }
    return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa8(%rax)
jne 0x6ad62
movq $0x0, -0x8(%rbp)
jmp 0x6afcd
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movslq 0x1c(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
cmpq %rcx, %rax
jl 0x6ad98
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x6ada4
movq -0x10(%rbp), %rax
movslq 0x1c(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jg 0x6adcc
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x6addc
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0x6adfc
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x6ae04
xorl %eax, %eax
movq %rax, -0x80(%rbp)
jmp 0x6ae04
movq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x6ae4c
movq 0x3591c6(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %r9d
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
leaq 0x319868(%rip), %rsi    # 0x3846a3
leaq 0x319893(%rip), %rdx    # 0x3846d5
movl %eax, (%rsp)
movb $0x0, %al
callq 0x22a0
cmpq $0x0, -0x58(%rbp)
je 0x6ae55
jmp 0x6ae74
leaq 0x32bfc8(%rip), %rdi    # 0x396e24
leaq 0x3174b8(%rip), %rsi    # 0x38231b
movl $0x4648, %edx           # imm = 0x4648
leaq 0x31987f(%rip), %rcx    # 0x3846ee
callq 0x2130
jmp 0x6ae76
movl -0x5c(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0xa8(%rdx), %rcx
movb %al, -0x81(%rbp)
jae 0x6aea1
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x6aeb0
jmp 0x6afc5
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
addq $0xa8, %rdi
movl -0x5c(%rbp), %esi
leaq -0x62(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xa1ee0
movl %eax, -0x5c(%rbp)
movzwl -0x62(%rbp), %eax
cmpl $0x2, %eax
je 0x6aeea
movl -0x60(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x6af09
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x6af14
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x40(%rbp), %rax
jg 0x6af36
movq -0x20(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x6af41
movq -0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0x6af67
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x6af72
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x6af72
movq -0xa0(%rbp), %rax
movq %rax, -0x58(%rbp)
movzwl -0x62(%rbp), %eax
cmpl $0x2, %eax
jne 0x6afa1
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x6afa1
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x6afa1
movl -0x60(%rbp), %eax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x6afaf
cmpw $0x0, -0x62(%rbp)
jne 0x6afb4
jmp 0x6ae76
movq -0x58(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x6ae76
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)