// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movl %esi, 0x8(%rsp)
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
movl $0x2, %esi
callq 0x40a294
movslq %r13d, %rbx
leaq (,%rbx,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4458c0
movq %rax, %r12
leal (%rbx,%rbx,2), %eax
movslq %eax, %rdi
callq 0x4458c0
movq %rax, %r15
leaq 0xd1065(%rip), %rbx     # 0x4d7a10
movq %rbx, %rdi
callq 0x4175e6
movq %rbx, %rdi
callq 0x4175f0
leal 0x1(%rax), %ebx
leaq 0xd10dc(%rip), %rdi     # 0x4d7aa1
movl %ebx, %esi
xorl %eax, %eax
callq 0x428330
leaq 0xd10d3(%rip), %rdx     # 0x4d7aa8
leaq 0x70(%rsp), %r14
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x428400
movq %r14, %rdi
xorl %esi, %esi
callq 0x482750
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x406b1f
movl %ebx, 0xc(%rsp)
leaq 0x34(%rbp), %r8
leaq 0x40(%rbp), %r9
movl 0x18(%rbp), %r11d
movl 0x1c(%rbp), %ebx
movl 0x20(%rbp), %r14d
subq $0x8, %rsp
leaq 0x48(%rsp), %r10
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), %rax
pushq %rax
pushq %r14
pushq 0x10(%rbp)
pushq %rbx
pushq 0x8(%rbp)
pushq %r11
pushq (%rbp)
pushq 0x28(%rbp)
movq 0x80(%rsp), %r11
pushq (%r11)
movq 0x90(%rsp), %r11
pushq (%r11)
pushq %r10
leaq 0xa8(%rsp), %rax
pushq %rax
callq 0x416a36
addq $0x80, %rsp
testl %r13d, %r13d
jle 0x406ad0
movl %r13d, %eax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movss (%r12,%r14,4), %xmm0
callq 0x41770c
movb %al, (%r15,%r14)
movss 0x4(%r12,%r14,4), %xmm0
callq 0x41770c
movb %al, 0x1(%r15,%r14)
movss 0x8(%r12,%r14,4), %xmm0
callq 0x41770c
movb %al, 0x2(%r15,%r14)
addq $0x3, %r14
cmpq %r14, %rbx
jne 0x406a96
movq 0x18(%rsp), %rsi
leal (%rsi,%rsi,2), %r9d
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl 0x8(%rsp), %edx
movl $0x3, %ecx
movq %r15, %r8
callq 0x41526a
leaq 0xd0f9b(%rip), %rdi     # 0x4d7a94
movq %rbx, %rsi
xorl %eax, %eax
callq 0x428330
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 $0x519848, %rax         # imm = 0x519848
movq (%rax), %rdi
leaq 0xd0f08(%rip), %rsi     # 0x4d7a38
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x428270
jmp 0x406b07