// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %rsi, %r14
cmpl $0x1, %edx
je 0x408535
testl %edx, %edx
jne 0x408428
movslq %ebp, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4010e0
movl %r8d, %r15d
movq %rdi, %r12
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm1
jne 0x40843c
jnp 0x408503
cvtss2sd %xmm0, %xmm0
movslq %edx, %rax
xorps %xmm1, %xmm1
cvtss2sd -0x4(%r12,%rax,4), %xmm1
callq 0x417a10
cvtsd2ss %xmm0, %xmm0
movl %ebp, %eax
negl %eax
addq $0x4, %r12
xorl %ecx, %ecx
movq %r12, %rdx
movss -0x4(%r12), %xmm1
addl %ebp, %ecx
addq $0x4, %r12
ucomiss %xmm1, %xmm0
jb 0x408460
movss (%rdx), %xmm2
ucomiss %xmm0, %xmm2
jb 0x408460
testl %ebp, %ebp
jle 0x4084fa
subss %xmm1, %xmm0
subss %xmm1, %xmm2
divss %xmm2, %xmm0
addl %ecx, %eax
movss 0xccff8(%rip), %xmm1    # 0x4d5490
movl %ebp, %edx
testb %r15b, %r15b
je 0x4084c5
minss %xmm1, %xmm0
xorps %xmm2, %xmm2
maxss %xmm0, %xmm2
movaps %xmm2, %xmm3
mulss %xmm2, %xmm3
addss %xmm2, %xmm2
movss 0xce78b(%rip), %xmm0    # 0x4d6c48
subss %xmm2, %xmm0
mulss %xmm3, %xmm0
subss %xmm0, %xmm1
movl %ecx, %ecx
leaq (%r14,%rcx,4), %rcx
movl %eax, %eax
leaq (%r14,%rax,4), %rax
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm2
mulss %xmm0, %xmm2
movss (%rax,%rsi,4), %xmm3
mulss %xmm1, %xmm3
addss %xmm2, %xmm3
movss %xmm3, (%rbx,%rsi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x4084d7
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movss (%r12), %xmm1
ucomiss %xmm0, %xmm1
jae 0x408535
movslq %edx, %rax
ucomiss -0x4(%r12,%rax,4), %xmm0
jb 0x408456
decl %edx
imull %ebp, %edx
movslq %edx, %rax
leaq (%r14,%rax,4), %rsi
movslq %ebp, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
jmp 0x408542
movslq %ebp, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x401058