// File: /workspace/github_c_makefile_star2_O2/archer1357[P]Offline-Raytracer/main.c
// Function: create_screenshot
bool create_screenshot(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};

  float viewPos[3]={2.0f,0.0f, 35.0f};
  // float viewPos[3]={0.0f,0.0f, 0.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);


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

  if(access(fn, F_OK )!=-1) {
    fprintf(stderr,"'%s' already exists.\n",fn);
    return false;
  }
  renderScene(0,canvasSize,
              canvasWidth,canvasHeight,
              aspectRatio,yFov,
              scene->minBound,scene->maxBound,
              viewPos,
              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]);
  }

  stbi_write_png(fn,canvasWidth,canvasHeight,3,bitmap,canvasWidth*3);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x15e8, %rsp           # imm = 0x15E8
movq %r8, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movss %xmm1, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
movq %rdx, %rbp
movl %esi, 0x8(%rsp)
movl %esi, %r13d
movq %rdi, 0x18(%rsp)
imull %edi, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x40(%rsp)
movl %eax, 0x50(%rsp)
movl %eax, 0x60(%rsp)
movl $0x420c0000, 0x38(%rsp) # imm = 0x420C0000
movq $0x40000000, 0x30(%rsp) # imm = 0x40000000
leaq 0x278(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x409184
movslq %r13d, %rbx
imulq $0xc, %rbx, %rdi
callq 0x442160
movq %rax, %r12
leal (%rbx,%rbx,2), %eax
movslq %eax, %rdi
callq 0x442160
movq %rax, %r15
leaq 0xcd6bb(%rip), %rbx     # 0x4d39c0
movq %rbx, %rdi
callq 0x413e50
movq %rbx, %rdi
callq 0x413e5a
leal 0x1(%rax), %ebx
leaq 0xcd732(%rip), %rdi     # 0x4d3a51
xorl %r14d, %r14d
movl %ebx, %esi
xorl %eax, %eax
callq 0x424bd0
leaq 0xcd726(%rip), %rdx     # 0x4d3a58
leaq 0x70(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %ebx, %ecx
xorl %eax, %eax
callq 0x424ca0
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x47f010
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x40647d
movl %ebx, 0xc(%rsp)
leaq 0x34(%rbp), %r8
leaq 0x40(%rbp), %r9
movl 0x18(%rbp), %r11d
movl 0x1c(%rbp), %ebx
movl 0x20(%rbp), %eax
subq $0x8, %rsp
xorl %edi, %edi
movl %r13d, %esi
movq 0x20(%rsp), %rdx
movl 0x10(%rsp), %ecx
movss 0x18(%rsp), %xmm0
movss 0x1c(%rsp), %xmm1
pushq $0x8
pushq $0x1
pushq %r12
leaq 0x298(%rsp), %r10
pushq %r10
pushq %rax
pushq 0x10(%rbp)
pushq %rbx
pushq 0x8(%rbp)
pushq %r11
pushq (%rbp)
pushq 0x28(%rbp)
movq 0x80(%rsp), %rax
pushq (%rax)
movq 0x90(%rsp), %rax
pushq (%rax)
leaq 0xb0(%rsp), %rax
pushq %rax
leaq 0xa8(%rsp), %rax
pushq %rax
callq 0x4132fc
addq $0x80, %rsp
testl %r13d, %r13d
cmovlel %r14d, %r13d
leaq (,%r13,2), %rbx
addq %r13, %rbx
cmpq %r14, %rbx
je 0x406430
movss (%r12,%r14,4), %xmm0
callq 0x413f51
movb %al, (%r15,%r14)
movss 0x4(%r12,%r14,4), %xmm0
callq 0x413f51
movb %al, 0x1(%r15,%r14)
movss 0x8(%r12,%r14,4), %xmm0
callq 0x413f51
movb %al, 0x2(%r15,%r14)
addq $0x3, %r14
jmp 0x4063f4
movq 0x18(%rsp), %rsi
leal (%rsi,%rsi,2), %r9d
leaq 0x70(%rsp), %rbx
pushq $0x3
popq %rcx
movq %rbx, %rdi
movl 0x8(%rsp), %edx
movq %r15, %r8
callq 0x411c2c
leaq 0xcd5ed(%rip), %rdi     # 0x4d3a44
movq %rbx, %rsi
xorl %eax, %eax
callq 0x424bd0
movl 0xc(%rsp), %ebx
cmpl $-0x1, %ebx
sete %al
addq $0x15e8, %rsp           # imm = 0x15E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x516868, %rax         # imm = 0x516868
movq (%rax), %rdi
leaq 0xcd55a(%rip), %rsi     # 0x4d39e8
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x424b10
jmp 0x406465