// File: /workspace/github_c_makefile_star2_O1/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 0x41415b
movsd %xmm0, 0x10(%rsp)
cmpl $0x2, %ebx
jl 0x40730b
movl %ebx, %r15d
movb $0x70, %r12b
movl $0x2d0, 0xc(%rsp)       # imm = 0x2D0
movl $0x500, %ebp            # imm = 0x500
movl $0x1, %r13d
movl $0x8201, %ebx           # imm = 0x8201
movq (%r14,%r13,8), %rdi
movb (%rdi), %al
cmpb $0x2d, %al
jne 0x407301
cmpb $0x6d, 0x1(%rdi)
jne 0x4072d3
movzbl 0x2(%rdi), %ecx
leal -0x67(%rcx), %edx
cmpl $0xf, %edx
ja 0x4072d3
btl %edx, %ebx
jae 0x4072d3
cmpb $0x2d, %al
setne %dl
cmpb $0x0, 0x3(%rdi)
sete %sil
movzbl %cl, %r8d
movzbl %r12b, %ecx
cmovel %r8d, %ecx
orb %dl, %sil
je 0x4072d7
movl %ecx, %r12d
jmp 0x407301
cmpb $0x2d, %al
jne 0x407301
cmpb $0x77, 0x1(%rdi)
jne 0x4072ea
addq $0x2, %rdi
callq 0x4256a0
movl %eax, %ebp
jmp 0x407301
cmpb $0x2d, %al
jne 0x407301
cmpb $0x68, 0x1(%rdi)
jne 0x407301
addq $0x2, %rdi
callq 0x4256a0
movl %eax, 0xc(%rsp)
incq %r13
cmpq %r13, %r15
jne 0x40728f
jmp 0x40731b
movl $0x500, %ebp            # imm = 0x500
movl $0x2d0, 0xc(%rsp)       # imm = 0x2D0
movb $0x70, %r12b
leaq 0x18(%rsp), %rdi
callq 0x4070c0
movl $0x1, %r14d
testb %al, %al
je 0x4074b0
cvtsi2ss %ebp, %xmm1
movl 0xc(%rsp), %ebx
xorps %xmm0, %xmm0
cvtsi2ss %ebx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movl $0x8, %edi
callq 0x4449d0
movq %rax, %r14
movl $0x8, %edi
callq 0x4449d0
movq %rax, %r15
movslq 0x48(%rsp), %rdi
shlq $0x4, %rdi
callq 0x4449d0
movq %rax, (%r15)
movl $0x100, %edi            # imm = 0x100
callq 0x4449d0
movq %rax, (%r14)
xorl %eax, %eax
callq 0x41415b
subsd 0x10(%rsp), %xmm0
leaq 0xcf765(%rip), %rdi     # 0x4d6afd
movb $0x1, %al
callq 0x427440
xorl %eax, %eax
callq 0x41415b
movsd %xmm0, 0x10(%rsp)
movzbl %r12b, %eax
cmpl $0x6f, %eax
jg 0x4073f6
cmpl $0x62, %eax
je 0x407434
cmpl $0x67, %eax
jne 0x407492
leaq 0xcf766(%rip), %rdi     # 0x4d6b30
callq 0x42e9d0
leaq 0x18(%rsp), %rdx
movss 0xcf584(%rip), %xmm1    # 0x4d6960
movl %ebp, %edi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406cb5
jmp 0x407492
cmpl $0x70, %eax
je 0x407464
cmpl $0x76, %eax
jne 0x407492
leaq 0xcf780(%rip), %rdi     # 0x4d6b8b
callq 0x42e9d0
leaq 0x18(%rsp), %rdx
movss 0xcf543(%rip), %xmm1    # 0x4d6960
movl %ebp, %edi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x4063d8
jmp 0x407492
leaq 0xcf709(%rip), %rdi     # 0x4d6b44
callq 0x42e9d0
leaq 0x18(%rsp), %rdx
movss 0xcf513(%rip), %xmm1    # 0x4d6960
movl %ebp, %edi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406a7b
jmp 0x407492
leaq 0xcf70c(%rip), %rdi     # 0x4d6b77
callq 0x42e9d0
leaq 0x18(%rsp), %rdx
movss 0xcf4e3(%rip), %xmm1    # 0x4d6960
movl %ebp, %edi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
movq %r14, %rcx
movq %r15, %r8
callq 0x406841
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x41415b
subsd 0x10(%rsp), %xmm0
leaq 0xcf66b(%rip), %rdi     # 0x4d6b14
movb $0x1, %al
callq 0x427440
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop