// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_werr(bit_extz_t*, long, long, long, long, long, long, long, long, long, long, long, rtrace_iter*)
int64_t extract_sub_werr(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 spec_toff, rtrace_iter *idx)
{
    if(idx->toff >= tar_pe && idx->qoff >= tar_te) {
        idx->coff = cei-1;
        idx->toff = cpe;
        idx->qoff = cte;
        idx->cerr = 0;
    }
    if(!(is_align(*ez))) return INT32_MAX;
    if(!(ez->cigar.n)) return (((double)(tar_te-spec_toff))/((double)(tar_te-tar_ts)))*ez->err;

    int64_t ci = idx->coff, pi = idx->toff, ti = idx->qoff, err = idx->cerr;
    int64_t t[2], ci0, pi0, ti0, err0, re = INT32_MAX, wts, wte, ps1, pe1;
    uint16_t c, sset = 0, eset = 0, ff[2]; uint32_t cl;
    while (ci >= csi) {
        ci0 = ci; pi0 = pi; ti0 = ti; err0 = err;
        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;
        t[0] = wts; t[1] = wte;
        // if(spec_toff == 118374) {
        //     fprintf(stderr, "+[M::%s::ci->%ld::cl->%u::c->%u] spec_toff::%ld, t::[%ld, %ld), p::[%ld, %ld), err0::%ld\n", 
        //     __func__, ci, cl, c, spec_toff, ti, ti0, pi, pi0, err0);
        // }

        if(wte < tar_ts) {///not we <= ps
            if(c != 0) err += cl;
            break;
        }
        if(wts > tar_te) {///not ws >= pe
            if(c != 0) err += 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) err = tar_te - wts;
                        else err = 0;
                        eset = 1; 
                    }
                } else if(c == 3) {///more t
                    pe1 = pi;
                    if(pe1 == tar_pe) {
                        err = tar_te - wts;
                        eset = 1; 
                    }
                }
            }

            if((c == 2) && (tar_te>=wts) && (tar_te<=wte)) {///more p
                if(tar_pe >= pi && tar_pe < pi + cl) {
                    err = tar_pe - pi;
                    eset = 1; 
                }
            }
            if(eset) {
                ff[0] = 1; t[1] = tar_te;
            }
        }


        if(!sset) {
            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 += (wte - tar_ts); 
                        sset = 1; 
                    }
                } else if(c == 3) {///more t
                    ps1 = pi; 
                    if(ps1 == tar_ps) {
                        err += (wte - tar_ts);
                        sset = 1; 
                    }
                }
            } 

            if((c == 2) && (tar_ts>=wts) && (tar_ts<=wte)) {///more p
                if(tar_ps >= pi && tar_ps < pi + cl) {
                    err += (pi + cl - tar_ps);
                    sset = 1; 
                }
            }
            if(sset) {
                ff[1] = 1; t[0] = tar_ts;
            }
        }

        // if(spec_toff == 118374) {
        //     fprintf(stderr, "-[M::%s::ci->%ld::cl->%u::c->%u] spec_toff::%ld, t::[%ld, %ld), p::[%ld, %ld), err0::%ld\n", 
        //     __func__, ci, cl, c, spec_toff, ti, ti0, pi, pi0, err0);
        // }
        if(spec_toff > t[1]) break;

        if((spec_toff >= t[0] && spec_toff < t[1]) || (spec_toff == t[0] && spec_toff == t[1])) {
            re = err + (((!ff[1]) && (!ff[0]) && (c!=0))?cl:0);
            if(c == 1 || c == 3) re -= (spec_toff - t[0]);
            idx->coff = ci0;
            idx->toff = pi0;
            idx->qoff = ti0;
            idx->cerr = err0;
            // return re;
        }
        if(ff[1]) break;
        if(ff[0]) continue;
        if(c!=0) err += cl;
    }
    return re;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x38(%rsp)
movq 0xb0(%rsp), %r9
movq 0xa0(%rsp), %rcx
movq 0x90(%rsp), %rax
cmpq %rax, 0x80(%r9)
jl 0x7f9a9
cmpq %rcx, 0x70(%r9)
jl 0x7f9a9
movq 0x80(%rsp), %rax
decq %rdx
movq %rdx, 0x88(%r9)
movq %r8, 0x80(%r9)
movq %rax, 0x70(%r9)
movq $0x0, 0x68(%r9)
movl 0x2c(%rdi), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl 0x28(%rdi), %edx
jg 0x7fe8a
movq 0xa8(%rsp), %r8
movq 0x98(%rsp), %rsi
cmpq $0x0, 0xa8(%rdi)
je 0x7fe5f
movq 0x88(%r9), %r8
cmpq 0x38(%rsp), %r8
jl 0x7fe8a
movq 0x68(%r9), %r14
movq 0x70(%r9), %rbp
movq 0x80(%r9), %rbx
movq 0xb8(%rdi), %rdi
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movq %rax, 0x20(%rsp)
movq %r8, %r15
movq %rbx, %r12
movq %rbp, %r13
movq 0x38(%rsp), %rax
movslq %r15d, %rdx
movzwl (%rdi,%rdx,2), %edx
movl %edx, %r11d
shrl $0xe, %r11d
movl %edx, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
testl %r15d, %r15d
jle 0x7fa6b
movl %r15d, %r9d
leal -0x1(%r9), %r8d
movzwl (%rdi,%r8,2), %ebx
movl %ebx, %ebp
shrl $0xe, %ebp
cmpl %r11d, %ebp
jne 0x7fa6f
andl $0x3fff, %ebx           # imm = 0x3FFF
addl %ebx, %r10d
decl %r9d
jg 0x7fa42
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0x7fa6f
leal -0x1(%r15), %r8d
movl %r10d, %r10d
testw %dx, %dx
js 0x7fac0
movq %r12, %rbx
subq %r10, %rbx
movq %r13, %rbp
subq %r10, %rbp
cmpq %rsi, %r13
jl 0x7fe85
movslq %r8d, %r8
cmpq %rbp, %rcx
jge 0x7faed
movzwl %dx, %edx
cmpl $0x4000, %edx           # imm = 0x4000
movl $0x0, %edx
cmovaeq %r10, %rdx
addq %rdx, %r14
movq %r8, %r15
movq %rbx, %r12
movq %rbp, %r13
cmpq %rax, %r8
jge 0x7fa22
jmp 0x7fe85
cmpw $0x3, %r11w
je 0x7fae2
movzwl %r11w, %r9d
movq %r13, %rbp
movq %r12, %rbx
cmpl $0x2, %r9d
jne 0x7fa83
movq %r12, %rbx
subq %r10, %rbx
movq %r13, %rbp
jmp 0x7fa83
movq %r13, %rbp
subq %r10, %rbp
movq %r12, %rbx
jmp 0x7fa83
movb $0x1, %al
movl %eax, 0x1c(%rsp)
movq %r13, 0x30(%rsp)
movq %r14, %r9
movb $0x1, %al
movl %eax, 0x18(%rsp)
cmpw $0x0, (%rsp)
movw $0x1, %ax
movl %eax, (%rsp)
jne 0x7fc1c
movq %rcx, %r9
subq %rbp, %r9
movq %r9, %rax
setle (%rsp)
cmpq %rcx, %r13
setl %r9b
orb (%rsp), %r9b
jne 0x7fb65
cmpw $0x2, %r11w
je 0x7fe99
testw %dx, %dx
js 0x7fb6c
movq %rax, %r9
addq %rbx, %r9
cmpq 0x90(%rsp), %r9
jne 0x7fb65
cmpw $0x1, %r11w
movl $0x0, %r9d
cmovneq %r9, %rax
movq %rax, %r9
movw $0x1, %ax
jmp 0x7fb97
xorl %eax, %eax
movq %r14, %r9
jmp 0x7fb97
cmpw $0x3, %r11w
sete (%rsp)
cmpq %rbx, 0x90(%rsp)
sete %r9b
andb (%rsp), %r9b
cmoveq %r14, %rax
movzbl %r9b, %r9d
movl %r9d, (%rsp)
movq %rax, %r9
movl (%rsp), %eax
cmpw $0x2, %r11w
jne 0x7fbe7
cmpq %rcx, %r13
jl 0x7fbe7
movl %eax, (%rsp)
movq 0x90(%rsp), %rax
movq %rax, %rsi
subq %rbx, %rsi
jl 0x7fbdc
movq %rsi, 0x10(%rsp)
movq %r9, %rsi
leaq (%rbx,%r10), %r9
cmpq %rax, %r9
movq %rsi, %r9
cmovgq 0x10(%rsp), %r9
movl $0x1, %eax
movl (%rsp), %esi
cmovgl %eax, %esi
movl %esi, (%rsp)
movq 0x98(%rsp), %rsi
movl (%rsp), %eax
testw %ax, %ax
je 0x7fc0a
movl $0x0, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
movq %r9, %rax
movw $0x1, %r9w
movl %r9d, (%rsp)
movq %rax, %r9
jmp 0x7fc1c
movl $0x0, (%rsp)
movq %r13, 0x30(%rsp)
movb $0x1, %al
movl %eax, 0x18(%rsp)
cmpw $0x0, 0x8(%rsp)
movq %r9, 0x10(%rsp)
je 0x7fc3d
movq %rbp, %rsi
movw $0x1, %ax
movq 0xa8(%rsp), %r11
jmp 0x7fd6d
movq %rsi, %r9
subq %rbp, %r9
movq %r9, 0x8(%rsp)
setl %al
movq %r13, %r9
subq %rsi, %r9
movq %r9, 0x28(%rsp)
setle %r9b
orb %al, %r9b
je 0x7fc71
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r9
jmp 0x7fcfd
cmpw $0x2, %r11w
movq 0x10(%rsp), %r9
je 0x7feb8
testw %dx, %dx
js 0x7fcbe
movq 0x8(%rsp), %rax
addq %rbx, %rax
cmpq 0x88(%rsp), %rax
jne 0x7fcf5
cmpw $0x1, %r11w
movl $0x0, %r9d
movq 0x28(%rsp), %rax
cmovneq %r9, %rax
movq 0x10(%rsp), %r9
addq %rax, %r9
movw $0x1, %ax
movl %eax, 0x8(%rsp)
jmp 0x7fcfd
movl $0x0, 0x8(%rsp)
cmpw $0x3, %r11w
jne 0x7fcfd
movq 0x88(%rsp), %rax
cmpq %rax, %rbx
jne 0x7fd05
addq 0x28(%rsp), %r9
movq %r9, 0x10(%rsp)
movw $0x1, %r9w
movl %r9d, 0x8(%rsp)
movq 0x10(%rsp), %r9
jmp 0x7fd05
movl $0x0, 0x8(%rsp)
movq 0x88(%rsp), %rax
cmpw $0x2, %r11w
jne 0x7fd44
cmpq %rbp, %rsi
movq 0xa8(%rsp), %r11
jl 0x7fd4c
cmpq %rax, %rbx
jg 0x7fd4c
movq %r9, 0x10(%rsp)
leaq (%rbx,%r10), %r9
subq %rax, %r9
jle 0x7fd3d
movq 0x10(%rsp), %rax
addq %r9, %rax
movq %rax, %r9
movw $0x1, %ax
jmp 0x7fd50
movq 0x10(%rsp), %r9
jmp 0x7fd4c
movq 0xa8(%rsp), %r11
movl 0x8(%rsp), %eax
testw %ax, %ax
movq %r9, 0x10(%rsp)
je 0x7fd68
movl $0x0, 0x1c(%rsp)
movw $0x1, %ax
jmp 0x7fd6d
xorl %eax, %eax
movq %rbp, %rsi
movq 0x30(%rsp), %r9
cmpq %r11, %r9
jl 0x7fe85
movl %eax, 0x8(%rsp)
movq %r11, %rax
movq %rsi, %r11
subq %rax, %r11
setle 0x7(%rsp)
cmpq %rax, %r9
setg %al
movq %rsi, 0x40(%rsp)
movzwl %dx, %esi
movl %esi, 0x28(%rsp)
testb %al, 0x7(%rsp)
movq 0x10(%rsp), %rsi
jne 0x7fdc4
movq 0x40(%rsp), %rax
xorq 0xa8(%rsp), %rax
xorq 0xa8(%rsp), %r9
orq %rax, %r9
jne 0x7fe1e
cmpb $0x0, 0x1c(%rsp)
movl $0x0, %eax
cmovneq %r10, %rax
cmpb $0x0, 0x18(%rsp)
movl $0x0, %r9d
cmoveq %r9, %rax
cmpl $0x4000, 0x28(%rsp)     # imm = 0x4000
cmovbq %r9, %rax
btl $0xe, %edx
cmovaeq %r9, %r11
addq %rsi, %r11
addq %rax, %r11
movq 0xb0(%rsp), %rax
movq %r15, 0x88(%rax)
movq %r12, 0x80(%rax)
movq %r13, 0x70(%rax)
movq %r14, 0x68(%rax)
movq %r11, 0x20(%rsp)
cmpb $0x0, 0x1c(%rsp)
je 0x7fe85
cmpl $0x4000, 0x28(%rsp)     # imm = 0x4000
movl $0x0, %eax
cmovbq %rax, %r10
cmpb $0x0, 0x18(%rsp)
cmoveq %rax, %r10
addq %r10, %rsi
movq %rsi, %r14
cmpq 0x38(%rsp), %r8
movq 0x98(%rsp), %rsi
movq 0x20(%rsp), %rax
jge 0x7fa0f
jmp 0x7fe8a
movq %rcx, %rax
subq %r8, %rax
cvtsi2sd %rax, %xmm0
subq %rsi, %rcx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %edx, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
jmp 0x7fe8a
movq 0x20(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a9027(%rip), %rdi    # 0x228ec7
leaq 0x1a579c(%rip), %rsi    # 0x225643
leaq 0x1a9149(%rip), %rcx    # 0x228ff7
movl $0x5d48, %edx           # imm = 0x5D48
callq 0x2110
leaq 0x1a9008(%rip), %rdi    # 0x228ec7
leaq 0x1a577d(%rip), %rsi    # 0x225643
leaq 0x1a912a(%rip), %rcx    # 0x228ff7
movl $0x5d67, %edx           # imm = 0x5D67
callq 0x2110