// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %r9, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movl 0xc8(%rsp), %eax
testl %eax, %eax
jle 0x415915
movq %r8, %r14
movq %rdx, %r12
movq %rsi, %r13
movq 0xc0(%rsp), %r15
addq $0xc, %rdi
movq %rdi, 0x30(%rsp)
movl %eax, %ebx
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
leaq 0x4c(%rsp), %rbp
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
movss %xmm0, 0xc(%rsp)
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0x4(%rsp)
leaq 0xc(%r15), %rsi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x407641
movq %rbp, %rdi
callq 0x407569
movaps %xmm0, %xmm1
movss %xmm0, 0x1c(%rsp)
movss 0xbfcb6(%rip), %xmm0    # 0x4d5490
divss %xmm1, %xmm0
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x40766c
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movss 0x24(%rsp), %xmm0
movq 0x48(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq %r14, %r8
movq 0xa0(%rsp), %r9
pushq 0xb8(%rsp)
movl 0xb8(%rsp), %eax
pushq %rax
pushq 0xb8(%rsp)
callq 0x414edb
addq $0x20, %rsp
testb %al, %al
jne 0x415906
movss 0x10(%r13), %xmm1
movss 0x14(%r13), %xmm2
movq %r15, %rdi
movq %rbp, %rsi
movss 0x1c(%rsp), %xmm0
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x41541a
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
addss %xmm0, %xmm1
movss 0x18(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0x18(%rsp)
movss 0x28(%rsp), %xmm1
addss %xmm0, %xmm1
movss 0x4(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0x4(%rsp)
movss 0x2c(%rsp), %xmm1
addss %xmm0, %xmm1
movss 0x8(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0x8(%rsp)
movss (%r15), %xmm1
mulss %xmm0, %xmm1
movss 0xc(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0xc(%rsp)
movss 0x4(%r15), %xmm1
mulss %xmm0, %xmm1
movss 0x10(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0x10(%rsp)
mulss 0x8(%r15), %xmm0
movss 0x14(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
addq $0x24, %r15
decq %rbx
jne 0x4157b2
jmp 0x41593c
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0xc(%rsp)
movss %xmm0, 0x10(%rsp)
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x18(%rsp)
movq 0xd0(%rsp), %rax
movss 0x18(%rsp), %xmm0
movss %xmm0, (%rax)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movq 0xd8(%rsp), %rax
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq