// File: /workspace/github_cpp_makefile_star2_O1/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), %r12
leaq (%r12,%rax), %r10
movslq 0x4(%r11,%rbx), %r15
subq %rax, %r15
incq %r15
cmpq %rsi, %r10
cmovgq %r10, %rsi
cmpq %rdx, %r15
cmovgeq %rdx, %r15
movq $-0x1, %rax
movq %r15, -0x40(%rsp)
movq %r15, %rdx
movq %rsi, -0x48(%rsp)
subq %rsi, %rdx
jle 0x6dadd
addq %rbx, %r11
movl 0x1c(%r11), %ebx
testq %rbx, %rbx
je 0x6dadd
movq %rdx, -0x20(%rsp)
movq %r8, -0x18(%rsp)
movq %r9, -0x10(%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 %ebx, %edx
cmovbel %edx, %edi
cmoval 0x8(%r11), %eax
cmovaq %r12, %r10
testl %edi, %edi
je 0x6d8ef
cmpq -0x48(%rsp), %r10
movq -0x40(%rsp), %rsi
jl 0x6d8f4
leaq (%r14,%r15,2), %rdx
addq $-0x2, %rdx
xorl %r8d, %r8d
movq %rdi, %r13
movzwl (%rdx,%rdi,2), %r9d
movl %r9d, %r11d
shrl $0xe, %r11d
andl $0x3fff, %r9d           # imm = 0x3FFF
cmpl $0x2, %r11d
movl %r9d, %r12d
cmovel %r8d, %r12d
decq %rdi
subq %r12, %r10
cmpl $0x3, %r11d
movl $0x0, %r11d
cmovneq %r9, %r11
subq %r11, %rax
cmpq $0x2, %r13
jl 0x6d8f4
cmpq -0x48(%rsp), %r10
jge 0x6d8a8
jmp 0x6d8f4
movq -0x40(%rsp), %rsi
leaq (%r14,%r15,2), %r14
xorpd %xmm1, %xmm1
movapd %xmm1, -0x38(%rsp)
cmpq %rbx, %rdi
movq %rcx, -0x8(%rsp)
jge 0x6d9c9
cmpq %rsi, %r10
jge 0x6d9c9
xorl %ebp, %ebp
movzwl (%r14,%rdi,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
movl %r8d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl $0x2, %r9d
movq %r15, %r13
cmoveq %rbp, %r13
sete %cl
addq %r10, %r13
cmpl $0x3, %r9d
movq %r15, %rdx
cmoveq %rbp, %rdx
movq -0x48(%rsp), %rsi
cmpq %r10, %rsi
movq %r10, %r12
cmovgq %rsi, %r12
movq -0x40(%rsp), %rsi
movq %rsi, %r11
cmpq %r13, %rsi
jl 0x6d96c
movq %r13, %r11
subq %r12, %r11
cmovleq %rbp, %r11
cmpq -0x48(%rsp), %r10
setge %r10b
testq %r11, %r11
jne 0x6d986
andb %r10b, %cl
je 0x6d9b0
movzwl %r8w, %ecx
xorl %r8d, %r8d
cmpl $0x4000, %ecx           # imm = 0x4000
setae %r8b
cmpl $0x2, %r9d
leaq -0x38(%rsp,%r8,8), %rcx
leaq -0x30(%rsp), %r8
cmoveq %r8, %rcx
cmoveq %r15, %r11
addq %r11, (%rcx)
addq %rdx, %rax
incq %rdi
cmpq %rbx, %rdi
jge 0x6d9cc
movq %r13, %r10
cmpq %rsi, %r13
jl 0x6d91b
jmp 0x6d9cc
movq %r10, %r13
movq -0x30(%rsp), %r12
cmpq %rbx, %rdi
jge 0x6da63
cmpq %rsi, %r13
movq -0x10(%rsp), %r9
movq -0x18(%rsp), %r10
movq 0x38(%rsp), %r11
movq -0x20(%rsp), %r15
jg 0x6da77
xorl %ecx, %ecx
movzwl (%r14,%rdi,2), %ebp
movl %ebp, %edx
andl $0xc000, %edx           # imm = 0xC000
cmpl $0x8000, %edx           # imm = 0x8000
jne 0x6da77
andl $0x3fff, %ebp           # imm = 0x3FFF
incq %rdi
cmpq %rbx, %rdi
jge 0x6da48
movzwl (%r14,%rdi,2), %edx
movl %edx, %r8d
andl $0xc000, %r8d           # imm = 0xC000
cmpl $0x8000, %r8d           # imm = 0x8000
jne 0x6da48
andl $0x3fff, %edx           # imm = 0x3FFF
addq %rdx, %rbp
incq %rdi
cmpq %rdi, %rbx
jne 0x6da1c
movq %rbx, %rdi
addq %rbp, %rax
cmpq -0x48(%rsp), %r13
cmovlq %rcx, %rbp
addq %rbp, %r12
cmpq %rbx, %rdi
jge 0x6da77
cmpq %rsi, %r13
jle 0x6d9f9
jmp 0x6da77
movq -0x10(%rsp), %r9
movq -0x18(%rsp), %r10
movq 0x38(%rsp), %r11
movq -0x20(%rsp), %r15
movq %r12, -0x30(%rsp)
movl %r13d, 0x4(%r11)
movl %eax, 0x10(%r11)
movl %edi, 0x8(%r11)
movl $0x0, 0x14(%r11)
subq -0x8(%rsp), %r10
cmpq %r15, %r10
jge 0x6dab9
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
cvtsi2sd %r15, %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
movq -0x30(%rsp), %rcx
cmpq %rax, %rcx
movq $-0x1, %rax
cmovleq %rcx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq