// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1778, %rsp           # imm = 0x1778
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movss %xmm1, 0x18(%rsp)
movss %xmm0, 0x14(%rsp)
movq %rdx, %r13
movl %edi, %r14d
movl %esi, 0x8(%rsp)
movl %esi, %ebp
imull %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x50(%rsp)
movl %eax, 0x60(%rsp)
movl %eax, 0x70(%rsp)
leaq 0x408(%rsp), %rdi
movl $0x2, %esi
callq 0x409f0c
movslq %ebp, %rbx
leaq (,%rbx,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4449d0
movq %rax, %r12
leal (%rbx,%rbx,2), %eax
movslq %eax, %rdi
callq 0x4449d0
movq %rax, %rbx
leaq 0xd0566(%rip), %r15     # 0x4d69d0
movq %r15, %rdi
callq 0x4165da
leaq 0xd055e(%rip), %rdi     # 0x4d69d7
callq 0x4165da
movq %r15, %rdi
callq 0x4165e4
incl %eax
leaq 0xd0553(%rip), %rdx     # 0x4d69e2
leaq 0x200(%rsp), %r15
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movl %eax, 0xc(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0x427510
movq %r15, %rdi
xorl %esi, %esi
callq 0x481890
cmpl $-0x1, %eax
jne 0x40681d
leaq 0xd06da(%rip), %rsi     # 0x4d6ba0
leaq 0xa0(%rsp), %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
movaps 0xd0753(%rip), %xmm0    # 0x4d6c30
leaq 0x80(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps 0xd0730(%rip), %xmm0    # 0x4d6c20
movaps %xmm0, (%rax)
leaq 0x34(%r13), %rax
movq %rax, 0x30(%rsp)
movq %r13, 0x38(%rsp)
leaq 0x40(%r13), %rax
movq %rax, 0x28(%rsp)
movl %ebp, 0x10(%rsp)
movl %ebp, %eax
movl %r14d, %r13d
leaq (%rax,%rax,2), %r14
xorps %xmm0, %xmm0
movl $0x1, %eax
xorl %ebp, %ebp
movl 0x8(%rsp), %r15d
movl %r13d, 0x4(%rsp)
movss %xmm0, (%rsp)
movl %ebp, 0x1c(%rsp)
movq %rax, 0x20(%rsp)
leaq 0x408(%rsp), %rdi
movl $0x2, %esi
callq 0x409f0c
xorps %xmm1, %xmm1
movss (%rsp), %xmm0
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movl $0x8, %edx
movl $0x3, %ecx
movl $0x1, %r8d
leaq 0x74(%rsp), %r9
callq 0x4083f2
movq 0x38(%rsp), %rbp
movl 0x18(%rbp), %eax
movl 0x1c(%rbp), %r10d
movl 0x20(%rbp), %r11d
subq $0x8, %rsp
xorl %edi, %edi
movl 0x18(%rsp), %esi
movl %r13d, %edx
movl %r15d, %ecx
movss 0x1c(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
pushq $0x8
pushq $0x1
pushq %r12
leaq 0x428(%rsp), %r15
pushq %r15
pushq %r11
pushq 0x10(%rbp)
pushq %r10
pushq 0x8(%rbp)
pushq %rax
pushq (%rbp)
pushq 0x28(%rbp)
movq 0xa8(%rsp), %rax
pushq (%rax)
movq 0xa8(%rsp), %rax
pushq (%rax)
leaq 0xc0(%rsp), %rax
pushq %rax
leaq 0xec(%rsp), %rax
pushq %rax
callq 0x41599b
addq $0x80, %rsp
cmpl $0x0, 0x10(%rsp)
jle 0x406646
xorl %ebp, %ebp
movss (%r12,%rbp,4), %xmm0
callq 0x4166f7
movb %al, (%rbx,%rbp)
movss 0x4(%r12,%rbp,4), %xmm0
callq 0x4166f7
movb %al, 0x1(%rbx,%rbp)
movss 0x8(%r12,%rbp,4), %xmm0
callq 0x4166f7
movb %al, 0x2(%rbx,%rbp)
addq $0x3, %rbp
cmpq %rbp, %r14
jne 0x40660f
movl $0x100, %esi            # imm = 0x100
leaq 0x100(%rsp), %r15
movq %r15, %rdi
leaq 0xd03b1(%rip), %rdx     # 0x4d6a0e
movl 0xc(%rsp), %ecx
movl 0x1c(%rsp), %ebp
movl %ebp, %r8d
xorl %eax, %eax
callq 0x427510
movq %r15, %rdi
movq %rbx, %rsi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
movl 0x8(%rsp), %r15d
movl %r15d, %ecx
movl $0x3, %r8d
movl $0x64, %r9d
callq 0x404b44
xorps %xmm0, %xmm0
cvtss2sd (%rsp), %xmm0
leaq 0xd0383(%rip), %rdi     # 0x4d6a28
movb $0x1, %al
callq 0x427440
movss (%rsp), %xmm0
incl %ebp
addss 0xd029d(%rip), %xmm0    # 0x4d6958
movq 0x20(%rsp), %rax
incl %eax
movss 0xd0292(%rip), %xmm1    # 0x4d695c
ucomiss %xmm0, %xmm1
jae 0x40652b
leaq 0x200(%rsp), %rdi
callq 0x4019c8
testq %rax, %rax
je 0x4067fb
movq %rax, %rbx
leaq 0xd033f(%rip), %rcx     # 0x4d6a32
movsd 0xd026d(%rip), %xmm0    # 0x4d6968
movq %rax, %rdi
movl 0x4(%rsp), %esi
movl %r15d, %edx
callq 0x401ace
leaq 0x100(%rsp), %r15
xorl %ebp, %ebp
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
leaq 0xd02eb(%rip), %rdx     # 0x4d6a0e
movl 0xc(%rsp), %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x427510
movq %r15, %rdi
leaq 0xd02fc(%rip), %rsi     # 0x4d6a37
callq 0x42e3b0
movq %rax, %r13
testq %rax, %rax
je 0x4067b4
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x432630
movq %r13, %rdi
callq 0x42e5b0
movq %rax, %r14
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x432630
movslq %r14d, %r14
movq %r14, %rdi
callq 0x4449d0
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %r13, %rcx
callq 0x42e4b0
movq %r13, %rdi
callq 0x42dc80
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x4021b5
movq %r12, %rdi
callq 0x444d10
jmp 0x4067c5
leaq 0xd027f(%rip), %rdi     # 0x4d6a3a
movq %r15, %rsi
xorl %eax, %eax
callq 0x427440
testq %r13, %r13
movq 0x20(%rsp), %rax
je 0x406807
incl %ebp
cmpl %ebp, %eax
jne 0x406714
movq %rbx, %rdi
callq 0x402633
leaq 0xd026c(%rip), %rdi     # 0x4d6a54
leaq 0x200(%rsp), %rsi
xorl %eax, %eax
callq 0x427440
movb $0x1, %bl
jmp 0x406809
leaq 0xd032b(%rip), %rdi     # 0x4d6b2d
callq 0x42e9d0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x1778, %rsp           # imm = 0x1778
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x518868, %rax         # imm = 0x518868
movq (%rax), %rdi
leaq 0xd01ca(%rip), %rsi     # 0x4d69f8
xorl %ebx, %ebx
leaq 0x200(%rsp), %rdx
xorl %eax, %eax
callq 0x427380
jmp 0x406809