// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: get_win_yoff(overlap_region*, long, long, long, long, long, long, long*, long*, long*, long*)
int64_t get_win_yoff(overlap_region *o, int64_t toff, int64_t k, int64_t qs, int64_t qe, int64_t ts, int64_t te, int64_t *q, int64_t *t,
int64_t *rq, int64_t *rt)
{
    int64_t wn = o->w_list.n<<1, s, qdis, tdis, dis;
    if(k<0) k = 0; if(k>=wn) k = wn-1;
    for (; k < wn; k++) {
        s = ((k&1)?(o->w_list.a[k>>1].x_end):(o->w_list.a[k>>1].x_start));
        if(s >= toff) break;
    }
    for (k=(k>=wn?(wn-1):(k)); k >= 0; k--) {
        s = ((k&1)?(o->w_list.a[k>>1].x_end):(o->w_list.a[k>>1].x_start));
        if(s <= toff) break;
    }

    q[0] = qs; q[1] = qe; t[0] = ts; t[1] = te;
    if(k < 0) {//toff <= t[1]
        q[1] =  o->w_list.a[0].x_start;
        t[1] = o->w_list.a[0].y_start;
        tdis = t[1] - toff; qdis = q[1] - q[0]; dis = MIN(qdis, tdis);
        (*rq) = q[1] - dis; (*rt) = t[1] - dis;
    } else if(k == wn-1) {//toff >= t[0]
        q[0] = o->w_list.a[k>>1].x_end;
        t[0] = o->w_list.a[k>>1].y_end;
        tdis = toff - t[0]; qdis = q[1] - q[0]; dis = MIN(qdis, tdis);
        (*rq) = q[0] + dis; (*rt) = t[0] + dis;
    } else {//toff >= t[0] && toff <= t[1]
        q[0] = ((k&1)?(o->w_list.a[k>>1].x_end):(o->w_list.a[k>>1].x_start));
        t[0] = ((k&1)?(o->w_list.a[k>>1].y_end):(o->w_list.a[k>>1].y_start));
        k++;
        q[1] = ((k&1)?(o->w_list.a[k>>1].x_end):(o->w_list.a[k>>1].x_start));
        t[1] = ((k&1)?(o->w_list.a[k>>1].y_end):(o->w_list.a[k>>1].y_start));
        k--;
        (*rt) = toff;
        (*rq) = q[0] + get_offset_adjust(toff-t[0], t[1]-t[0], q[1]-q[0]);
    }
    return k;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x58(%rsp), %r11
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
movq 0x48(%rdi), %r13
leaq (,%r13,2), %rax
xorl %r12d, %r12d
testq %rdx, %rdx
cmovgq %rdx, %r12
cmpq %rax, %r12
leaq -0x1(%r13,%r13), %rdx
cmovgeq %rdx, %r12
cmpq %rax, %r12
jge 0x493cc
movq 0x58(%rdi), %r13
movq %r12, %rbp
shlq $0x4, %rbp
andq $-0x20, %rbp
movq %r13, %r10
addq %rbp, %r10
testb $0x1, %r12b
leaq 0x4(%r13,%rbp), %r13
cmoveq %r10, %r13
movslq (%r13), %r10
cmpq %rsi, %r10
jge 0x493cf
incq %r12
jmp 0x49397
movq %rdx, %r12
cmpq $-0x1, %r12
movabsq $-0x1, %rax
cmovlq %r12, %rax
testq %r12, %r12
js 0x49416
movq 0x58(%rdi), %r10
movq %r12, %r13
shlq $0x4, %r13
andq $-0x20, %r13
leaq (%r10,%r13), %rbp
testb $0x1, %r12b
leaq 0x4(%r10,%r13), %r10
cmoveq %rbp, %r10
movslq (%r10), %r10
cmpq %rsi, %r10
jle 0x49413
decq %r12
jmp 0x493e1
movq %r12, %rax
movq %rcx, (%r14)
movq %r8, 0x8(%r14)
movq %r9, (%rbx)
movq %r15, 0x8(%rbx)
testq %rax, %rax
js 0x49478
cmpq %rdx, %rax
jne 0x494b9
movq 0x58(%rdi), %rcx
shlq $0x4, %rdx
andq $-0x20, %rdx
movslq 0x4(%rcx,%rdx), %rdi
movq %rdi, (%r14)
movslq 0xc(%rcx,%rdx), %rcx
movq %rcx, (%rbx)
subq %rcx, %rsi
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
cmpq %rsi, %rdx
cmovgeq %rsi, %rdx
addq %rdx, %rcx
movq 0x50(%rsp), %rsi
movq %rcx, (%rsi)
addq (%rbx), %rdx
movq %rdx, (%r11)
jmp 0x49566
movq 0x58(%rdi), %rcx
movslq (%rcx), %rdx
movq %rdx, 0x8(%r14)
movslq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rbx)
subq %rsi, %rcx
movq 0x8(%r14), %rdx
movq %rdx, %rsi
subq (%r14), %rsi
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
subq %rsi, %rdx
movq 0x50(%rsp), %rcx
movq %rdx, (%rcx)
movq 0x8(%rbx), %rcx
subq %rsi, %rcx
movq %rcx, (%r11)
jmp 0x49566
movq 0x58(%rdi), %rdi
movq %rax, %r9
shrq %r9
shlq $0x5, %r9
testb $0x1, %al
jne 0x494e8
leaq (%rdi,%r9), %rcx
leaq (%rdi,%r9), %rdx
addq $0x8, %rdx
leaq (%rdi,%r9), %r8
addq $0x4, %r8
addq %r9, %rdi
addq $0xc, %rdi
jmp 0x4950e
leaq (%rdi,%r9), %rcx
addq $0x4, %rcx
leaq (%rdi,%r9), %rdx
addq $0xc, %rdx
movq %rax, %r9
shlq $0x4, %r9
leaq (%r9,%rdi), %r8
addq $0x10, %r8
addq %r9, %rdi
addq $0x18, %rdi
movslq (%r8), %r8
movslq (%rdx), %rdx
movslq (%rcx), %rcx
movq %rcx, (%r14)
movq %rdx, (%rbx)
movq %r8, 0x8(%r14)
movslq (%rdi), %rcx
movq %rcx, 0x8(%rbx)
movq %rsi, (%r11)
movq (%r14), %rcx
movl (%rbx), %edx
movl 0x8(%rbx), %edi
subl %edx, %esi
subl %edx, %edi
movl 0x8(%r14), %edx
subl %ecx, %edx
cvtsi2sd %rsi, %xmm0
cvtsi2sd %rdi, %xmm1
cvtsi2sd %rdx, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rdx
movl %edx, %edx
addq %rcx, %rdx
movq 0x50(%rsp), %rcx
movq %rdx, (%rcx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq