// File: /workspace/github_cpp_makefile_star2_O3/srajangarg[P]raytracer/src/main.cpp
// Function: getShadedColor(Primitive const&, Vec3 const&, Ray const&)
RGB
getShadedColor(Primitive const & primitive, Vec3 const & pos, Ray const & ray)
{
  RGB amb(0, 0, 0), lamb(0, 0, 0), spec(0, 0, 0);
  RGB C(primitive.getColor()), S, I;
  Vec3 r, u, n, i;
  bool use_dist;

  double ma = primitive.getMaterial().getMA();
  double ms = primitive.getMaterial().getMS();
  double ml = primitive.getMaterial().getML();
  double msp = primitive.getMaterial().getMSP();
  double msm = primitive.getMaterial().getMSM();
  double reach_light, ratio = 0, trans;
  
  // ambient component : Ma * C * A
  amb = ma * C * world->getAmbientLightColor();
  
  for (auto it = world->lightsBegin(); it != world->lightsEnd(); it++)
  {  
    trans = 1;
    auto light = *it;
    vector<Ray> shadows = light->getShadowRays(pos, use_dist);
    reach_light = shadows.size();

    // iterate over shadows
    for (auto shadow : shadows)
    {
      auto inter = world->intersect(shadow);

      if (inter != NULL) {
        // if the light is passing through a transparent object, consider it
        if (inter->getMaterial().getMT() > 0)
            trans = inter->getMaterial().getMT();
        // else decrease the number of shadow rays reaching the object
        else if (not use_dist or (shadow.minT() > 0 and shadow.minT() < 1))
            reach_light--;
      }
    }

    // how many shadow rays did not collide
    ratio = reach_light / (double) shadows.size();
    // if none of them passed thorugh, skip lambertian and specular
    if (ratio < EPS)
      continue;

    // normal and incidence vectors, light color
    n = primitive.calculateNormal(pos);
    i = light->getIncidenceVector(pos);
    I = light->getColor(pos);

    // lambertian component : Ml * C * I * max(i.n, 0)
    lamb += trans * ratio * ml * C * I * std::max(i * n, (double)0);

    // S                    : Msm * C + (1-Msm) * RBG(1, 1, 1)  
    // r                    : 2(i.n)n  - i   
    S = msm * C + (1 - msm) * RGB(1, 1, 1); 
    r = 2*((i * n) * n) - i;
    u = ray.direction();
    u.normalize();

    // specular component   : Ms * S * I * (max(-r.u, 0))^Msp                 
    spec += trans * ratio * ms * S * I * pow(std::max(-r*u, (double)0), msp);
  }

  return amb + spec + lamb;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rcx, 0xf0(%rsp)
movq %rdx, %r15
movq %rsi, %rax
movq %rdi, 0xe0(%rsp)
movsd 0x118(%rsi), %xmm0
movaps %xmm0, 0x20(%rsp)
movss 0x120(%rsi), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x130(%rsi), %xmm0
movss %xmm0, 0x10(%rsp)
cvtps2pd 0x124(%rsi), %xmm0
movaps %xmm0, 0x190(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x12c(%rsi), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0xee8e(%rip), %rsi      # 0x37510
movq %rax, 0xf8(%rsp)
movups 0x108(%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0x28288
movaps (%rbx), %xmm0
movaps %xmm0, 0x160(%rsp)
movsd 0x10(%rbx), %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0xee4a(%rip), %rax      # 0x37510
movq 0x20(%rax), %r12
cmpq 0x28(%rax), %r12
je 0x28bf7
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
movapd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm1
mulsd %xmm0, %xmm1
movsd 0x2a55(%rip), %xmm2     # 0x2b148
subsd %xmm0, %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x30(%rsp), %xmm4
mulpd %xmm4, %xmm0
addsd %xmm2, %xmm1
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
addpd %xmm0, %xmm2
movapd %xmm2, 0x180(%rsp)
movapd %xmm3, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm0, 0x170(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
leaq 0x120(%rsp), %rbx
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movq (%r12), %r13
movq (%r13), %rax
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
leaq 0xf(%rsp), %rcx
callq *0x28(%rax)
movq 0x68(%rsp), %rbp
movq 0x70(%rsp), %r14
movq %r14, %rax
subq %rbp, %rax
sarq $0x6, %rax
movq %rax, %xmm0
punpckldq 0x4db2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4dba(%rip), %xmm0     # 0x2d550
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
cmpq %rbp, %r14
je 0x288bd
movapd %xmm2, 0x10(%rsp)
movsd 0x298f(%rip), %xmm0     # 0x2b148
movaps %xmm0, 0x50(%rsp)
movups (%rbp), %xmm0
movaps %xmm0, 0x120(%rsp)
movups 0x10(%rbp), %xmm0
movaps %xmm0, 0x130(%rsp)
movups 0x20(%rbp), %xmm0
movaps %xmm0, 0x140(%rsp)
movupd 0x30(%rbp), %xmm0
movapd %xmm0, 0x150(%rsp)
movq 0xed19(%rip), %rdi      # 0x37510
movq %rbx, %rsi
callq 0x281dc
testq %rax, %rax
je 0x2882c
movss 0x138(%rax), %xmm0
ucomiss 0x4d5d(%rip), %xmm0     # 0x2d570
movapd 0x30(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
jbe 0x2883a
cvtss2sd %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
jmp 0x28876
movapd 0x30(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
jmp 0x28876
cmpb $0x1, 0xf(%rsp)
jne 0x28862
movsd 0x150(%rsp), %xmm0
ucomisd 0x303e(%rip), %xmm0     # 0x2b890
jbe 0x28876
movsd 0x28ec(%rip), %xmm1     # 0x2b148
ucomisd %xmm0, %xmm1
jbe 0x28876
movapd 0x10(%rsp), %xmm0
addsd 0x4cb8(%rip), %xmm0     # 0x2d528
movapd %xmm0, 0x10(%rsp)
addq $0x40, %rbp
cmpq %r14, %rbp
jne 0x287be
movq 0x68(%rsp), %rbp
movq 0x70(%rsp), %rax
subq %rbp, %rax
sarq $0x6, %rax
movq %rax, %xmm1
punpckldq 0x4c9f(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4ca7(%rip), %xmm1     # 0x2d550
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movapd 0x10(%rsp), %xmm1
jmp 0x288de
movapd %xmm2, %xmm0
movsd 0x287f(%rip), %xmm1     # 0x2b148
movaps %xmm1, 0x50(%rsp)
movapd %xmm2, %xmm1
movapd 0x30(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
divsd %xmm0, %xmm1
movsd 0x4c46(%rip), %xmm0     # 0x2d530
movapd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
ja 0x28bbf
movq 0xf8(%rsp), %rsi
movq (%rsi), %rax
movq %rbx, %rdi
movq %r15, %rdx
callq *0x18(%rax)
movsd 0x120(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movsd 0x128(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
movq (%r13), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq *0x20(%rax)
movsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0x128(%rsp), %xmm0
movsd %xmm0, 0x108(%rsp)
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movq (%r13), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq *0x18(%rax)
movapd 0x50(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
movsd 0xb0(%rsp), %xmm7
movapd %xmm7, %xmm0
movsd 0x108(%rsp), %xmm4
mulsd %xmm4, %xmm0
movsd 0x100(%rsp), %xmm5
movapd %xmm5, %xmm1
movapd 0xc0(%rsp), %xmm6
mulsd %xmm6, %xmm1
addsd %xmm0, %xmm1
movsd 0x110(%rsp), %xmm3
movapd %xmm3, %xmm0
movsd 0x118(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movapd %xmm1, 0x1a0(%rsp)
mulsd %xmm0, %xmm6
mulsd %xmm0, %xmm7
mulsd %xmm2, %xmm0
addsd %xmm6, %xmm6
addsd %xmm7, %xmm7
addsd %xmm0, %xmm0
subsd %xmm5, %xmm6
xorpd 0x272a(%rip), %xmm6     # 0x2b160
subsd %xmm4, %xmm7
subsd %xmm3, %xmm0
movaps 0x120(%rsp), %xmm2
movaps %xmm2, 0x10(%rsp)
movsd 0x130(%rsp), %xmm2
movaps %xmm2, 0xb0(%rsp)
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
movapd %xmm2, 0xc0(%rsp)
movq 0xf0(%rsp), %rax
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
movsd 0x28(%rax), %xmm3
movapd %xmm2, %xmm4
mulsd %xmm2, %xmm4
movapd %xmm1, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm5, %xmm4
sqrtsd %xmm4, %xmm4
movsd 0x2698(%rip), %xmm5     # 0x2b148
divsd %xmm4, %xmm5
mulsd %xmm5, %xmm1
mulsd %xmm5, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm3, %xmm5
mulsd %xmm0, %xmm5
mulsd %xmm6, %xmm1
subsd %xmm2, %xmm1
subsd %xmm5, %xmm1
xorpd %xmm0, %xmm0
maxsd %xmm1, %xmm0
movsd 0xe8(%rsp), %xmm1
callq 0x45a0
movapd 0x50(%rsp), %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
mulpd 0x190(%rsp), %xmm4
movapd 0x170(%rsp), %xmm1
mulpd %xmm4, %xmm1
movapd 0xb0(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm1, %xmm2
movapd 0x1a0(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm2, %xmm1
movapd %xmm1, %xmm5
movapd %xmm4, %xmm1
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
movapd 0x30(%rsp), %xmm2
mulpd %xmm2, %xmm1
movapd 0x10(%rsp), %xmm6
mulpd %xmm6, %xmm1
mulpd 0xc0(%rsp), %xmm1
movapd 0x90(%rsp), %xmm3
addpd %xmm1, %xmm3
movapd %xmm3, 0x90(%rsp)
movapd %xmm4, %xmm1
unpckhpd %xmm4, %xmm1            # xmm1 = xmm1[1],xmm4[1]
mulpd 0x180(%rsp), %xmm1
mulpd %xmm6, %xmm1
mulpd %xmm0, %xmm1
movapd 0xa0(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0xa0(%rsp)
movapd 0x80(%rsp), %xmm0
addpd %xmm5, %xmm0
movapd %xmm0, 0x80(%rsp)
movq 0x68(%rsp), %rbp
movapd 0x20(%rsp), %xmm3
testq %rbp, %rbp
je 0x28be0
movq 0x78(%rsp), %rsi
subq %rbp, %rsi
movq %rbp, %rdi
callq 0x4310
movapd 0x20(%rsp), %xmm3
movapd 0x30(%rsp), %xmm2
addq $0x8, %r12
movq 0xe925(%rip), %rax      # 0x37510
cmpq 0x28(%rax), %r12
jne 0x28752
jmp 0x28c22
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd 0x30(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
xorps %xmm0, %xmm0
cvtss2sd 0x4c(%rsp), %xmm0
mulsd %xmm0, %xmm3
mulsd 0xd8(%rsp), %xmm3
movapd 0x80(%rsp), %xmm4
movapd %xmm4, %xmm1
unpckhpd %xmm4, %xmm1            # xmm1 = xmm1[1],xmm4[1]
addsd %xmm3, %xmm1
addsd %xmm4, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm0, %xmm2
mulpd 0x160(%rsp), %xmm2
addpd 0xa0(%rsp), %xmm2
addpd 0x90(%rsp), %xmm2
movq 0xe0(%rsp), %rax
movupd %xmm2, (%rax)
movsd %xmm1, 0x10(%rax)
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28c9d
jmp 0x28c9d
jmp 0x28c9d
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x28cb7
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x4310
movq %rbx, %rdi
callq 0x45b0