// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: ff_tend(overlap_region*, long, long, double, double, long)
uint32_t inline ff_tend(overlap_region *z, int64_t wn, int64_t dn, double dr, double er, int64_t min_err)
{
    int64_t k, zwn = z->w_list.n, err, mm, qi, ci, cn, ql, ws, we, zs, ze, s, e, os, oe, ovlp; bit_extz_t ez; uint32_t cl; uint16_t c;
    zs = z->x_pos_s; ze = z->x_pos_e + 1; ql = ze - zs; 
    if(ql < wn) return 0; if(dn > (ql*dr)) dn = ql*dr; if(dn < wn) return 0; if(ql < dn) return 0;
    
    s = zs; e = zs + dn;
    // if(z->y_id == 27) fprintf(stderr, "-a-[M::%s] tid::%u\t%.*s\tz::[%ld,%ld)\ti::[%ld,%ld)\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), zs, ze, s, e);
    for (k = err = mm = 0, qi = zs; (k < zwn) && (z->w_list.a[k].x_start < e); k++) {
        // if(z->y_id == 27) fprintf(stderr, "-a-[M::%s] tid::%u\t%.*s\tw::[%d,%d)\terr::%d\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].error);
        if(!(is_ualn_win(z->w_list.a[k]))) {
            set_bit_extz_t(ez, (*z), k); 
            ci = 0; cn = ez.cigar.n; qi = ez.ts; //ti = ez.ps;
            while (ci < cn && qi < e) {
                ws = qi;
                ci = pop_trace(&(ez.cigar), ci, &c, &cl);
                if(c!=2) qi += cl;
                // if(c!=3) ti += cl; 
                we = qi; 

                if(c == 0) {
                    os = MAX(s, ws); oe = MIN(e, we);
                    ovlp = ((oe>os)? (oe-os):0); mm += ovlp;
                } else {
                    err += cl;
                }
                // assert(is_ovlp_debug(s, e, ws, we, c));

                if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                    // fprintf(stderr, "-0-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                    return 1;
                }
            }
        } else {
            ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end + 1;
            err += we - ws;

            // assert(is_ovlp_debug(s, e, ws, we, -1));

            if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                // fprintf(stderr, "-1-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                return 1;
            }
        } 
    }

    s = ze - dn; e = ze;
    for (k = zwn - 1, err = mm = 0, qi = ze; (k >= 0) && ((z->w_list.a[k].x_end + 1) > s); k--) {
        if(!(is_ualn_win(z->w_list.a[k]))) {
            set_bit_extz_t(ez, (*z), k); 
            ci = ((int64_t)ez.cigar.n) - 1; qi = ez.te + 1; //ti = ez.pe + 1;
            while (ci >= 0 && qi > s) {
                we = qi;
                ci = pop_trace_back(&(ez.cigar), ci, &c, &cl);
                if(c!=2) qi -= cl;
                // if(c!=3) ti += cl; 
                ws = qi; 

                if(c == 0) {
                    os = MAX(s, ws); oe = MIN(e, we);
                    ovlp = ((oe>os)? (oe-os):0); mm += ovlp;
                } else {
                    err += cl;
                }

                // assert(is_ovlp_debug(s, e, ws, we, c));

                if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                    // fprintf(stderr, "-2-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                    return 1;
                }
            }
        } else {
            ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end + 1;
            err += we - ws;

            // assert(is_ovlp_debug(s, e, ws, we, -1));

            if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                // fprintf(stderr, "-3-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                return 1;
            }
        } 
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0x4(%rdi), %r9d
movq %rdi, -0x38(%rsp)
movl 0x8(%rdi), %eax
incl %eax
movq %rax, -0x18(%rsp)
subq %r9, %rax
movq $0x0, -0x40(%rsp)
cmpq %rsi, %rax
jl 0x9897b
cvtsi2sd %rdx, %xmm2
cvtsi2sd %rax, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
cvttsd2si %xmm3, %rdi
cmovbeq %rdx, %rdi
cmpq %rsi, %rdi
setl %dl
cmpq %rdi, %rax
setl %al
orb %dl, %al
jne 0x9897b
movq %rdi, -0x20(%rsp)
movq -0x38(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x30(%rsp)
testq %rax, %rax
movq %rsi, -0x28(%rsp)
jle 0x987b7
movq -0x20(%rsp), %rax
leaq (%rax,%r9), %r11
movq -0x38(%rsp), %rax
movq 0x58(%rax), %r8
xorl %r10d, %r10d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r8, -0x10(%rsp)
movq %r10, %rdx
shlq $0x5, %rdx
movslq (%r8,%rdx), %rax
cmpq %rax, %r11
jle 0x987b7
leaq (%r8,%rdx), %rdi
cmpw $0x7fff, 0x14(%rdi)     # imm = 0x7FFF
movq %r10, -0x8(%rsp)
jne 0x98669
cmpl $0x0, 0x1c(%rdi)
jne 0x98669
cmpw $0x0, 0x12(%rdi)
js 0x9877d
movl 0x1c(%r8,%rdx), %r10d
testl %r10d, %r10d
je 0x98763
cmpq %rax, %r11
jle 0x98763
movq -0x38(%rsp), %rdi
movq 0x70(%rdi), %rdi
movl 0x18(%r8,%rdx), %edx
leaq (%rdi,%rdx,2), %rdx
xorl %ebp, %ebp
movzwl (%rdx,%rbp,2), %ebx
movl %ebx, %edi
shrl $0xe, %edi
movl %ebx, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
incl %ebp
cmpl %ebp, %r10d
jbe 0x986e1
movl %ebp, %r8d
leaq (%rdx,%r8,2), %r8
movzwl (%r8), %esi
movl %esi, %r15d
shrl $0xe, %r15d
cmpl %edi, %r15d
jne 0x986dc
andl $0x3fff, %esi           # imm = 0x3FFF
addl %esi, %r14d
incl %ebp
addq $0x2, %r8
cmpl %ebp, %r10d
jne 0x986b5
movl %r10d, %ebp
movq -0x28(%rsp), %rsi
cmpw $0x2, %di
movl %r14d, %r8d
movl $0x0, %edi
cmovneq %r8, %rdi
addq %rax, %rdi
movzwl %bx, %ebx
cmpl $0x3fff, %ebx           # imm = 0x3FFF
ja 0x98721
cmpq %r9, %rax
cmovleq %r9, %rax
cmpq %rdi, %r11
movq %rdi, %r8
cmovlq %r11, %r8
subq %rax, %r8
movl $0x0, %eax
cmovleq %rax, %r8
addq %r8, %r13
jmp 0x98724
addq %r8, %r12
cmpq %rcx, %r12
jle 0x98750
leaq (%r12,%r13), %rax
cmpq %rsi, %rax
jle 0x98750
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
ja 0x98971
movl %ebp, %ebp
cmpl %r10d, %ebp
jae 0x98763
movq %rdi, %rax
cmpq %r11, %rdi
jl 0x98694
movq -0x8(%rsp), %r10
incq %r10
cmpq -0x30(%rsp), %r10
movq -0x10(%rsp), %r8
jne 0x98633
jmp 0x987b7
movslq 0x4(%rdi), %rdx
subq %rax, %rdx
addq %rdx, %r12
incq %r12
cmpq %rcx, %r12
jle 0x98763
leaq (%r12,%r13), %rax
cmpq %rsi, %rax
jle 0x98763
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x98763
jmp 0x98971
cmpq $0x0, -0x30(%rsp)
jle 0x9897b
movq -0x18(%rsp), %r9
subq -0x20(%rsp), %r9
movq -0x38(%rsp), %rax
movq 0x58(%rax), %r10
movq $0x0, -0x40(%rsp)
xorl %eax, %eax
xorl %r11d, %r11d
movq -0x30(%rsp), %rbx
leaq -0x1(%rbx), %r15
movq %r15, -0x30(%rsp)
shlq $0x5, %r15
movslq 0x4(%r10,%r15), %r14
incq %r14
cmpq %r14, %r9
jge 0x9897b
leaq (%r10,%r15), %rdx
cmpw $0x7fff, 0x14(%rdx)     # imm = 0x7FFF
jne 0x98824
cmpl $0x0, 0x1c(%rdx)
jne 0x98824
cmpw $0x0, 0x12(%rdx)
js 0x9893e
movl 0x1c(%r10,%r15), %edi
testq %rdi, %rdi
je 0x9892c
movslq 0x4(%r10,%r15), %rdx
incq %rdx
cmpq %rdx, %r9
jge 0x9892c
movl 0x18(%r10,%r15), %r15d
addq %r15, %r15
movq -0x38(%rsp), %rsi
addq 0x70(%rsi), %r15
decq %rdi
movq %rax, %r14
movslq %edi, %rax
movzwl (%r15,%rax,2), %eax
movl %eax, %ebp
shrl $0xe, %ebp
movl %eax, %r13d
andl $0x3fff, %r13d          # imm = 0x3FFF
testl %edi, %edi
jle 0x9889e
leal -0x1(%rdi), %r12d
movzwl (%r15,%r12,2), %r8d
movl %r8d, %esi
shrl $0xe, %esi
cmpl %ebp, %esi
jne 0x988a3
andl $0x3fff, %r8d           # imm = 0x3FFF
addl %r8d, %r13d
decl %edi
jg 0x98875
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x988a3
decl %edi
movl %edi, %r12d
cmpw $0x2, %bp
movl %r13d, %edi
movl $0x0, %esi
cmovneq %rdi, %rsi
movq %rdx, %r13
subq %rsi, %r13
movzwl %ax, %eax
cmpl $0x3fff, %eax           # imm = 0x3FFF
ja 0x988ea
cmpq %r13, %r9
movq %r13, %rax
cmovgq %r9, %rax
movq -0x18(%rsp), %rsi
cmpq %rsi, %rdx
cmovgeq %rsi, %rdx
subq %rax, %rdx
movl $0x0, %eax
cmovleq %rax, %rdx
addq %rdx, %r11
jmp 0x988ed
addq %rdi, %r14
cmpq %rcx, %r14
jle 0x98917
leaq (%r14,%r11), %rax
cmpq -0x28(%rsp), %rax
jle 0x98917
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
ja 0x98971
movl %r12d, %edi
testl %edi, %edi
js 0x9892f
movq %r13, %rdx
cmpq %r9, %r13
jg 0x9885a
jmp 0x9892f
movq %rax, %r14
movq %r14, %rax
cmpq $0x1, %rbx
jg 0x987e4
jmp 0x9897b
movslq (%rdx), %rdx
subq %rdx, %r14
addq %rax, %r14
cmpq %rcx, %r14
jle 0x9892f
leaq (%r14,%r11), %rax
cmpq -0x28(%rsp), %rax
jle 0x9892f
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x9892f
movl $0x1, %eax
movq %rax, -0x40(%rsp)
movq -0x40(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq