// File: /workspace/github_c_makefile_star2_O1/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, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movss %xmm0, 0x94(%rsp)
movl %ecx, %r15d
movl %edx, 0x64(%rsp)
movl %esi, %r12d
movl %edi, %ebp
mulss 0xbe970(%rip), %xmm1    # 0x4d54ac
movaps %xmm1, %xmm0
callq 0x418150
movl %r12d, 0x90(%rsp)
cmpl %r12d, %ebp
jge 0x416f05
movq 0x188(%rsp), %rax
movq 0x170(%rsp), %rcx
movq 0x160(%rsp), %rdx
xorps %xmm1, %xmm1
cvtsi2ssl 0x64(%rsp), %xmm1
movss %xmm1, 0x8c(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
movss %xmm1, 0x88(%rsp)
movq %rax, %r15
movss 0xc1769(%rip), %xmm1    # 0x4d8304
divss %xmm0, %xmm1
movss %xmm1, 0x84(%rsp)
leaq 0xc(%rdx), %rax
movq %rax, 0xc0(%rsp)
movslq %ebp, %rdx
movq %rcx, %rbp
movq %rdx, 0xb8(%rsp)
movq 0x160(%rsp), %rax
movl $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, 0xc8(%rsp)
movl %edx, %eax
cltd
idivl 0x64(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x8c(%rsp), %xmm0
divss 0x88(%rsp), %xmm1
movss 0xbe85d(%rip), %xmm2    # 0x4d5490
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc16c1(%rip), %xmm1    # 0x4d8304
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0x94(%rsp), %xmm0
movss %xmm0, 0xac(%rsp)
movss %xmm2, 0xb0(%rsp)
movss 0x84(%rsp), %xmm0
movss %xmm0, 0xb4(%rsp)
movq 0xc0(%rsp), %r13
movq %r13, %rdi
movq 0x158(%rsp), %rsi
leaq 0xac(%rsp), %rdx
callq 0x408297
movq %r13, %rdi
movq %r13, %rsi
callq 0x40759c
leaq 0xf0(%rsp), %r12
leaq 0xd8(%rsp), %rbx
leaq 0xe4(%rsp), %r14
movq 0x168(%rsp), %r13
xorps %xmm0, %xmm0
movss %xmm0, 0x60(%rsp)
movb $0x1, %al
movss %xmm0, 0x5c(%rsp)
movss %xmm0, 0x58(%rsp)
testb $0x1, %al
je 0x416eb3
xorl %eax, %eax
movl %eax, 0x80(%rsp)
movq %rax, 0x78(%rsp)
movl %eax, 0x70(%rsp)
movq %rax, 0x68(%rsp)
movq 0x160(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq %r15, 0x8(%rsp)
movl 0x180(%rsp), %eax
movl %eax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq 0x98(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq %rbp, %r8
movq 0x178(%rsp), %r9
callq 0x41557c
movq %rbp, %rcx
movl %eax, %ebp
testb %al, %al
je 0x416e80
movq 0xd0(%rsp), %r15
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 0x178(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq %r13, %r8
movq 0x98(%rsp), %r9
callq 0x415743
movss (%r15), %xmm0
movss 0x4(%r15), %xmm1
mulss 0x78(%rsp), %xmm0
addss 0x68(%rsp), %xmm0
mulss 0x7c(%rsp), %xmm1
addss 0x6c(%rsp), %xmm1
movss 0x8(%r15), %xmm2
movq 0x188(%rsp), %r15
mulss 0x80(%rsp), %xmm2
addss 0x70(%rsp), %xmm2
movss 0x10c(%rsp), %xmm3
mulss %xmm3, %xmm0
mulss %xmm3, %xmm1
mulss %xmm3, %xmm2
movss 0x60(%rsp), %xmm3
addss %xmm0, %xmm3
movss %xmm3, 0x60(%rsp)
movss 0x5c(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x5c(%rsp)
movss 0x58(%rsp), %xmm0
addss %xmm2, %xmm0
movss %xmm0, 0x58(%rsp)
xorl %eax, %eax
testb %bpl, %bpl
movq 0x170(%rsp), %rbp
movq 0x168(%rsp), %r13
leaq 0xe4(%rsp), %r14
leaq 0xd8(%rsp), %rbx
leaq 0xf0(%rsp), %r12
jne 0x416cde
movq 0xc8(%rsp), %rdx
movq %rdx, %rax
subq 0xb8(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x1b0(%rsp), %rcx
movss 0x60(%rsp), %xmm0
movss %xmm0, (%rcx,%rax,4)
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x4(%rcx,%rax,4)
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x8(%rcx,%rax,4)
incq %rdx
cmpl 0x90(%rsp), %edx
jne 0x416bc2
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)