// File: /workspace/github_cpp_makefile_star2_O1/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 0x50(%rsp), %r10
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
movq 0x48(%rdi), %r13
leaq (,%r13,2), %r12
xorl %eax, %eax
testq %rdx, %rdx
cmovgq %rdx, %rax
cmpq %r12, %rax
leaq -0x1(%r13,%r13), %rdx
cmovgeq %rdx, %rax
movq %rax, %r13
sarq %r13
testb $0x1, %al
jne 0x7c9a2
shlq $0x5, %r13
addq 0x58(%rdi), %r13
jmp 0x7c9b1
movq 0x58(%rdi), %rbp
shlq $0x5, %r13
addq %rbp, %r13
addq $0x4, %r13
movslq (%r13), %r13
cmpq %rsi, %r13
jge 0x7c9c5
incq %rax
cmpq %r12, %rax
jl 0x7c98e
movq %rdx, %rax
testq %rax, %rax
js 0x7ca09
movq %rax, %r12
shrq %r12
testb $0x1, %al
jne 0x7c9de
shlq $0x5, %r12
addq 0x58(%rdi), %r12
jmp 0x7c9ed
movq 0x58(%rdi), %r13
shlq $0x5, %r12
addq %r13, %r12
addq $0x4, %r12
movslq (%r12), %r12
cmpq %rsi, %r12
jle 0x7ca09
leaq -0x1(%rax), %r12
testq %rax, %rax
movq %r12, %rax
jg 0x7c9ca
movq $-0x1, %rax
movq %rcx, (%r14)
movq %r8, 0x8(%r14)
movq %r9, (%rbx)
movq %r15, 0x8(%rbx)
testq %rax, %rax
js 0x7ca69
cmpq %rdx, %rax
jne 0x7caa5
movq 0x58(%rdi), %rcx
movq %rax, %rdx
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 %rcx, (%r10)
addq (%rbx), %rdx
movq %rdx, (%r11)
jmp 0x7cb58
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 %rdx, (%r10)
movq 0x8(%rbx), %rcx
subq %rsi, %rcx
movq %rcx, (%r11)
jmp 0x7cb58
movq 0x58(%rdi), %rdx
movq %rax, %rcx
shlq $0x4, %rcx
movq %rcx, %r8
andq $-0x20, %r8
leaq (%rdx,%r8), %r9
testb $0x1, %al
leaq 0x4(%rdx,%r8), %rdx
cmoveq %r9, %rdx
movslq (%rdx), %rdx
movq %rdx, (%r14)
movq 0x58(%rdi), %rdx
leaq 0xc(%rdx,%r8), %r9
leaq 0x8(%rdx,%r8), %rdx
cmovneq %r9, %rdx
movslq (%rdx), %rdx
movq %rdx, (%rbx)
movq 0x58(%rdi), %rdx
addq $0x10, %rcx
andq $-0x20, %rcx
leaq (%rdx,%rcx), %r8
testb $0x1, %al
leaq 0x4(%rdx,%rcx), %rdx
cmovneq %r8, %rdx
movslq (%rdx), %rdx
movq %rdx, 0x8(%r14)
movq 0x58(%rdi), %rdx
leaq 0xc(%rdx,%rcx), %rdi
leaq 0x8(%rdx,%rcx), %rcx
cmoveq %rdi, %rcx
movslq (%rcx), %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 %rdx, (%r10)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq