// File: /workspace/github_c_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %r9, %r14
movq %r8, 0x90(%rsp)
movss %xmm0, 0x64(%rsp)
movl %ecx, %r13d
movl %edx, %r15d
movl %esi, 0x50(%rsp)
movl %edi, %r12d
movq 0x170(%rsp), %rbp
movq 0x160(%rsp), %rbx
mulss 0xbe130(%rip), %xmm1    # 0x4d247c
movaps %xmm1, %xmm0
callq 0x4158e0
movl %r15d, 0x60(%rsp)
cvtsi2ss %r15d, %xmm1
movss %xmm1, 0x5c(%rsp)
cvtsi2ss %r13d, %xmm1
movss %xmm1, 0x58(%rsp)
movss 0xbf8c9(%rip), %xmm1    # 0x4d3c40
divss %xmm0, %xmm1
movss %xmm1, 0x54(%rsp)
leaq 0xc(%rbx), %rax
movq %rax, 0xb0(%rsp)
movslq %r12d, %rdx
movq 0x178(%rsp), %r13
movq %r14, %r15
movslq 0x50(%rsp), %rcx
movq %rcx, 0xb8(%rsp)
leaq 0xe0(%rsp), %r12
movq %rdx, 0xa8(%rsp)
movq %r14, 0x88(%rsp)
cmpq 0xb8(%rsp), %rdx
jge 0x414697
movq 0x160(%rsp), %rax
andl $0x0, 0x18(%rax)
movq 0x150(%rsp), %rcx
movss (%rcx), %xmm0
movss %xmm0, (%rax)
movss 0x4(%rcx), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x8(%rcx), %xmm0
movss %xmm0, 0x8(%rax)
movl $0x3f800000, 0x1c(%rax) # imm = 0x3F800000
movq %rdx, 0xc0(%rsp)
movl %edx, %eax
cltd
idivl 0x60(%rsp)
cvtsi2ss %edx, %xmm0
cvtsi2ss %eax, %xmm1
divss 0x5c(%rsp), %xmm0
divss 0x58(%rsp), %xmm1
movss 0xbe030(%rip), %xmm2    # 0x4d2460
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xbf800(%rip), %xmm1    # 0x4d3c40
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0x64(%rsp), %xmm0
movss %xmm0, 0x9c(%rsp)
movss %xmm2, 0xa0(%rsp)
movss 0x54(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
movq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq 0x158(%rsp), %rsi
leaq 0x9c(%rsp), %rdx
callq 0x4079d0
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x406f42
xorps %xmm2, %xmm2
xorps %xmm0, %xmm0
movb $0x1, %al
testb $0x1, %al
movq 0x168(%rsp), %rsi
je 0x414666
movaps %xmm2, 0xd0(%rsp)
movss %xmm0, 0x50(%rsp)
andl $0x0, 0x80(%rsp)
andq $0x0, 0x78(%rsp)
andl $0x0, 0x70(%rsp)
andq $0x0, 0x68(%rsp)
movq 0x160(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
leaq 0xc8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x10c(%rsp), %r14
movq %r14, 0x20(%rsp)
leaq 0x100(%rsp), %rbx
movq %rbx, 0x18(%rsp)
movq 0x188(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x180(%rsp), %eax
movl %eax, (%rsp)
movq %r12, %rdi
movq 0x90(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x412f6b
testb %al, %al
je 0x414658
movq %r12, %r15
movq %rbp, %r12
movq %r13, %rbp
movq 0xc8(%rsp), %r13
leaq 0x68(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x1a0(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x188(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x180(%rsp), %eax
movl %eax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq 0x168(%rsp), %r8
movq 0x90(%rsp), %r9
callq 0x413125
movss 0x8(%r13), %xmm0
mulss 0x80(%rsp), %xmm0
addss 0x70(%rsp), %xmm0
movss 0xfc(%rsp), %xmm1
movsd (%r13), %xmm2
movq %rbp, %r13
movq %r12, %rbp
movq %r15, %r12
movq 0x88(%rsp), %r15
movsd 0x78(%rsp), %xmm3
mulps %xmm2, %xmm3
movsd 0x68(%rsp), %xmm2
addps %xmm3, %xmm2
mulss %xmm1, %xmm0
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm2, %xmm1
movaps 0xd0(%rsp), %xmm2
addps %xmm1, %xmm2
movss 0x50(%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
xorl %eax, %eax
jmp 0x4144a6
movss 0x50(%rsp), %xmm0
movaps 0xd0(%rsp), %xmm2
movq 0xc0(%rsp), %rdx
movq %rdx, %rax
subq 0xa8(%rsp), %rax
imulq $0xc, %rax, %rax
movq 0x1b0(%rsp), %rcx
movlps %xmm2, (%rcx,%rax)
movss %xmm0, 0x8(%rcx,%rax)
incq %rdx
jmp 0x4143c0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)