// File: /workspace/github_cpp_makefile_star2_O2/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), %r15
shlq $0x5, %rbx
movslq (%r15,%rbx), %r10
leaq (%r10,%rax), %r11
movslq 0x4(%r15,%rbx), %r12
subq %rax, %r12
incq %r12
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r12
cmovgeq %rdx, %r12
movabsq $-0x1, %rax
movq %r12, %rdx
movq %rsi, -0x38(%rsp)
subq %rsi, %rdx
jle 0x3bea6
addq %rbx, %r15
movl 0x1c(%r15), %ebx
testq %rbx, %rbx
je 0x3bea6
movq %rcx, -0x18(%rsp)
movq %rdx, -0x10(%rsp)
movq %r8, -0x8(%rsp)
movq %r9, -0x30(%rsp)
movl 0x4(%r14), %r13d
movl 0x10(%r14), %esi
movl 0x8(%r14), %r14d
movq 0x70(%rdi), %rdx
xorl %ecx, %ecx
cmpl %ebx, %r14d
cmoval 0x8(%r15), %esi
movl 0x18(%r15), %edi
cmoval %ecx, %r14d
leaq (%rdx,%rdi,2), %rdi
cmovaq %r10, %r13
testq %r14, %r14
jle 0x3bd0b
cmpq -0x38(%rsp), %r13
jl 0x3bd0b
movzwl -0x2(%rdi,%r14,2), %edx
decq %r14
movl %edx, %r8d
shrl $0xe, %r8d
andl $0x3fff, %edx           # imm = 0x3FFF
cmpl $0x2, %r8d
movl %edx, %r9d
cmovel %ecx, %r9d
subq %r9, %r13
cmpl $0x3, %r8d
movl $0x0, %r8d
cmovneq %rdx, %r8
subq %r8, %rsi
jmp 0x3bcc8
leaq -0x20(%rsp), %rcx
xorpd %xmm1, %xmm1
movapd %xmm1, -0x8(%rcx)
cmpq %rbx, %r14
jge 0x3bdce
cmpq %r12, %r13
jge 0x3bdce
movzwl (%rdi,%r14,2), %r9d
movl %r9d, %r8d
shrl $0xe, %r8d
movl %r9d, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
cmpl $0x2, %r8d
movq %r10, %rbp
movl $0x0, %r11d
cmoveq %r11, %rbp
sete %cl
addq %r13, %rbp
cmpl $0x3, %r8d
movq %r10, %rdx
cmoveq %r11, %rdx
movq -0x38(%rsp), %r11
cmpq %r13, %r11
movq %r13, %r15
cmovgq %r11, %r15
movq %r12, %r11
cmpq %rbp, %r12
jl 0x3bd7d
movq %rbp, %r11
subq %r15, %r11
movl $0x0, %eax
cmovleq %rax, %r11
cmpq -0x38(%rsp), %r13
setge %r15b
testq %r11, %r11
jne 0x3bd9c
andb %r15b, %cl
je 0x3bdc0
xorl %ecx, %ecx
cmpw $0x4000, %r9w           # imm = 0x4000
setae %cl
cmpl $0x2, %r8d
leaq -0x28(%rsp,%rcx,8), %rcx
leaq -0x20(%rsp), %r8
cmoveq %r8, %rcx
cmoveq %r10, %r11
addq %r11, (%rcx)
addq %rdx, %rsi
incq %r14
movq %rbp, %r13
jmp 0x3bd19
movq -0x20(%rsp), %rdx
xorl %r8d, %r8d
cmpq %rbx, %r14
jge 0x3be3a
cmpq %r12, %r13
jg 0x3be3a
movzwl (%rdi,%r14,2), %ebp
movl %ebp, %ecx
andl $0xc000, %ecx           # imm = 0xC000
cmpl $0x8000, %ecx           # imm = 0x8000
jne 0x3be3a
andl $0x3fff, %ebp           # imm = 0x3FFF
incq %r14
cmpq %rbx, %r14
jge 0x3be26
movzwl (%rdi,%r14,2), %ecx
movl %ecx, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
jne 0x3be29
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, %rbp
jmp 0x3bdfb
movq %rbx, %r14
addq %rbp, %rsi
cmpq -0x38(%rsp), %r13
cmovlq %r8, %rbp
addq %rbp, %rdx
jmp 0x3bdd6
movq 0x38(%rsp), %rcx
movl %r13d, 0x4(%rcx)
movl %esi, 0x10(%rcx)
movl %r14d, 0x8(%rcx)
andl $0x0, 0x14(%rcx)
movq -0x8(%rsp), %rax
subq -0x18(%rsp), %rax
movq -0x10(%rsp), %rcx
cmpq %rcx, %rax
jge 0x3be82
cvtsi2sd %rax, %xmm1
cvtsi2sd %rcx, %xmm2
cvtsi2sdq -0x30(%rsp), %xmm3
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm3
cvttsd2si %xmm3, %rax
jmp 0x3be87
movq -0x30(%rsp), %rax
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq %rax, %rdx
movabsq $-0x1, %rax
cmovleq %rdx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq