// File: /workspace/github_c_makefile_star2_O3/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 0x41531b
movsd %xmm0, 0x10(%rsp)
cmpl $0x2, %ebx
jl 0x4073a8
movl %ebx, %r15d
movb $0x70, %r12b
movl $0x2d0, %ebx            # imm = 0x2D0
movl $0x500, %ebp            # imm = 0x500
movl $0x1, %r13d
movq (%r14,%r13,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x40739e
movzbl 0x1(%rdi), %eax
cmpl $0x68, %eax
je 0x407393
cmpl $0x77, %eax
je 0x407386
cmpl $0x6d, %eax
jne 0x40739e
movzbl 0x2(%rdi), %eax
leal -0x67(%rax), %ecx
cmpl $0xf, %ecx
ja 0x40739e
movl $0x8201, %edx           # imm = 0x8201
btl %ecx, %edx
jae 0x40739e
cmpb $0x0, 0x3(%rdi)
jne 0x40739e
movl %eax, %r12d
jmp 0x40739e
addq $0x2, %rdi
callq 0x426590
movl %eax, %ebp
jmp 0x40739e
addq $0x2, %rdi
callq 0x426590
movl %eax, %ebx
incq %r13
cmpq %r13, %r15
jne 0x407349
jmp 0x4073b5
movl $0x500, %ebp            # imm = 0x500
movl $0x2d0, %ebx            # imm = 0x2D0
movb $0x70, %r12b
leaq 0x18(%rsp), %rdi
callq 0x407183
movl $0x1, %r14d
testb %al, %al
je 0x407546
cvtsi2ss %ebp, %xmm1
xorps %xmm0, %xmm0
cvtsi2ss %ebx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
movl $0x8, %edi
callq 0x4458c0
movq %rax, %r14
movl $0x8, %edi
callq 0x4458c0
movq %rax, %r15
movslq 0x48(%rsp), %rdi
shlq $0x4, %rdi
callq 0x4458c0
movq %rax, (%r15)
movl $0x100, %edi            # imm = 0x100
callq 0x4458c0
movq %rax, (%r14)
xorl %eax, %eax
callq 0x41531b
subsd 0x10(%rsp), %xmm0
leaq 0xd070f(%rip), %rdi     # 0x4d7b3d
movb $0x1, %al
callq 0x428330
xorl %eax, %eax
callq 0x41531b
movsd %xmm0, 0x10(%rsp)
movzbl %r12b, %eax
cmpl $0x6f, %eax
jg 0x40748c
cmpl $0x62, %eax
je 0x4074ca
cmpl $0x67, %eax
jne 0x407528
leaq 0xd0710(%rip), %rdi     # 0x4d7b70
callq 0x42f8c0
leaq 0x18(%rsp), %rdx
movss 0xd0506(%rip), %xmm1    # 0x4d7978
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406d78
jmp 0x407528
cmpl $0x70, %eax
je 0x4074fa
cmpl $0x76, %eax
jne 0x407528
leaq 0xd072a(%rip), %rdi     # 0x4d7bcb
callq 0x42f8c0
leaq 0x18(%rsp), %rdx
movss 0xd04c5(%rip), %xmm1    # 0x4d7978
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406498
jmp 0x407528
leaq 0xd06b3(%rip), %rdi     # 0x4d7b84
callq 0x42f8c0
leaq 0x18(%rsp), %rdx
movss 0xd0495(%rip), %xmm1    # 0x4d7978
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406b3e
jmp 0x407528
leaq 0xd06b6(%rip), %rdi     # 0x4d7bb7
callq 0x42f8c0
leaq 0x18(%rsp), %rdx
movss 0xd0465(%rip), %xmm1    # 0x4d7978
movl %ebp, %edi
movl %ebx, %esi
movss 0xc(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406904
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x41531b
subsd 0x10(%rsp), %xmm0
leaq 0xd0615(%rip), %rdi     # 0x4d7b54
movb $0x1, %al
callq 0x428330
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq