// File: chhylp123[P]hifiasm/Correct.cpp
// Function: get_win_off(long, utg_ct_t*, overlap_region*, double, ul_ov_t*)
int64_t get_win_off(int64_t ql, utg_ct_t *p, overlap_region* o, double o_rate, ul_ov_t *res)
{
    if(!o->w_list.n) return 0;
    int64_t q[2], t[2], wk[2], wq[2], wt[2], k, tot_l, qs, qe, ts, te, os, oe, ovlp, aln_l;
    qs = 0; qe = ql-1; ts = p->s; te = p->e-1; //[qs, qe] && [ps, pe]

    k = 0; memset(res, 0, sizeof((*res)));
    k = get_win_yoff(o, ts, k, qs, qe, ts, te, q, t, &(wq[0]), &(wt[0])); wk[0] = k; 
    k = get_win_yoff(o, te, k, qs, qe, ts, te, q, t, &(wq[1]), &(wt[1])); wk[1] = k;
    res->ts = ts; res->te = te + 1;///offset of t
    res->qs = wk[0]<0?(uint32_t)-1:wk[0]; res->qe = wk[1] + 1;///id of window

    wk[0] = ((wk[0]>=0)?(wk[0]>>1):0);
    wk[1] = ((wk[1]>=0)?(wk[1]>>1):0)+1;
    tot_l = wq[1]+1-wq[0]; aln_l = 0;
    for (k = wk[0]; k < wk[1]; k++) {
        if(is_ualn_win(o->w_list.a[k])) continue;
        os = MAX(wq[0], o->w_list.a[k].x_start); 
        oe = MIN(wq[1], o->w_list.a[k].x_end) + 1;
        ovlp = ((oe>os)? (oe-os):0); aln_l += ovlp;
    }
    if((aln_l < (tot_l*o_rate)) || (aln_l == 0)) return 0;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x8c31b
movq $0x0, -0x18(%rbp)
jmp 0x8c71c
movq $0x0, -0xa8(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x1c, %edx
callq 0x2100
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq -0xb8(%rbp), %r9
movq -0xc0(%rbp), %r14
leaq -0x50(%rbp), %rbx
leaq -0x60(%rbp), %r11
leaq -0x80(%rbp), %r10
leaq -0x90(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x8bdc0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq -0xb8(%rbp), %r9
movq -0xc0(%rbp), %r14
leaq -0x50(%rbp), %rbx
leaq -0x60(%rbp), %r11
leaq -0x80(%rbp), %r10
addq $0x8, %r10
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x8bdc0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xb8(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x14(%rax)
cmpq $0x0, -0x70(%rbp)
jge 0x8c492
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0xe8(%rbp)
jmp 0x8c49d
movq -0x70(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpq $0x0, -0x70(%rbp)
jl 0x8c4d5
movq -0x70(%rbp), %rax
sarq %rax
movq %rax, -0xf0(%rbp)
jmp 0x8c4e0
xorl %eax, %eax
movq %rax, -0xf0(%rbp)
jmp 0x8c4e0
movq -0xf0(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x68(%rbp)
jl 0x8c502
movq -0x68(%rbp), %rax
sarq %rax
movq %rax, -0xf8(%rbp)
jmp 0x8c50d
xorl %eax, %eax
movq %rax, -0xf8(%rbp)
jmp 0x8c50d
movq -0xf8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
subq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0xe0(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x8c6e3
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x8c5b7
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x8c5b7
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x8c5b7
jmp 0x8c6cc
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x98(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jl 0x8c5e6
movq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x8c606
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x98(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jg 0x8c644
movq -0x78(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x8c665
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jle 0x8c69e
movq -0xd0(%rbp), %rax
subq -0xc8(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0x8c6a9
xorl %eax, %eax
movq %rax, -0x110(%rbp)
jmp 0x8c6a9
movq -0x110(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x8c545
cvtsi2sdq -0xe0(%rbp), %xmm1
cvtsi2sdq -0xa0(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x8c70a
cmpq $0x0, -0xe0(%rbp)
jne 0x8c714
movq $0x0, -0x18(%rbp)
jmp 0x8c71c
movq $0x1, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)