// File: /workspace/github_cpp_makefile_star2_O2/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, 0x40(%rsp)
movq %rdi, %r14
movq 0xb0(%rsp), %rsi
movq 0xa0(%rsp), %rdi
movq 0x80(%rsi), %rcx
cmpq 0x90(%rsp), %rcx
jl 0x4bfe6
cmpq %rdi, 0x70(%rsi)
jl 0x4bfe6
movq 0x80(%rsp), %rax
decq %rdx
movq %rdx, 0x88(%rsi)
movq %r8, 0x80(%rsi)
movq %rax, 0x70(%rsi)
andq $0x0, 0x68(%rsi)
movq %r8, %rcx
movl 0x2c(%r14), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl 0x28(%r14), %edx
jg 0x4c402
movq 0x98(%rsp), %r8
cmpq $0x0, 0xa8(%r14)
je 0x4c3d9
addq $0xa8, %r14
movq 0x88(%rsi), %rax
movq 0x68(%rsi), %r15
movq 0x70(%rsi), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rsi, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x14(%rsp)
movq %rdx, %r13
movq %rcx, %rbx
movq %rax, %rbp
cmpq 0x40(%rsp), %rax
jl 0x4c3d2
movq %r8, %r12
movq %r14, %rdi
movl %ebp, %esi
leaq 0x26(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x62f1e
movzwl 0x26(%rsp), %esi
cmpl $0x1, %esi
ja 0x4c08a
movl 0x8(%rsp), %edi
movq %rbx, %rcx
subq %rdi, %rcx
movq %r13, %rdx
subq %rdi, %rdx
movq %r12, %r8
jmp 0x4c0b9
cmpl $0x3, %esi
movq %r12, %r8
je 0x4c0ac
movq %r13, %rdx
movq %rbx, %rcx
cmpl $0x2, %esi
jne 0x4c0b9
movl 0x8(%rsp), %edx
movq %rbx, %rcx
subq %rdx, %rcx
movq %r13, %rdx
jmp 0x4c0b9
movl 0x8(%rsp), %ecx
movq %r13, %rdx
subq %rcx, %rdx
movq %rbx, %rcx
cmpq %r8, %r13
jl 0x4c3d2
cltq
cmpq %rdx, 0xa0(%rsp)
jge 0x4c0e5
testl %esi, %esi
movl 0x8(%rsp), %esi
movl $0x0, %edi
cmovneq %rsi, %rdi
addq %rdi, %r15
jmp 0x4c040
movb $0x1, %dil
movl %edi, 0x10(%rsp)
movw $0x1, %di
movl %edi, 0x34(%rsp)
movq %r13, %r11
movq %r15, 0x18(%rsp)
movb $0x1, %dil
movl %edi, 0xc(%rsp)
cmpw $0x0, 0x14(%rsp)
movw $0x1, %di
movl %edi, 0x14(%rsp)
jne 0x4c20a
movq 0xa0(%rsp), %r9
movq %r9, %r8
subq %rdx, %r8
setle %dil
cmpq %r9, %r13
setl %r9b
orb %dil, %r9b
movb $0x1, %r9b
je 0x4c13d
movq %r15, %rdi
jmp 0x4c186
cmpl $0x2, %esi
je 0x4c411
cmpw $0x1, %si
ja 0x4c169
leaq (%rcx,%r8), %r11
movq %r15, %rdi
cmpq 0x90(%rsp), %r11
jne 0x4c186
xorl %r9d, %r9d
cmpl $0x1, %esi
cmovneq %r9, %r8
jmp 0x4c183
cmpl $0x3, %esi
setne %dil
cmpq %rcx, 0x90(%rsp)
setne %r9b
orb %dil, %r9b
cmovneq %r15, %r8
movq %r8, %rdi
cmpl $0x2, %esi
jne 0x4c1cb
cmpq 0xa0(%rsp), %r13
jl 0x4c1cb
movq 0x90(%rsp), %r8
subq %rcx, %r8
movq %r8, 0x18(%rsp)
jl 0x4c1cb
movl 0x8(%rsp), %r11d
addq %rcx, %r11
cmpq 0x90(%rsp), %r11
jle 0x4c1cb
movl $0x0, 0xc(%rsp)
movq 0xa0(%rsp), %r11
jmp 0x4c1ff
testb %r9b, %r9b
je 0x4c1ea
movb $0x1, %r8b
movl %r8d, 0xc(%rsp)
movl $0x0, 0x14(%rsp)
movq %r13, %r11
movq %rdi, 0x18(%rsp)
jmp 0x4c207
movl $0x0, 0xc(%rsp)
movq 0xa0(%rsp), %r11
movq %rdi, 0x18(%rsp)
movw $0x1, %di
movl %edi, 0x14(%rsp)
movq %r12, %r8
cmpw $0x0, 0x28(%rsp)
je 0x4c21a
movq %rdx, %r9
jmp 0x4c29c
movq %r8, %r9
subq %rdx, %r9
setl %r10b
movq %r13, %rdi
subq %r8, %rdi
movq %rdi, 0x28(%rsp)
setle %dil
orb %r10b, %dil
movb $0x1, %r10b
je 0x4c370
cmpl $0x2, %esi
jne 0x4c27b
cmpq %rdx, %r8
jl 0x4c27b
cmpq 0x88(%rsp), %rcx
jg 0x4c27b
movl 0x8(%rsp), %edi
addq %rcx, %rdi
cmpq 0x88(%rsp), %rdi
jle 0x4c27b
movq 0x18(%rsp), %r9
subq 0x88(%rsp), %r9
addq %rdi, %r9
movq %r9, 0x18(%rsp)
jmp 0x4c291
testb %r10b, %r10b
je 0x4c291
movb $0x1, %dil
movl %edi, 0x10(%rsp)
movl $0x0, 0x34(%rsp)
jmp 0x4c212
movl $0x0, 0x10(%rsp)
movq %r8, %r9
movq 0xa8(%rsp), %r8
cmpq %r8, %r11
jl 0x4c3d2
movq %r9, %rdi
subq %r8, %rdi
setle %r10b
cmpq %r8, %r11
setg %r8b
testb %r8b, %r10b
jne 0x4c2d6
movq 0xa8(%rsp), %r8
xorq %r8, %r9
xorq %r8, %r11
orq %r9, %r11
jne 0x4c334
movl 0x8(%rsp), %r8d
cmpb $0x0, 0x10(%rsp)
movl $0x0, %r10d
cmoveq %r10, %r8
cmpb $0x0, 0xc(%rsp)
cmoveq %r10, %r8
testl %esi, %esi
cmoveq %r10, %r8
movl %esi, %r9d
andl $-0x3, %r9d
cmpw $0x1, %r9w
cmovneq %r10, %rdi
addq 0x18(%rsp), %rdi
addq %r8, %rdi
movq 0xb0(%rsp), %r8
movq %rbp, 0x88(%r8)
movq %rbx, 0x80(%r8)
movq %r13, 0x70(%r8)
movq %r15, 0x68(%r8)
movq %rdi, 0x38(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x4c3d2
testl %esi, %esi
movl 0x8(%rsp), %esi
movl $0x0, %edi
cmoveq %rdi, %rsi
cmpb $0x0, 0xc(%rsp)
cmoveq %rdi, %rsi
movq 0x18(%rsp), %r15
addq %rsi, %r15
movl 0x34(%rsp), %esi
movq %rsi, 0x28(%rsp)
movq %r12, %r8
jmp 0x4c040
cmpl $0x2, %esi
je 0x4c430
cmpw $0x1, %si
ja 0x4c3a9
addq %rcx, %r9
cmpq 0x88(%rsp), %r9
jne 0x4c23f
xorl %r10d, %r10d
cmpl $0x1, %esi
movq 0x28(%rsp), %rdi
cmovneq %r10, %rdi
addq %rdi, 0x18(%rsp)
jmp 0x4c23f
cmpl $0x3, %esi
jne 0x4c23f
cmpq 0x88(%rsp), %rcx
jne 0x4c23f
movq 0x28(%rsp), %rdi
addq %rdi, 0x18(%rsp)
xorl %r10d, %r10d
jmp 0x4c23f
movq 0x38(%rsp), %rax
jmp 0x4c402
movq %rdi, %rcx
subq 0xa8(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
subq %r8, %rdi
cvtsi2sd %rdi, %xmm1
cvtsi2sd %edx, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19ab0f(%rip), %rdi    # 0x1e6f27
leaq 0x197284(%rip), %rsi    # 0x1e36a3
leaq 0x19ac31(%rip), %rcx    # 0x1e7057
movl $0x5d48, %edx           # imm = 0x5D48
callq 0x2110
leaq 0x19aaf0(%rip), %rdi    # 0x1e6f27
leaq 0x197265(%rip), %rsi    # 0x1e36a3
leaq 0x19ac12(%rip), %rcx    # 0x1e7057
movl $0x5d67, %edx           # imm = 0x5D67
callq 0x2110