// File: /workspace/github_c_makefile_star2_O0/archer1357[P]Offline-Raytracer/main.c
// Function: create_video
bool create_video(int canvasWidth, int canvasHeight,
                  struct Scene *scene,float aspectRatio,float yFov,
                  struct Ray **rayStacks,
                  struct KdTraverseNode **traverseStacks)  {
  int canvasSize=canvasWidth*canvasHeight;

  float viewRot[9]={1.0f,0.0f,0.0f,
                    0.0f,1.0f,0.0f,
                    0.0f,0.0f, 1.0f};

  sobol_state_t qrnd;
  sobol_init(&qrnd,2);

  float *canvas;
  canvas=(float*)malloc(sizeof(float)*canvasSize*3);

  //

  unsigned char *bitmap;
  bitmap=(unsigned char*)malloc(canvasSize*3);

  avi_t *avifile;
  createDir("output");
  createDir("output/tmp");
  int fileNum=lastFileNum("output")+1;
  //     printf("fn %i\n",fileNum);
  char fn[512];
  snprintf(fn,sizeof(fn),"output/output%04i.avi",fileNum);

  if(access(fn, F_OK )!=-1) {
    fprintf(stderr,"'%s' already exists.\n",fn);

    return false;
  }



  //
  float viewPosKfs[]={
    2.0f,10.0f,35.0f,
    -9.0f,-10.0f,30.0f,
    -11.0f,10.0f,25.0f,

    12.0f,13.0f,20.0f,
    -12.0f,13.0f,40.0f,
    12.0f,-12.0f,35.0f,

    13.0f,13.0f,40.0f,

    0.0f,0.0f,-10.0f,

    };
  float viewPosInds[]={0.0f,5.0f,10.0f,
                       15.0f,20.0f,25.0f,
                       35.0f,45.0f};


    float frame;
    int count=0;
    for(frame=0.0f;frame<=45.0f;frame+=1.0f/(float)FPS) {

      sobol_init(&qrnd,2);
      float pos[3];
      keyframer(frame,viewPosInds,
                viewPosKfs,8, 3,
                0.0f,true, pos);

      renderScene(0,canvasSize,
                  canvasWidth,canvasHeight,
                  aspectRatio,yFov,
                  scene->minBound,scene->maxBound,
                  pos,
                  viewRot,
                  rayStacks[0],
                  traverseStacks[0],
                  scene->rootNode,
                  scene->triangles,scene->trianglesNum,
                  scene->spheres,scene->spheresNum,
                  scene->pointLights, scene->pointLightsNum,

                  &qrnd,

                  canvas,
                  RAY_MAX_BOUNCES,RAY_MAX_SPLITS
                  );

      //
      int i;
      for(i=0;i<canvasSize;i++) {
        bitmap[i*3]=floatToByte(canvas[i*3]);
        bitmap[i*3+1]=floatToByte(canvas[i*3+1]);
        bitmap[i*3+2]=floatToByte(canvas[i*3+2]);
      }



      char fn2[256];
      snprintf(fn2,sizeof(fn2),"output/tmp/tmp%04i_%i.jpg",fileNum,count);
      jo_write_jpg(fn2,bitmap,canvasWidth,canvasHeight,3, 100);

// #ifdef WIN32
//       char fn3[256];
//       snprintf(fn3,sizeof(fn3),"output/tmp/tmp%04i_%i_w32.bmp",fileNum,count);
//       printf("a\n");
//       // jo_write_jpg(fn3,bitmap,CANVAS_WIDTH,CANVAS_HEIGHT,3, 100);
//       SaveBMPToFile(fn3,bitmap,CANVAS_WIDTH,CANVAS_HEIGHT);
// #endif
      printf("frame %g\n",frame);

      count++;
      //



    }
    int i;
    avifile = AVI_open_output_file(fn);


    if(!avifile) {
      printf("aa\n");
      return false;
    }



    AVI_set_video(avifile,canvasWidth,canvasHeight,(double)FPS,"MJPG");


    for(i=0;i<count;i++) {

      char fn2[256];
      snprintf(fn2,sizeof(fn2),"output/tmp/tmp%04i_%i.jpg",fileNum,i);

      FILE *in;
      unsigned char *buffer=0;
      int buffer_len=0;

      in=fopen(fn2,"rb");

      if (in==0) {
        printf("Cannot open test.jpeg %s\n",fn2);
        return false;
      }

      fseek(in, 0, SEEK_END);
      buffer_len=(int)ftell(in);
      fseek(in,0,SEEK_SET);
      buffer=(unsigned char *)malloc(buffer_len);
      fread(buffer,1,buffer_len,in);
      fclose(in);
      // add_avifile_jpeg_frame( avifile,  bitmap, CANVAS_WIDTH,CANVAS_HEIGHT);
      AVI_write_frame(avifile,(char*)buffer,buffer_len,1);
      free(buffer);
    }

    AVI_close(avifile);

    //

    printf("Wrote '%s'.\n",fn);

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1958, %rsp           # imm = 0x1958
movl %edi, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movq %rdx, -0x40(%rbp)
movss %xmm0, -0x44(%rbp)
movss %xmm1, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movl -0x30(%rbp), %eax
imull -0x34(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x24, %edx
callq 0x4010e0
movss 0xd8762(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x80(%rbp)
movss 0xd8755(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x70(%rbp)
movss 0xd8748(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x60(%rbp)
leaq -0x13f0(%rbp), %rdi
movl $0x2, %esi
callq 0x410b70
movslq -0x5c(%rbp), %rax
shlq $0x2, %rax
imulq $0x3, %rax, %rdi
callq 0x452d20
movq %rax, -0x13f8(%rbp)
imull $0x3, -0x5c(%rbp), %eax
movslq %eax, %rdi
callq 0x452d20
movq %rax, -0x1400(%rbp)
leaq 0xd9bec(%rip), %rdi     # 0x4e4970
callq 0x4245c0
leaq 0xd9be7(%rip), %rdi     # 0x4e4977
callq 0x4245c0
leaq 0xd9bd4(%rip), %rdi     # 0x4e4970
callq 0x4245e0
addl $0x1, %eax
movl %eax, -0x140c(%rbp)
leaq -0x1610(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl $0x200, %esi            # imm = 0x200
leaq 0xd9bbf(%rip), %rdx     # 0x4e4982
movb $0x0, %al
callq 0x435a00
leaq -0x1610(%rbp), %rdi
xorl %esi, %esi
callq 0x48fbd0
cmpl $-0x1, %eax
je 0x40ae05
movq $0x526c68, %rax         # imm = 0x526C68
movq (%rax), %rdi
leaq -0x1610(%rbp), %rdx
leaq 0xd9ba3(%rip), %rsi     # 0x4e4998
movb $0x0, %al
callq 0x435870
movb $0x0, -0x29(%rbp)
jmp 0x40b30b
leaq -0x1670(%rbp), %rdi
leaq 0xd9d3d(%rip), %rsi     # 0x4e4b50
movl $0x60, %edx
callq 0x401058
movq 0xd9dbc(%rip), %rax     # 0x4e4be0
movq %rax, -0x1690(%rbp)
movq 0xd9db6(%rip), %rax     # 0x4e4be8
movq %rax, -0x1688(%rbp)
movq 0xd9db0(%rip), %rax     # 0x4e4bf0
movq %rax, -0x1680(%rbp)
movq 0xd9daa(%rip), %rax     # 0x4e4bf8
movq %rax, -0x1678(%rbp)
movl $0x0, -0x1698(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x1694(%rbp)
movss 0xd9ab2(%rip), %xmm0    # 0x4e4924
ucomiss -0x1694(%rbp), %xmm0
jb 0x40b141
leaq -0x13f0(%rbp), %rdi
movl $0x2, %esi
callq 0x410b70
movss -0x1694(%rbp), %xmm0
leaq -0x1690(%rbp), %rdi
leaq -0x1670(%rbp), %rsi
leaq -0x16a4(%rbp), %r9
movl $0x8, %edx
movl $0x3, %ecx
xorps %xmm1, %xmm1
movl $0x1, %r8d
callq 0x40ddd0
movl -0x5c(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movss -0x44(%rbp), %xmm0
movss -0x48(%rbp), %xmm1
movq -0x40(%rbp), %r8
addq $0x34, %r8
movq -0x40(%rbp), %r9
addq $0x40, %r9
leaq -0x16a4(%rbp), %rax
leaq -0x80(%rbp), %rdi
movq %rdi, -0x1900(%rbp)
movq -0x50(%rbp), %rdi
movq (%rdi), %rdi
movq %rdi, -0x18f8(%rbp)
movq -0x58(%rbp), %rdi
movq (%rdi), %rdi
movq %rdi, -0x18f0(%rbp)
movq -0x40(%rbp), %rdi
movq 0x28(%rdi), %rdi
movq %rdi, -0x18e8(%rbp)
movq -0x40(%rbp), %rdi
movq (%rdi), %r13
movq -0x40(%rbp), %rdi
movl 0x18(%rdi), %r12d
movq -0x40(%rbp), %rdi
movq 0x8(%rdi), %r15
movq -0x40(%rbp), %rdi
movl 0x1c(%rdi), %r14d
movq -0x40(%rbp), %rdi
movq 0x10(%rdi), %rbx
movq -0x40(%rbp), %rdi
movl 0x20(%rdi), %r11d
movq -0x13f8(%rbp), %rdi
movq %rdi, -0x18e0(%rbp)
xorl %edi, %edi
leaq -0x13f0(%rbp), %r10
movq %rax, (%rsp)
movq -0x1900(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x18f8(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x18f0(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0x18e8(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x18e0(%rbp), %rax
movq %r13, 0x28(%rsp)
movl %r12d, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movl %r14d, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movl %r11d, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movl $0x1, 0x68(%rsp)
movl $0x8, 0x70(%rsp)
callq 0x4232c0
movl $0x0, -0x16a8(%rbp)
movl -0x16a8(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x40b0af
movq -0x13f8(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1400(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x13f8(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1400(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x13f8(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1400(%rbp), %rax
imull $0x3, -0x16a8(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x16a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x16a8(%rbp)
jmp 0x40afed
leaq -0x17b0(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl -0x1698(%rbp), %r8d
movl $0x100, %esi            # imm = 0x100
leaq 0xd98df(%rip), %rdx     # 0x4e49ae
movb $0x0, %al
callq 0x435a00
leaq -0x17b0(%rbp), %rdi
movq -0x1400(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x3, %r8d
movl $0x64, %r9d
callq 0x4085a0
movss -0x1694(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xd98ba(%rip), %rdi     # 0x4e49c8
movb $0x1, %al
callq 0x435930
movl -0x1698(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1698(%rbp)
movss 0xd97fc(%rip), %xmm0    # 0x4e4928
addss -0x1694(%rbp), %xmm0
movss %xmm0, -0x1694(%rbp)
jmp 0x40ae6a
leaq -0x1610(%rbp), %rdi
callq 0x4018b0
movq %rax, -0x1408(%rbp)
cmpq $0x0, -0x1408(%rbp)
jne 0x40b175
leaq 0xd986d(%rip), %rdi     # 0x4e49d2
movb $0x0, %al
callq 0x435930
movb $0x0, -0x29(%rbp)
jmp 0x40b30b
movq -0x1408(%rbp), %rdi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
movsd 0xd97de(%rip), %xmm0    # 0x4e4968
leaq 0xd9845(%rip), %rcx     # 0x4e49d6
callq 0x401ab0
movl $0x0, -0x17b4(%rbp)
movl -0x17b4(%rbp), %eax
cmpl -0x1698(%rbp), %eax
jge 0x40b2e6
leaq -0x18c0(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl -0x17b4(%rbp), %r8d
movl $0x100, %esi            # imm = 0x100
leaq 0xd97dc(%rip), %rdx     # 0x4e49ae
movb $0x0, %al
callq 0x435a00
movq $0x0, -0x18d0(%rbp)
movl $0x0, -0x18d4(%rbp)
leaq -0x18c0(%rbp), %rdi
leaq 0xd97df(%rip), %rsi     # 0x4e49db
callq 0x43c8a0
movq %rax, -0x18c8(%rbp)
cmpq $0x0, -0x18c8(%rbp)
jne 0x40b230
leaq -0x18c0(%rbp), %rsi
leaq 0xd97be(%rip), %rdi     # 0x4e49de
movb $0x0, %al
callq 0x435930
movb $0x0, -0x29(%rbp)
jmp 0x40b30b
movq -0x18c8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x440980
movq -0x18c8(%rbp), %rdi
callq 0x43caa0
movl %eax, -0x18d4(%rbp)
movq -0x18c8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x440980
movslq -0x18d4(%rbp), %rdi
callq 0x452d20
movq %rax, -0x18d0(%rbp)
movq -0x18d0(%rbp), %rdi
movslq -0x18d4(%rbp), %rdx
movq -0x18c8(%rbp), %rcx
movl $0x1, %esi
callq 0x43c9a0
movq -0x18c8(%rbp), %rdi
callq 0x43c170
movq -0x1408(%rbp), %rdi
movq -0x18d0(%rbp), %rsi
movslq -0x18d4(%rbp), %rdx
movl $0x1, %ecx
callq 0x4033a0
movq -0x18d0(%rbp), %rdi
callq 0x453060
movl -0x17b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x17b4(%rbp)
jmp 0x40b1a0
movq -0x1408(%rbp), %rdi
callq 0x403ba0
leaq -0x1610(%rbp), %rsi
leaq 0xd96f8(%rip), %rdi     # 0x4e49f8
movb $0x0, %al
callq 0x435930
movb $0x1, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
addq $0x1958, %rsp           # imm = 0x1958
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)