// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rcx, -0x28(%rsp)
movl 0x4(%rdi), %ecx
movl 0x8(%rdi), %r8d
incl %r8d
movq %r8, -0x10(%rsp)
subq %rcx, %r8
xorl %eax, %eax
cmpq %rsi, %r8
jl 0x994f6
cvtsi2sd %rdx, %xmm2
cvtsi2sd %r8, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
cvttsd2si %xmm3, %r9
cmovbeq %rdx, %r9
cmpq %rsi, %r9
setl %dl
cmpq %r9, %r8
setl %r8b
orb %dl, %r8b
jne 0x994f6
movq 0x48(%rdi), %rdx
movq %rdx, -0x30(%rsp)
testq %rdx, %rdx
jle 0x994f6
movq %r9, -0x8(%rsp)
leaq (%r9,%rcx), %rax
movq %rdi, -0x38(%rsp)
movq 0x58(%rdi), %r9
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r9, -0x20(%rsp)
movq %r8, %rdi
shlq $0x5, %rdi
movslq (%r9,%rdi), %rdx
cmpq %rdx, %rax
jle 0x9933a
movq %r8, -0x18(%rsp)
leaq (%r9,%rdi), %r8
cmpw $0x7fff, 0x14(%r8)      # imm = 0x7FFF
jne 0x99229
movl 0x1c(%r8), %r10d
testl %r10d, %r10d
jne 0x99237
cmpw $0x0, 0x12(%r8)
jns 0x99322
movslq 0x4(%r8), %rdi
subq %rdx, %rdi
addq %rdi, %r15
incq %r15
cmpq -0x28(%rsp), %r15
jle 0x99322
leaq (%r15,%r12), %rdx
cmpq %rsi, %rdx
jle 0x99322
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x99322
jmp 0x994f1
movl 0x1c(%r9,%rdi), %r10d
testl %r10d, %r10d
je 0x99322
movl 0x18(%r9,%rdi), %r13d
addq %r13, %r13
movq -0x38(%rsp), %rdi
addq 0x70(%rdi), %r13
xorl %ebp, %ebp
movzwl (%r13,%rbp,2), %r8d
movl %r8d, %edi
shrl $0xe, %edi
movl %r8d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
incl %ebp
cmpl %ebp, %r10d
jbe 0x992a0
movq %rsi, %r9
movl %ebp, %ebx
leaq (,%rbx,2), %rbx
addq %r13, %rbx
movzwl (%rbx), %esi
movl %esi, %r14d
shrl $0xe, %r14d
cmpl %edi, %r14d
jne 0x9929d
andl $0x3fff, %esi           # imm = 0x3FFF
addl %esi, %r11d
incl %ebp
addq $0x2, %rbx
cmpl %ebp, %r10d
jne 0x99277
movl %r10d, %ebp
movq %r9, %rsi
movl %r11d, %ebx
leaq (%rdx,%rbx), %r11
cmpw $0x2, %di
movq %r11, %rdi
cmoveq %rdx, %rdi
movzwl %r8w, %r8d
cmpl $0x3fff, %r8d           # imm = 0x3FFF
ja 0x992de
cmpq %rcx, %rdx
cmovleq %rcx, %rdx
cmpq %r11, %rax
cmovlq %rax, %r11
subq %rdx, %r11
movl $0x0, %edx
cmovleq %rdx, %r11
addq %r11, %r12
jmp 0x992e1
addq %rbx, %r15
cmpq -0x28(%rsp), %r15
jle 0x9930f
leaq (%r15,%r12), %rdx
cmpq %rsi, %rdx
jle 0x9930f
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
ja 0x994f1
movl %ebp, %ebp
cmpl %r10d, %ebp
jae 0x99322
movq %rdi, %rdx
cmpq %rax, %rdi
jl 0x9924a
movq -0x18(%rsp), %r8
incq %r8
cmpq -0x30(%rsp), %r8
movq -0x20(%rsp), %r9
jne 0x991a6
cmpq $0x0, -0x30(%rsp)
jle 0x99501
movq %rsi, %r14
movq -0x10(%rsp), %r9
subq -0x8(%rsp), %r9
movq -0x38(%rsp), %rax
movq 0x58(%rax), %rcx
xorl %eax, %eax
xorl %r11d, %r11d
xorl %ebx, %ebx
movq %rcx, -0x20(%rsp)
movq -0x30(%rsp), %rsi
leaq -0x1(%rsi), %rdi
movq %rdi, -0x30(%rsp)
shlq $0x5, %rdi
movslq 0x4(%rcx,%rdi), %r12
incq %r12
cmpq %r12, %r9
jge 0x994f6
leaq (%rcx,%rdi), %r8
cmpw $0x7fff, 0x14(%r8)      # imm = 0x7FFF
movq %rsi, -0x18(%rsp)
jne 0x993f5
movl 0x1c(%r8), %edx
testl %edx, %edx
jne 0x99401
cmpw $0x0, 0x12(%r8)
jns 0x994de
movslq (%r8), %rdx
subq %rdx, %r12
addq %r12, %r11
cmpq -0x28(%rsp), %r11
jle 0x994de
leaq (%r11,%rbx), %rdx
cmpq %r14, %rdx
jle 0x994de
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x994de
jmp 0x994f1
movl 0x1c(%rcx,%rdi), %edx
testl %edx, %edx
je 0x994de
movl 0x18(%rcx,%rdi), %r15d
addq %r15, %r15
movq -0x38(%rsp), %rcx
addq 0x70(%rcx), %r15
movl %edx, %edi
decq %rdi
movq -0x10(%rsp), %rcx
movslq %edi, %rdx
movzwl (%r15,%rdx,2), %ebp
movl %ebp, %r13d
shrl $0xe, %r13d
movl %ebp, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
testl %edi, %edi
jle 0x99461
leal -0x1(%rdi), %edx
movzwl (%r15,%rdx,2), %r10d
movl %r10d, %esi
shrl $0xe, %esi
cmpl %r13d, %esi
jne 0x99465
andl $0x3fff, %r10d          # imm = 0x3FFF
addl %r10d, %r8d
decl %edi
jg 0x99439
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x99465
decl %edi
movl %edi, %edx
movl %r8d, %r8d
movq %r12, %rdi
subq %r8, %rdi
cmpw $0x2, %r13w
movq %rdi, %r13
cmoveq %r12, %r13
movzwl %bp, %esi
cmpl $0x3fff, %esi           # imm = 0x3FFF
ja 0x9949f
cmpq %rdi, %r9
cmovgq %r9, %rdi
cmpq %rcx, %r12
cmovgeq %rcx, %r12
subq %rdi, %r12
cmovleq %rax, %r12
addq %r12, %rbx
jmp 0x994a2
addq %r8, %r11
cmpq -0x28(%rsp), %r11
jle 0x994cc
leaq (%r11,%rbx), %rsi
cmpq %r14, %rsi
jle 0x994cc
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
ja 0x994f1
movl %edx, %edi
testl %edi, %edi
js 0x994de
movq %r13, %r12
cmpq %r9, %r13
jg 0x9941c
cmpq $0x1, -0x18(%rsp)
movq -0x20(%rsp), %rcx
jg 0x99368
jmp 0x994f6
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x994f6