// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq 0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x58(%rbp)
movq 0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, %eax
movq %rax, -0x178(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
addq -0x178(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
addq $0x1, %rax
subq -0x178(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x180(%rbp), %rax
jge 0x6ec01
movq -0x180(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x188(%rbp), %rax
jle 0x6ec19
movq -0x188(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x6ec30
movq $-0x1, -0x8(%rbp)
jmp 0x6f2fe
movq -0x18(%rbp), %rax
cmpq -0x180(%rbp), %rax
jl 0x6ec4a
movq -0x18(%rbp), %rax
movq %rax, -0x1c0(%rbp)
jmp 0x6ec58
movq -0x180(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x188(%rbp), %rax
jg 0x6ec7d
movq -0x20(%rbp), %rax
movq %rax, -0x1c8(%rbp)
jmp 0x6ec8b
movq -0x188(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x6ecad
movq $-0x1, -0x8(%rbp)
jmp 0x6f2fe
jmp 0x6ecaf
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x48(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0xc0(%rbp)
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x154(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x150(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x160(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x15c(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x144(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x148(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x6edbe
movq $-0x1, -0x8(%rbp)
jmp 0x6f2fe
movq -0xc8(%rbp), %rax
movq %rax, -0x190(%rbp)
cmpq $0x0, -0x60(%rbp)
jl 0x6ede0
movq -0x60(%rbp), %rax
cmpq -0x190(%rbp), %rax
jle 0x6edfe
movq $0x0, -0x60(%rbp)
movslq -0x154(%rbp), %rax
movq %rax, -0x50(%rbp)
movslq -0x160(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x6ee00
xorl %eax, %eax
cmpq $0x0, -0x60(%rbp)
movb %al, -0x1c9(%rbp)
jle 0x6ee20
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
setge %al
movb %al, -0x1c9(%rbp)
movb -0x1c9(%rbp), %al
testb $0x1, %al
jne 0x6ee2f
jmp 0x6eeb3
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x198(%rbp)
cmpq $0x2, -0x198(%rbp)
je 0x6ee82
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rcx
movq -0x50(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x3, -0x198(%rbp)
je 0x6eeae
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
jmp 0x6ee00
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0x190(%rbp), %rcx
movb %al, -0x1ca(%rbp)
jge 0x6eeef
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x1ca(%rbp)
movb -0x1ca(%rbp), %al
testb $0x1, %al
jne 0x6eefe
jmp 0x6f0d7
movq -0x50(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x198(%rbp)
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x1a0(%rbp)
cmpq $0x2, -0x198(%rbp)
je 0x6ef5a
movq -0x1a0(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x3, -0x198(%rbp)
je 0x6ef73
movq -0x1a0(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x1a8(%rbp), %rax
jl 0x6efac
movq -0x18(%rbp), %rax
movq %rax, -0x1d8(%rbp)
jmp 0x6efba
movq -0x1a8(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x1b0(%rbp), %rax
jg 0x6efdf
movq -0x20(%rbp), %rax
movq %rax, -0x1e0(%rbp)
jmp 0x6efed
movq -0x1b0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jle 0x6f013
movq -0x70(%rbp), %rax
subq -0x68(%rbp), %rax
movq %rax, -0x1e8(%rbp)
jmp 0x6f01e
xorl %eax, %eax
movq %rax, -0x1e8(%rbp)
jmp 0x6f01e
movq -0x1e8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x2, -0x198(%rbp)
jne 0x6f068
movq -0x1a8(%rbp), %rax
cmpq -0x1b0(%rbp), %rax
jne 0x6f068
movq -0x1a8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x6f068
movq -0x1a8(%rbp), %rax
cmpq -0x20(%rbp), %rax
jg 0x6f068
movq $0x1, -0x78(%rbp)
cmpq $0x0, -0x1b8(%rbp)
je 0x6f07a
movq $0x1, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x6f086
jmp 0x6eec9
cmpq $0x2, -0x198(%rbp)
je 0x6f0bd
movq -0x1b8(%rbp), %rcx
cmpq $0x0, -0x198(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
addq -0x90(%rbp,%rax,8), %rcx
movq %rcx, -0x90(%rbp,%rax,8)
jmp 0x6f0d2
movq -0x1a0(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x6eec9
jmp 0x6f0d9
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0x190(%rbp), %rcx
movb %al, -0x1e9(%rbp)
jge 0x6f0ff
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
setle %al
movb %al, -0x1e9(%rbp)
movb -0x1e9(%rbp), %al
testb $0x1, %al
jne 0x6f10e
jmp 0x6f235
movq -0x50(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x198(%rbp)
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x1a0(%rbp)
cmpq $0x2, -0x198(%rbp)
je 0x6f160
jmp 0x6f235
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0x190(%rbp), %rcx
movb %al, -0x1ea(%rbp)
jge 0x6f1a9
movq -0x198(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq -0x60(%rbp), %rdx
movzwl (%rcx,%rdx,2), %ecx
sarl $0xe, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
sete %al
movb %al, -0x1ea(%rbp)
movb -0x1ea(%rbp), %al
testb $0x1, %al
jne 0x6f1b5
jmp 0x6f1e7
movq -0xb8(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
addq -0x1a0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x6f16c
movq -0x1a0(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x1a8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x6f230
movq -0x1a8(%rbp), %rax
cmpq -0x20(%rbp), %rax
jg 0x6f230
movq -0x1a0(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x6f0d9
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq 0x10(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
subq -0x28(%rbp), %rax
movq -0x20(%rbp), %rcx
subq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jge 0x6f2bb
movq -0x30(%rbp), %rax
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm0
movq -0x20(%rbp), %rax
subq -0x18(%rbp), %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
cvtsi2sdq -0x38(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x98(%rbp)
movsd -0x40(%rbp), %xmm1
cvtsi2sdq -0x98(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x98(%rbp), %rax
jg 0x6f2f6
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6f2fe
movq $-0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopl (%rax,%rax)