// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/utils.c
// Function: renderScene2
void renderScene2(int updateFrom,int updateTo,
                 int width, int height,
                 float aspect,float yFov,
                 const float *minBound,const float *maxBound,
                 const float *origin,
                 const float *rotMat,
                 struct Ray *rayStack,
                 struct KdTraverseNode *traverseStack,
                 struct KdNode *rootNode,
                 struct Triangle *triangles,int trianglesNum,
                 struct Sphere *spheres,int spheresNum,
                 struct PointLight *pointLights,int pointLightsNum,


                 sobol_state_t *qrnd,


                 float *canvas,

                 int maxBounces,int maxSplits) {

  int denom=maxBounces*maxSplits;
  float glossyRatio=(denom!=0)?(1.0f/(float)denom)*0.2f:0.0f;


  float d;
  int i;

  //
  d=1.0f/tanf(yFov/2.0f);

  //
  int raycastsNum;


  for(i=updateFrom;i<updateTo;i++) {
    int x,y;
    float u,v,px,py;
    float pixelCol[3]={0.0f,0.0f,0.0f};

    raycastsNum=1;

    rayStack[raycastsNum-1].bounces=0;
    rayStack[raycastsNum-1].origin[0]=origin[0];
    rayStack[raycastsNum-1].origin[1]=origin[1];
    rayStack[raycastsNum-1].origin[2]=origin[2];
    rayStack[raycastsNum-1].ratio=1.0f;

    x=i%width;
    y=i/width;
    u=x/(float)width;
    v=1.0f-y/(float)height; //flip
    px=2.0f*u-1.0f;
    py=2.0f*v-1.0f;

    float bla[3]={px*aspect,py,-d};//rhs


    mat3_mulVector(rayStack[raycastsNum-1].vec,rotMat,bla);
    vec3_normal(rayStack[raycastsNum-1].vec,rayStack[raycastsNum-1].vec);

    while(raycastsNum > 0) {
      float curCol[3]={0.0f,0.0f,0.0f};
      float nor[3],colPt[3];
      float illumCol[3]={0.0f,0.0f,0.0f};
      float specCol[3]={0.0f,0.0f,0.0f};
      struct Material *material;

      //
      raycastsNum--;
      struct Ray ray=rayStack[raycastsNum];

      if(!intersectSceneObject(&ray,traverseStack,minBound,maxBound,rootNode,
                               triangles,trianglesNum,spheres,spheresNum,
                               colPt,nor,&material)) {

        break;
      }

      calcLights(&ray,material,colPt,nor,
                 traverseStack,minBound,maxBound,rootNode,
                 triangles,trianglesNum,spheres,spheresNum,
                 pointLights,pointLightsNum,illumCol,specCol);

      //
      curCol[0]=(material->col[0]*illumCol[0]+specCol[0]);
      curCol[1]=material->col[1]*illumCol[1]+specCol[1];
      curCol[2]=material->col[2]*illumCol[2]+specCol[2];

      curCol[0]*=ray.ratio;
      curCol[1]*=ray.ratio;
      curCol[2]*=ray.ratio;

      pixelCol[0]+=curCol[0];
      pixelCol[1]+=curCol[1];
      pixelCol[2]+=curCol[2];




      ///here



    }

    //
    canvas[(i-updateFrom)*3]=pixelCol[0];
    canvas[(i-updateFrom)*3+1]=pixelCol[1];
    canvas[(i-updateFrom)*3+2]=pixelCol[2];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r9, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movss %xmm0, 0x6c(%rsp)
movl %ecx, %ebp
movl %edx, 0x58(%rsp)
movl %esi, %ebx
movl %edi, %r12d
mulss 0xbe98c(%rip), %xmm1    # 0x4d6488
movaps %xmm1, %xmm0
callq 0x419040
movl %ebx, 0x68(%rsp)
cmpl %ebx, %r12d
jge 0x417df9
movq 0x1a0(%rsp), %r15
movq 0x160(%rsp), %rbx
movq 0x150(%rsp), %rcx
xorps %xmm1, %xmm1
cvtsi2ssl 0x58(%rsp), %xmm1
movss %xmm1, 0x64(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %ebp, %xmm1
movss %xmm1, 0x60(%rsp)
movss 0xc1937(%rip), %xmm1    # 0x4d9484
divss %xmm0, %xmm1
movss %xmm1, 0x5c(%rsp)
leaq 0xc(%rcx), %rax
movq %rax, 0xb0(%rsp)
addq $0x8, %r15
movq 0x150(%rsp), %r13
xorl %r14d, %r14d
movl %r14d, 0x18(%r13)
movq 0x140(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, (%r13)
movss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%r13)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x8(%r13)
movl $0x3f800000, 0x1c(%r13) # imm = 0x3F800000
movl %r12d, %eax
cltd
idivl 0x58(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x64(%rsp), %xmm0
divss 0x60(%rsp), %xmm1
movss 0xbfd50(%rip), %xmm2    # 0x4d7920
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc18a4(%rip), %xmm1    # 0x4d9484
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0x6c(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
movss %xmm2, 0xa8(%rsp)
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0xac(%rsp)
movq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movq 0x148(%rsp), %rsi
leaq 0xa4(%rsp), %rdx
callq 0x40865a
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x40761b
movl %r14d, 0x88(%rsp)
movq %r14, 0x80(%rsp)
movl %r14d, 0x78(%rsp)
movq %r14, 0x70(%rsp)
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movaps %xmm1, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xfc(%rsp), %rbp
movq %rbp, 0x20(%rsp)
leaq 0xc4(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x170(%rsp), %eax
movl %eax, (%rsp)
leaq 0xd0(%rsp), %rdi
movq 0x158(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq %rbx, %r8
movq %rsi, %r13
movq %rbx, %r14
movq 0x168(%rsp), %rbx
movq %rbx, %r9
callq 0x416687
testb %al, %al
je 0x417dcf
movq %rbp, %rcx
movq 0xb8(%rsp), %rbp
leaq 0x70(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x190(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x188(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
leaq 0xd0(%rsp), %rdi
movq %rbp, %rsi
leaq 0xc4(%rsp), %rdx
movq %r13, %r8
movq 0x90(%rsp), %r9
callq 0x416841
movss 0x8(%rbp), %xmm0
mulss 0x88(%rsp), %xmm0
addss 0x78(%rsp), %xmm0
movss 0xec(%rsp), %xmm1
movsd (%rbp), %xmm2
movsd 0x80(%rsp), %xmm3
mulps %xmm2, %xmm3
movsd 0x70(%rsp), %xmm2
addps %xmm3, %xmm2
mulss %xmm1, %xmm0
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm2, %xmm1
addps 0xc063b(%rip), %xmm1    # 0x4d8400
addss 0xbe6cb(%rip), %xmm0    # 0x4d6498
jmp 0x417dd5
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movlps %xmm1, -0x8(%r15)
movss %xmm0, (%r15)
addq $0xc, %r15
incl %r12d
cmpl %r12d, 0x68(%rsp)
movq 0x160(%rsp), %rbx
jne 0x417b67
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)