// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, 0x50(%rsp)
movq 0xc0(%rsp), %r9
movq 0xb0(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq 0x80(%r9), %r15
cmpq %rax, %r15
jl 0x7f320
cmpq %rcx, 0x70(%r9)
jl 0x7f320
movq 0x90(%rsp), %rax
decq %rdx
movq %rdx, 0x88(%r9)
movq %r8, 0x80(%r9)
movq %rax, 0x70(%r9)
movq $0x0, 0x68(%r9)
movq %r8, %r15
movl 0x2c(%rdi), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl 0x28(%rdi), %edx
jg 0x7f7ba
movq 0xb8(%rsp), %r8
movq 0xa8(%rsp), %rsi
cmpq $0x0, 0xa8(%rdi)
je 0x7f78f
movq 0x88(%r9), %r8
cmpq 0x50(%rsp), %r8
jl 0x7f7ba
movq 0x68(%r9), %r14
movq 0x70(%r9), %r11
movq 0xb8(%rdi), %rdi
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq %rax, 0x30(%rsp)
movq %r8, %r12
movq %r15, %r13
movq %r11, %rbp
movq 0x50(%rsp), %rax
movslq %r12d, %rdx
movzwl (%rdi,%rdx,2), %edx
movl %edx, %ebx
shrl $0xe, %ebx
movl %edx, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
testl %r12d, %r12d
jle 0x7f3dd
movl %r12d, %r11d
leal -0x1(%r11), %r8d
movzwl (%rdi,%r8,2), %r9d
movl %r9d, %r15d
shrl $0xe, %r15d
cmpl %ebx, %r15d
jne 0x7f3e2
andl $0x3fff, %r9d           # imm = 0x3FFF
addl %r9d, %r10d
decl %r11d
jg 0x7f3b1
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0x7f3e2
leal -0x1(%r12), %r8d
movl %r10d, %r10d
testw %dx, %dx
js 0x7f433
movq %r13, %r15
subq %r10, %r15
movq %rbp, %r11
subq %r10, %r11
cmpq %rsi, %rbp
jl 0x7f7b5
movslq %r8d, %r8
cmpq %r11, %rcx
jge 0x7f45f
movzwl %dx, %edx
cmpl $0x4000, %edx           # imm = 0x4000
movl $0x0, %edx
cmovaeq %r10, %rdx
addq %rdx, %r14
movq %r8, %r12
movq %r15, %r13
movq %r11, %rbp
cmpq %rax, %r8
jge 0x7f393
jmp 0x7f7b5
cmpw $0x3, %bx
je 0x7f454
movzwl %bx, %r9d
movq %rbp, %r11
movq %r13, %r15
cmpl $0x2, %r9d
jne 0x7f3f6
movq %r13, %r15
subq %r10, %r15
movq %rbp, %r11
jmp 0x7f3f6
movq %rbp, %r11
subq %r10, %r11
movq %r13, %r15
jmp 0x7f3f6
movb $0x1, %al
movq %rax, 0x38(%rsp)
movq %rbp, 0x20(%rsp)
movq %r14, %r9
movb $0x1, %al
movq %rax, 0x18(%rsp)
cmpw $0x0, 0x10(%rsp)
movw $0x1, %ax
movl %eax, 0x10(%rsp)
movq 0x98(%rsp), %rax
jne 0x7f5a5
movq %rcx, %rax
subq %r11, %rax
setle 0x10(%rsp)
cmpq %rcx, %rbp
setl %r9b
orb 0x10(%rsp), %r9b
je 0x7f51b
movl $0x0, 0x10(%rsp)
cmpw $0x2, %bx
jne 0x7f58e
cmpq %rcx, %rbp
jl 0x7f58e
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movq %r14, %r9
movb $0x1, %al
movq %rax, 0x18(%rsp)
movq 0x98(%rsp), %rax
subq %r15, 0x28(%rsp)
jl 0x7f5a5
leaq (%r15,%r10), %r9
cmpq 0xa0(%rsp), %r9
jle 0x7f586
movq $0x0, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x28(%rsp), %rax
jmp 0x7f577
cmpw $0x2, %bx
je 0x7f7c9
testw %dx, %dx
js 0x7f559
movq %rcx, %rax
subq %r11, %rax
leaq (%r15,%rax), %r9
cmpq 0xa0(%rsp), %r9
jne 0x7f586
cmpw $0x1, %bx
movl $0x0, %r9d
movq %r9, 0x18(%rsp)
movl $0x0, %r9d
cmovneq %r9, %rax
jmp 0x7f572
cmpw $0x3, %bx
jne 0x7f586
cmpq %r15, 0xa0(%rsp)
jne 0x7f586
movq $0x0, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movw $0x1, %r9w
movl %r9d, 0x10(%rsp)
movq %rax, %r9
jmp 0x7f59d
movl $0x0, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
movq %r14, %r9
movb $0x1, %al
movq %rax, 0x18(%rsp)
movq 0x98(%rsp), %rax
cmpw $0x0, 0xc(%rsp)
movq %r9, 0x28(%rsp)
je 0x7f5c2
movq %r11, %r9
movw $0x1, %ax
movl %eax, 0xc(%rsp)
jmp 0x7f66a
movq %rsi, %r9
subq %r11, %r9
movq %r9, 0x48(%rsp)
setl 0xc(%rsp)
movq %rbp, %r9
subq %rsi, %r9
movq %r9, 0x40(%rsp)
setle %r9b
orb 0xc(%rsp), %r9b
je 0x7f616
movl $0x0, 0xc(%rsp)
cmpw $0x2, %bx
movq 0x28(%rsp), %r9
jne 0x7f667
cmpq %r11, %rsi
jl 0x7f667
cmpq %rax, %r15
jg 0x7f667
leaq (%r15,%r10), %rbx
subq %rax, %rbx
jle 0x7f65f
addq %rbx, %r9
jmp 0x7f779
cmpw $0x2, %bx
je 0x7f7e8
testw %dx, %dx
movq 0x28(%rsp), %r9
js 0x7f759
movq 0x48(%rsp), %r9
addq %r15, %r9
cmpq %rax, %r9
jne 0x7f65f
xorl %r9d, %r9d
cmpw $0x1, %bx
movl $0x0, %eax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
cmovneq %r9, %rax
addq %rax, 0x28(%rsp)
jmp 0x7f787
movl $0x0, 0xc(%rsp)
movq %r11, %r9
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rsi
cmpq %rax, %rsi
jl 0x7f7b5
movq %r9, %rbx
subq %rax, %rbx
setle 0x40(%rsp)
cmpq %rax, %rsi
setg 0x17(%rsp)
movzwl %dx, %eax
movl %eax, 0x48(%rsp)
movb 0x17(%rsp), %al
testb %al, 0x40(%rsp)
jne 0x7f6b7
movq 0xb8(%rsp), %rax
xorq %rax, %r9
xorq %rax, %rsi
orq %r9, %rsi
jne 0x7f712
cmpb $0x0, 0x38(%rsp)
movl $0x0, %eax
cmovneq %r10, %rax
cmpb $0x0, 0x18(%rsp)
movl $0x0, %esi
cmoveq %rsi, %rax
cmpl $0x4000, 0x48(%rsp)     # imm = 0x4000
cmovbq %rsi, %rax
btl $0xe, %edx
cmovaeq %rsi, %rbx
addq 0x28(%rsp), %rbx
addq %rax, %rbx
movq 0xc0(%rsp), %rax
movq %r12, 0x88(%rax)
movq %r13, 0x80(%rax)
movq %rbp, 0x70(%rax)
movq %r14, 0x68(%rax)
movq %rbx, 0x30(%rsp)
cmpb $0x0, 0x38(%rsp)
je 0x7f7b5
cmpl $0x4000, 0x48(%rsp)     # imm = 0x4000
movl $0x0, %eax
cmovbq %rax, %r10
cmpb $0x0, 0x18(%rsp)
cmoveq %rax, %r10
movq 0x28(%rsp), %r14
addq %r10, %r14
cmpq 0x50(%rsp), %r8
movq 0xa8(%rsp), %rsi
movq 0x30(%rsp), %rax
jge 0x7f380
jmp 0x7f7ba
movl $0x0, 0xc(%rsp)
cmpw $0x3, %bx
jne 0x7f667
cmpq %rax, %r15
jne 0x7f667
addq 0x40(%rsp), %r9
movq %r9, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq %rsi, %r9
jmp 0x7f5b5
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 0x7f7ba
movq 0x30(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b5807(%rip), %rdi    # 0x234fd7
leaq 0x1b1f7c(%rip), %rsi    # 0x231753
leaq 0x1b5929(%rip), %rcx    # 0x235107
movl $0x5d48, %edx           # imm = 0x5D48
callq 0x2110
leaq 0x1b57e8(%rip), %rdi    # 0x234fd7
leaq 0x1b1f5d(%rip), %rsi    # 0x231753
leaq 0x1b590a(%rip), %rcx    # 0x235107
movl $0x5d67, %edx           # imm = 0x5D67
callq 0x2110