// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_err(overlap_region*, long, long, long, long, long, double, ul_ov_t*)
int64_t extract_sub_err(overlap_region *z, int64_t s, int64_t e, int64_t os0, int64_t oe0, int64_t err0, double err_sec_rate, ul_ov_t *p)
{
    // fprintf(stderr, "\n[M::%s]\ts::%ld\te::%ld\tset_f::%ld\tovlp_id::%u\twid::%u\n", __func__, s, e, set_f, ovlp_id(*p), ovlp_cur_wid(*p));
    // if((!set_f) && (!ovlp_cur_ylen(*p))) return 1;///no potential informative site
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, t, l[2], err;
    bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0; 
    s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd; 
    e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
    if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
    if(s >= e) return -1;
    os = MAX(s, s0); oe = MIN(e, e0);
    if(oe <= os) return -1;

    set_bit_extz_t(ez, (*z), wk);
    if(!ez.cigar.n) return -1;
    int64_t cn = ez.cigar.n, op, ol; int64_t ws, we, ovlp; 
    if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
        ck = 0; xk = ez.ts; yk = ez.ps;
    }
    
    while (ck > 0 && xk >= s) {///x -> t; y -> p
        --ck;
        op = ez.cigar.a[ck]>>14;
        if(op!=2) xk -= (ez.cigar.a[ck]&(0x3fff));
        if(op!=3) yk -= (ez.cigar.a[ck]&(0x3fff)); 
    }

    // if(is_dbg) fprintf(stderr, "---0---[M::%s] set_f::%ld, yk::%ld\n", __func__, set_f, yk);
     //some cigar will span s or e
    l[0] = l[1] = 0;
    while (ck < cn && xk < e) {//[s, e)
        ws = xk; 
        op = ez.cigar.a[ck]>>14;
        ol = (ez.cigar.a[ck]&(0x3fff));
        if(op!=2) xk += ol;
        if(op!=3) yk += ol; 
        ck++; we = xk; t= 0;
        // if(op != 0 && op != 1) continue;///only collect match/snp

        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);
        if((op==2) && (ws == we) && ((ws>=s) && (ws<=e))) t = 1;
        if(ovlp) t = 1;
        if(!t) continue;

        if(op!=2) {
            l[(!!op)] += ovlp;
        } else {
            l[1] += ol;
        }
    }

    while (ck < cn && xk <= e) {//[s, e)
        ws = xk; 
        op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
        if(op != 2) break;

        for (ck++; (ck < cn) && (op == (ez.cigar.a[ck]>>14)); ck++) {
            ol += (ez.cigar.a[ck]&(0x3fff));
        }
        yk += ol; we = xk; 
        
        if(ws >= s && ws <= e) l[1] += ol;
    }
    // if(is_dbg) fprintf(stderr, "---1---[M::%s] set_f::%ld, yk::%ld\n", __func__, set_f, yk);

    ovlp_cur_xoff(*p) = xk; ovlp_cur_yoff(*p) = yk; 
    ovlp_cur_coff(*p) = ck; ovlp_cur_ylen(*p) = 0;

    // fprintf(stderr, "[M::%s] cid::%u\trid::%u\t%.*s\tq::[%u,%u)\terr::%u\n", __func__, c_idx->a[zi].ts, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), c_idx->a[zi].qs, c_idx->a[zi].qe, c_idx->a[zi].qn);

    

    err = err0;
    if((oe0 - os0) < (e - s)) {
        err = (((double)(oe0 - os0))/((double)(e - s)))*err0; 
    }
    err *= err_sec_rate;
    // fprintf(stderr, "[M::%s]\t%.*s\tl[0]::%ld\tl[1]::%ld\tthres::%ld\n", __func__, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), l[0], l[1], err);
    if(l[1] <= err) return l[1];
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x38(%rsp), %r14
movl (%r14), %ebx
movl 0x18(%r14), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r11
shlq $0x5, %rbx
movslq (%r11,%rbx), %r13
leaq (%rax,%r13), %r10
movslq 0x4(%r11,%rbx), %r12
subq %rax, %r12
incq %r12
cmpq %rsi, %r10
cmovgq %r10, %rsi
cmpq %rdx, %r12
cmovgeq %rdx, %r12
movq $-0x1, %rax
movq %r12, %rdx
subq %rsi, %rdx
jle 0x6d590
movq %r9, %rbp
addq %rbx, %r11
movl 0x1c(%r11), %r9d
testq %r9, %r9
je 0x6d590
movq %rdx, -0x8(%rsp)
movl 0x4(%r14), %r10d
movl 0x10(%r14), %eax
movl 0x8(%r14), %edx
movq 0x70(%rdi), %r14
movl 0x18(%r11), %r15d
xorl %edi, %edi
cmpl %r9d, %edx
cmovbel %edx, %edi
cmoval 0x8(%r11), %eax
cmovaq %r13, %r10
testl %edi, %edi
movq %rbp, -0x38(%rsp)
movq %rcx, -0x10(%rsp)
je 0x6d3a1
cmpq %rsi, %r10
jl 0x6d3a1
leaq (%r14,%r15,2), %rdx
addq $-0x2, %rdx
xorl %ebp, %ebp
movq %rdi, %rcx
movzwl (%rdx,%rdi,2), %r13d
movl %r13d, %r11d
shrl $0xe, %r11d
andl $0x3fff, %r13d          # imm = 0x3FFF
cmpl $0x2, %r11d
movl %r13d, %ebx
cmovel %ebp, %ebx
decq %rdi
subq %rbx, %r10
cmpl $0x3, %r11d
movl $0x0, %r11d
cmovneq %r13, %r11
subq %r11, %rax
cmpq $0x2, %rcx
jb 0x6d3a1
cmpq %rsi, %r10
jge 0x6d35f
leaq (%r14,%r15,2), %rcx
movq %rcx, -0x30(%rsp)
xorl %edx, %edx
xorpd %xmm1, %xmm1
movapd %xmm1, -0x28(%rsp)
movq %r9, %r14
cmpq %r9, %rdi
jge 0x6d492
cmpq %r12, %r10
movq -0x38(%rsp), %r9
jge 0x6d48d
movq %r8, -0x18(%rsp)
xorl %ebp, %ebp
movq -0x30(%rsp), %rcx
movzwl (%rcx,%rdi,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
movl %r8d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl $0x2, %r9d
movq %r15, %r11
cmoveq %rbp, %r11
sete %cl
addq %r10, %r11
cmpl $0x3, %r9d
movq %r15, %rdx
cmoveq %rbp, %rdx
cmpq %r10, %rsi
movq %r10, %r13
cmovgq %rsi, %r13
movq %r12, %rbx
cmpq %r11, %r12
jl 0x6d423
movq %r11, %rbx
subq %r13, %rbx
cmovleq %rbp, %rbx
cmpq %rsi, %r10
setge %r10b
testq %rbx, %rbx
jne 0x6d43b
andb %r10b, %cl
je 0x6d465
movzwl %r8w, %ecx
xorl %r8d, %r8d
cmpl $0x4000, %ecx           # imm = 0x4000
setae %r8b
cmpl $0x2, %r9d
leaq -0x28(%rsp,%r8,8), %rcx
leaq -0x20(%rsp), %r8
cmoveq %r8, %rcx
cmoveq %r15, %rbx
addq %rbx, (%rcx)
addq %rdx, %rax
incq %rdi
cmpq %r14, %rdi
jge 0x6d47c
movq %r11, %r10
cmpq %r12, %r11
jl 0x6d3d7
movq -0x20(%rsp), %rdx
movq -0x38(%rsp), %r9
movq -0x18(%rsp), %r8
jmp 0x6d49a
movq %r10, %r11
jmp 0x6d49a
movq %r10, %r11
movq -0x38(%rsp), %r9
cmpq %r14, %rdi
jge 0x6d52d
cmpq %r12, %r11
jg 0x6d52d
xorl %ebx, %ebx
movq -0x30(%rsp), %rcx
movzwl (%rcx,%rdi,2), %r13d
movl %r13d, %ecx
andl $0xc000, %ecx           # imm = 0xC000
cmpl $0x8000, %ecx           # imm = 0x8000
jne 0x6d52d
andl $0x3fff, %r13d          # imm = 0x3FFF
incq %rdi
cmpq %r14, %rdi
jge 0x6d507
movq -0x30(%rsp), %rcx
movzwl (%rcx,%rdi,2), %ecx
movl %ecx, %r10d
andl $0xc000, %r10d          # imm = 0xC000
cmpl $0x8000, %r10d          # imm = 0x8000
jne 0x6d507
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, %r13
incq %rdi
cmpq %rdi, %r14
jne 0x6d4d8
jmp 0x6d51b
addq %r13, %rax
cmpq %rsi, %r11
cmovlq %rbx, %r13
addq %r13, %rdx
cmpq %r14, %rdi
jl 0x6d4ae
jmp 0x6d52d
addq %r13, %rax
xorl %ecx, %ecx
cmpq %rsi, %r11
cmovgeq %r13, %rcx
addq %rcx, %rdx
movq %r14, %rdi
movq 0x38(%rsp), %rcx
movl %r11d, 0x4(%rcx)
movl %eax, 0x10(%rcx)
movl %edi, 0x8(%rcx)
movl $0x0, 0x14(%rcx)
subq -0x10(%rsp), %r8
movq -0x8(%rsp), %rax
cmpq %rax, %r8
jge 0x6d571
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
cvtsi2sd %rax, %xmm2
cvtsi2sd %r9, %xmm3
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm3
cvttsd2si %xmm3, %r9
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq %rax, %rdx
movq $-0x1, %rax
cmovleq %rdx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq