// File: /workspace/github_c_makefile_star2_O2/archer1357[P]Offline-Raytracer/main.c
// Function: main
int main(int argc, char *argv[]) {
  double startTime;
  startTime=timer();

  int canvasWidth=1280;
  int canvasHeight=720;

  int i;

#ifdef __EMSCRIPTEN__

  canvasWidth=640;
  canvasHeight=480;
#else

  char mode='p';


  for(i=1;i<argc;i++) {
    if(argv[i][0]=='-' && argv[i][1]=='m' &&
       (argv[i][2]=='v' || argv[i][2]=='p' || argv[i][2]=='g')
       && argv[i][3]==0) {
      mode=argv[i][2];
    } else if(argv[i][0]=='-' && argv[i][1]=='w') {
      canvasWidth=atoi(&argv[i][2]);
    } else if(argv[i][0]=='-' && argv[i][1]=='h') {
      canvasHeight=atoi(&argv[i][2]);
    } else {
      //err
    }
  }
#endif
  int canvasSize=canvasWidth*canvasHeight;




  float aspectRatio=(float)canvasWidth/(float)canvasHeight;
  float yFov=(float)M_PI_4;


  struct Scene scene;

  if(!initScene(&scene)) {
#ifdef _MSC_VER
  system("pause");
#endif

    return 1;
  }


  //

  struct Ray **rayStacks;
  struct KdTraverseNode **traverseStacks;

  rayStacks=(struct Ray**)malloc(sizeof(void*)*(WORKER_THREADS+1));
  traverseStacks=(struct KdTraverseNode**)malloc(sizeof(void*)*(WORKER_THREADS+1));

  for(i=0;i<WORKER_THREADS+1;i++) {
    traverseStacks[i]=(struct KdTraverseNode*)
      malloc(sizeof(struct KdTraverseNode)*scene.kdtreeDepth);
    rayStacks[i]=(struct Ray*)malloc(sizeof(struct Ray)*RAY_MAX_DEPTH);
  }



  //
  printf("Init took %g seconds.\n",timer()-startTime);
  startTime=timer();


#ifdef __EMSCRIPTEN__
  struct EmData emData;
 emData.canvasWidth=canvasWidth;
 emData.canvasHeight=canvasHeight;
 emData.aspectRatio=aspectRatio;
 emData.yFov=yFov;
 emData.traverseStacks=traverseStacks;
 emData.rayStacks=rayStacks;
 emData.pixelIndex=0;
 emData.scene=&scene;
 emscripten_set_canvas_size(canvasWidth,canvasHeight);
 em_canvas_paint_init(canvasWidth,canvasHeight);

 emscripten_set_main_loop_arg(em_main_loop,&emData,0,1);
#else
 //
 if(mode=='v') {
   printf("generating video.\n");
   create_video(canvasWidth,canvasHeight,&scene,aspectRatio,yFov,
                rayStacks,traverseStacks
                );
 } else if(mode=='p') {
   printf("generating picture.\n");
   create_screenshot(canvasWidth,canvasHeight,&scene,aspectRatio,yFov,
                     rayStacks,traverseStacks
                     );
 } else if(mode=='b') {
   printf("generating picture (bi directional lighting test).\n");
   create_screenshot2(canvasWidth,canvasHeight,&scene,aspectRatio,yFov,
                     rayStacks,traverseStacks
                     );
 } else if(mode=='g') {
   printf("generating gbuffer.\n");
   create_gbuffer(canvasWidth,canvasHeight,&scene,aspectRatio,yFov,
                  rayStacks,traverseStacks);
 }

#endif

 printf("Output took %g seconds.\n",timer()-startTime);



#ifdef _MSC_VER
  system("pause");
#endif
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movl %edi, %ebx
xorl %eax, %eax
callq 0x411cfb
movsd %xmm0, 0x10(%rsp)
movslq %ebx, %r15
movl $0x500, %ebp            # imm = 0x500
movl $0x2d0, %ebx            # imm = 0x2D0
movb $0x70, 0xb(%rsp)
pushq $0x1
popq %r13
movl $0x8201, %r12d          # imm = 0x8201
cmpq %r15, %r13
jge 0x406cec
movq (%r14,%r13,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x406ce7
movzbl 0x1(%rdi), %eax
cmpl $0x68, %eax
je 0x406cdc
cmpl $0x77, %eax
je 0x406ccf
cmpl $0x6d, %eax
jne 0x406ce7
movzbl 0x2(%rdi), %eax
leal -0x67(%rax), %ecx
cmpl $0xf, %ecx
ja 0x406ce7
btl %ecx, %r12d
jae 0x406ce7
cmpb $0x0, 0x3(%rdi)
jne 0x406ce7
movb %al, 0xb(%rsp)
jmp 0x406ce7
addq $0x2, %rdi
callq 0x422e30
movl %eax, %ebp
jmp 0x406ce7
addq $0x2, %rdi
callq 0x422e30
movl %eax, %ebx
incq %r13
jmp 0x406c90
leaq 0x18(%rsp), %rdi
callq 0x406add
testb %al, %al
je 0x406dc5
cvtsi2ss %ebp, %xmm1
cvtsi2ss %ebx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
pushq $0x8
popq %r15
movq %r15, %rdi
callq 0x442160
movq %rax, %r14
movq %r15, %rdi
callq 0x442160
movq %rax, %r15
movslq 0x48(%rsp), %rdi
shlq $0x4, %rdi
callq 0x442160
movq %rax, (%r15)
movl $0x100, %edi            # imm = 0x100
callq 0x442160
movq %rax, (%r14)
xorl %eax, %eax
callq 0x411cfb
subsd 0x10(%rsp), %xmm0
leaq 0xccd91(%rip), %rdi     # 0x4d3aed
movb $0x1, %al
callq 0x424bd0
xorl %eax, %eax
callq 0x411cfb
movb 0xb(%rsp), %al
cmpb $0x62, %al
movsd %xmm0, 0x10(%rsp)
je 0x406e2d
movzbl %al, %eax
cmpl $0x67, %eax
je 0x406dcd
cmpl $0x70, %eax
je 0x406dfd
cmpl $0x76, %eax
jne 0x406e5b
leaq 0xccde2(%rip), %rdi     # 0x4d3b7b
callq 0x42c160
leaq 0x18(%rsp), %rdx
movss 0xccb85(%rip), %xmm1    # 0x4d3930
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x405e34
jmp 0x406e5b
pushq $0x1
popq %rbx
jmp 0x406e78
leaq 0xccd4c(%rip), %rdi     # 0x4d3b20
callq 0x42c160
leaq 0x18(%rsp), %rdx
movss 0xccb4a(%rip), %xmm1    # 0x4d3930
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x4066d0
jmp 0x406e5b
leaq 0xccd63(%rip), %rdi     # 0x4d3b67
callq 0x42c160
leaq 0x18(%rsp), %rdx
movss 0xccb1a(%rip), %xmm1    # 0x4d3930
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406268
jmp 0x406e5b
leaq 0xccd00(%rip), %rdi     # 0x4d3b34
callq 0x42c160
leaq 0x18(%rsp), %rdx
movss 0xccaea(%rip), %xmm1    # 0x4d3930
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x40649c
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x411cfb
subsd 0x10(%rsp), %xmm0
leaq 0xccc93(%rip), %rdi     # 0x4d3b04
movb $0x1, %al
callq 0x424bd0
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)