// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %r9, 0x58(%rsp)
movq %r8, %r14
movq %rcx, 0x50(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq 0xe0(%rsp), %r15
movl 0xe8(%rsp), %eax
addq $0xc, %rdi
movq %rdi, 0x48(%rsp)
xorl %ebx, %ebx
testl %eax, %eax
cmovgl %eax, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x6c(%rsp), %rbp
subq $0x1, %rbx
jb 0x4132b8
leaq 0xc(%r15), %rsi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x406fba
movq %rbp, %rdi
callq 0x406f25
movaps %xmm0, %xmm1
movss %xmm0, 0x8(%rsp)
movss 0xbf2a6(%rip), %xmm0    # 0x4d2460
divss %xmm1, %xmm0
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x406fe5
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movss 0x10(%rsp), %xmm0
movq 0x60(%rsp), %rdx
movq 0xb8(%rsp), %rcx
movq %r14, %r8
movq 0xc0(%rsp), %r9
pushq 0xd8(%rsp)
movl 0xd8(%rsp), %eax
pushq %rax
pushq 0xd8(%rsp)
callq 0x412969
addq $0x20, %rsp
testb %al, %al
jne 0x4132af
movss 0x10(%r13), %xmm1
movss 0x14(%r13), %xmm2
movq %r15, %rdi
movq %rbp, %rsi
movss 0x8(%rsp), %xmm0
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x412e09
movss 0xc(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
addps %xmm2, %xmm1
movaps 0x30(%rsp), %xmm3
addps %xmm1, %xmm3
movaps %xmm3, 0x30(%rsp)
movss 0x68(%rsp), %xmm1
addss %xmm0, %xmm1
movss 0x4(%rsp), %xmm3
addss %xmm1, %xmm3
movss %xmm3, 0x4(%rsp)
movsd (%r15), %xmm1
mulps %xmm2, %xmm1
movaps 0x10(%rsp), %xmm2
addps %xmm1, %xmm2
movaps %xmm2, 0x10(%rsp)
mulss 0x8(%r15), %xmm0
movaps 0x20(%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, 0x20(%rsp)
addq $0x24, %r15
jmp 0x413188
movq 0xf0(%rsp), %rax
movaps 0x30(%rsp), %xmm0
movlps %xmm0, (%rax)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movq 0xf8(%rsp), %rax
movaps 0x10(%rsp), %xmm0
movlps %xmm0, (%rax)
movaps 0x20(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq