// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/utils.c
// Function: calcLights
void calcLights(const struct Ray *ray,struct Material *material,
                const float *colPt,const float *nor,
                struct KdTraverseNode *traverseStack,
                const float *minBound,const float *maxBound,
                struct KdNode *rootNode,
                struct Triangle *triangles,int trianglesNum,
                struct Sphere *spheres,int spheresNum,
                struct PointLight *pointLights,int pointLightsNum,
                float *colOut,float *specOut) {
  //
  int q;
  float resultCol[3]={0.0f,0.0f,0.0f};
  float resultSpec[3]={0.0f,0.0f,0.0f};

  //
  for(q=0;q<pointLightsNum;q++) {
    bool blocked=false;
    float col[3],spec;

    //light dir, dist
    float toLightDir[3],lightDist,invLightDist;
    vec3_sub(toLightDir,pointLights[q].pos,colPt);

    lightDist=vec3_length(toLightDir);
    invLightDist=1.0f/lightDist;
    vec3_mulScalar(toLightDir,toLightDir,invLightDist);

    //

#ifndef RUN_TEST
    if(intersectKdtreeP(colPt,toLightDir,lightDist,
    minBound,maxBound,
                        traverseStack,rootNode,
                        triangles,trianglesNum,spheres,spheresNum)) {
      blocked=true;
    }
#endif

    if(blocked) {
      continue;
    }

    calc_pointLight(&pointLights[q],toLightDir,lightDist,
                    ray->vec,
                    material->shininess,material->intensity,
                    nor,
                    col,&spec);

    resultCol[0]+=col[0]+spec;
    resultCol[1]+=col[1]+spec;
    resultCol[2]+=col[2]+spec;
    resultSpec[0]+=pointLights[q].col[0]*spec;
    resultSpec[1]+=pointLights[q].col[1]*spec;
    resultSpec[2]+=pointLights[q].col[2]*spec;
  }

  colOut[0]=resultCol[0];
  colOut[1]=resultCol[1];
  colOut[2]=resultCol[2];
  specOut[0]=resultSpec[0];
  specOut[1]=resultSpec[1];
  specOut[2]=resultSpec[2];

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movl 0xf8(%rsp), %eax
testl %eax, %eax
jle 0x4169d3
movq %r8, %r14
movq %rdx, %r12
movq %rsi, %r13
movq 0xf0(%rsp), %r15
addq $0xc, %rdi
movq %rdi, 0x58(%rsp)
movl %eax, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x7c(%rsp), %rbp
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0xc(%r15), %rsi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x40769c
movq %rbp, %rdi
callq 0x4075f1
movaps %xmm0, %xmm1
movss %xmm0, 0x8(%rsp)
movss 0xc104f(%rip), %xmm0    # 0x4d7920
divss %xmm1, %xmm0
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x4076c7
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movss 0x10(%rsp), %xmm0
movq 0x70(%rsp), %rdx
movq 0xc8(%rsp), %rcx
movq %r14, %r8
movq 0xd0(%rsp), %r9
pushq 0xe8(%rsp)
movl 0xe8(%rsp), %eax
pushq %rax
pushq 0xe8(%rsp)
callq 0x416058
addq $0x20, %rsp
testb %al, %al
jne 0x4169c4
movss 0x10(%r13), %xmm1
movss 0x14(%r13), %xmm2
movq %r15, %rdi
movq %rbp, %rsi
movss 0x8(%rsp), %xmm0
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x416525
movss 0xc(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
addps %xmm2, %xmm1
movaps 0x40(%rsp), %xmm3
addps %xmm1, %xmm3
movaps %xmm3, 0x40(%rsp)
movss 0x78(%rsp), %xmm1
addss %xmm0, %xmm1
movaps 0x20(%rsp), %xmm3
addss %xmm1, %xmm3
movaps %xmm3, 0x20(%rsp)
movsd (%r15), %xmm1
mulps %xmm2, %xmm1
movaps 0x10(%rsp), %xmm2
addps %xmm1, %xmm2
movaps %xmm2, 0x10(%rsp)
mulss 0x8(%r15), %xmm0
movaps 0x30(%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, 0x30(%rsp)
addq $0x24, %r15
decq %rbx
jne 0x4168a9
jmp 0x4169f0
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x100(%rsp), %rax
movaps 0x40(%rsp), %xmm0
movlps %xmm0, (%rax)
movaps 0x20(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movq 0x108(%rsp), %rax
movaps 0x10(%rsp), %xmm0
movlps %xmm0, (%rax)
movaps 0x30(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq