// File: /workspace/github_c_makefile_star2_O2/archer1357[P]Offline-Raytracer/math.c
// Function: keyframer
void keyframer(float frame, const float *positions,
               const float *values, int valuesNum, int vecLen,
               float repeatGap,bool smooth, float *out) {

  int i,j;
  float frame2;
  float pos1,pos2;

  //
  if(valuesNum==0) {
    memset(out,0,sizeof(float)*vecLen);
    return;
  }

  //
  if(valuesNum==1) {
    //return values from only keyframe
    memcpy(out,values,sizeof(float)*vecLen);
    return;
  }


  //
  if(repeatGap!=0.0f) {
    //todo
    frame2=(float)fmod((double)frame,(double)positions[valuesNum-1]);
  } else {
    //when frame before beginning
    if(frame <= positions[0]) {
      //return values from first keyframe
      memcpy(out,values,sizeof(float)*vecLen);
      return;
    }

    //when frame past end
    if(frame >= positions[valuesNum-1]) {
      //return values from last keyframe
      memcpy(out,&values[(valuesNum-1)*vecLen],sizeof(float)*vecLen);
      return;
    }

    frame2=frame;
  }


  //
  i=0;

  while(true) {
    pos1=positions[i];
    pos2=positions[i+1];

    if(frame2>=pos1 && frame2<=pos2) {
      float v=(frame2-pos1)/(pos2-pos1);

      for(j=0;j<vecLen;j++) {
        float val1=values[i*vecLen+j];
        float val2=values[(i+1)*vecLen+j];

        if(smooth) {
          out[j]=lerp(val1,val2,smoothStep(0.0f,1.0f,v));
        } else {
          out[j]=lerp(val1,val2,v);
        }
      }

      return;
    }

    i++;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %r9, %rbx
movl %ecx, %r14d
movq %rsi, %r15
cmpl $0x1, %edx
je 0x407c6a
testl %edx, %edx
jne 0x407b58
movslq %r14d, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4010e8
movl %r8d, %ebp
movq %rdi, %r12
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jne 0x407b6c
jnp 0x407c37
cvtss2sd %xmm0, %xmm0
movslq %edx, %rax
cvtss2sd -0x4(%r12,%rax,4), %xmm1
callq 0x4151a0
cvtsd2ss %xmm0, %xmm0
movss (%r12), %xmm1
movl %r14d, %eax
negl %eax
addq $0x4, %r12
xorl %ecx, %ecx
movaps %xmm1, %xmm2
movss (%r12), %xmm1
addl %r14d, %ecx
addq $0x4, %r12
ucomiss %xmm2, %xmm0
jb 0x407b94
ucomiss %xmm0, %xmm1
jb 0x407b94
subss %xmm2, %xmm0
subss %xmm2, %xmm1
divss %xmm1, %xmm0
movss 0xca89e(%rip), %xmm1    # 0x4d2460
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r14d
testb %bpl, %bpl
je 0x407bf6
minss %xmm1, %xmm0
xorps %xmm2, %xmm2
maxss %xmm0, %xmm2
movaps %xmm2, %xmm3
mulss %xmm2, %xmm3
addss %xmm2, %xmm2
movss 0xcc04a(%rip), %xmm0    # 0x4d3c38
subss %xmm2, %xmm0
mulss %xmm3, %xmm0
subss %xmm0, %xmm1
movslq %ecx, %rsi
leaq (%r15,%rsi,4), %rcx
addl %esi, %eax
cltq
leaq (%r15,%rax,4), %rax
cmpq %rdx, %r14
je 0x407c2e
movss (%rcx,%rdx,4), %xmm2
mulss %xmm0, %xmm2
movss (%rax,%rdx,4), %xmm3
mulss %xmm1, %xmm3
addss %xmm2, %xmm3
movss %xmm3, (%rbx,%rdx,4)
incq %rdx
jmp 0x407c09
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movss (%r12), %xmm1
ucomiss %xmm0, %xmm1
jae 0x407c6a
movslq %edx, %rax
ucomiss -0x4(%r12,%rax,4), %xmm0
jb 0x407b89
decl %edx
imull %r14d, %edx
movslq %edx, %rax
leaq (%r15,%rax,4), %rsi
movslq %r14d, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
jmp 0x407c77
movslq %r14d, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
movq %r15, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x401060