// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movb %r8b, %al
movss %xmm0, -0x4(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movss %xmm1, -0x24(%rbp)
andb $0x1, %al
movb %al, -0x25(%rbp)
movq %r9, -0x30(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x40de1a
movq -0x30(%rbp), %rdi
movslq -0x20(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x4010e0
jmp 0x40e041
cmpl $0x1, -0x1c(%rbp)
jne 0x40de3a
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x20(%rbp), %rdx
shlq $0x2, %rdx
callq 0x401058
jmp 0x40e041
movss -0x24(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x40de4b
jp 0x40de4b
jmp 0x40de7a
movss -0x4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm1
cvtss2sd %xmm1, %xmm1
callq 0x425f00
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
jmp 0x40def5
movss -0x4(%rbp), %xmm1
movq -0x10(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x40dea6
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x20(%rbp), %rdx
shlq $0x2, %rdx
callq 0x401058
jmp 0x40e041
movss -0x4(%rbp), %xmm0
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
ucomiss (%rax,%rcx,4), %xmm0
jb 0x40deeb
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %eax
subl $0x1, %eax
imull -0x20(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rsi
movslq -0x20(%rbp), %rdx
shlq $0x2, %rdx
callq 0x401058
jmp 0x40e041
movss -0x4(%rbp), %xmm0
movss %xmm0, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x3c(%rbp), %xmm0
ucomiss -0x40(%rbp), %xmm0
jb 0x40e033
movss -0x3c(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x40e033
movss -0x3c(%rbp), %xmm0
subss -0x40(%rbp), %xmm0
movss -0x44(%rbp), %xmm1
subss -0x40(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x40e031
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
imull -0x20(%rbp), %ecx
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
imull -0x20(%rbp), %ecx
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x50(%rbp)
testb $0x1, -0x25(%rbp)
je 0x40e000
movss -0x4c(%rbp), %xmm0
movss %xmm0, -0x54(%rbp)
movss -0x50(%rbp), %xmm0
movss %xmm0, -0x58(%rbp)
movss -0x48(%rbp), %xmm2
xorps %xmm0, %xmm0
movss 0xd54aa(%rip), %xmm1    # 0x4e3484
callq 0x40dce0
movss -0x58(%rbp), %xmm1
movaps %xmm0, %xmm2
movss -0x54(%rbp), %xmm0
callq 0x40dd90
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x40e021
movss -0x4c(%rbp), %xmm0
movss -0x50(%rbp), %xmm1
movss -0x48(%rbp), %xmm2
callq 0x40dd90
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x40e023
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x40df6b
jmp 0x40e041
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x40defc
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)