// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, 0xc8(%rsp)
movq %rdx, %rbp
movq %rsi, %rax
movq %rdi, 0xb8(%rsp)
movsd 0x118(%rsi), %xmm1
movapd %xmm1, 0x160(%rsp)
movss 0x120(%rsi), %xmm0
movss %xmm0, 0x2c(%rsp)
cvtps2pd 0x124(%rsi), %xmm0
movaps %xmm0, 0x190(%rsp)
cvtss2sd 0x12c(%rsi), %xmm0
movsd %xmm0, 0xc0(%rsp)
cvtss2sd 0x130(%rsi), %xmm2
movaps %xmm2, 0x30(%rsp)
movq 0x105a6(%rip), %rsi     # 0x2f548
mulsd %xmm2, %xmm1
movsd 0x2152(%rip), %xmm0     # 0x21100
subsd %xmm2, %xmm0
movapd %xmm0, 0x50(%rsp)
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
movq %rax, 0xd0(%rsp)
movups 0x108(%rax), %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x120(%rsp), %r13
movq %r13, %rdi
callq 0x1eb66
movaps (%r13), %xmm0
movaps %xmm0, 0x170(%rsp)
movsd 0x10(%r13), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x1053f(%rip), %rax     # 0x2f548
movq 0x20(%rax), %rcx
movapd 0x30(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
addpd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movapd 0x160(%rsp), %xmm0
unpcklpd 0x10(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movapd %xmm0, 0x180(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
leaq 0x108(%rsp), %r12
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
cmpq 0x28(%rax), %rcx
je 0x1f4a1
movq %rcx, 0x100(%rsp)
movq (%rcx), %rbx
movq (%rbx), %rax
movq %r12, %r15
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %r14
movq %rbp, %rdx
leaq 0xf(%rsp), %rcx
callq *0x28(%rax)
movq 0x108(%rsp), %rbp
movq 0x110(%rsp), %r12
movq %r12, %rax
subq %rbp, %rax
sarq $0x6, %rax
movq %rax, %xmm0
punpckldq 0x4350(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4358(%rip), %xmm0     # 0x23420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
movsd 0x201e(%rip), %xmm0     # 0x21100
movaps %xmm0, 0x30(%rsp)
cmpq %r12, %rbp
je 0x1f197
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 0x1041f(%rip), %rdi     # 0x2f548
movq %r13, %rsi
callq 0x1eaea
testq %rax, %rax
je 0x1f18e
movss 0x138(%rax), %xmm0
ucomiss 0x42fb(%rip), %xmm0     # 0x23440
jbe 0x1f152
cvtss2sd %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
jmp 0x1f18e
cmpb $0x1, 0xf(%rsp)
jne 0x1f17a
movsd 0x150(%rsp), %xmm0
ucomisd 0x2796(%rip), %xmm0     # 0x21900
jbe 0x1f18e
movsd 0x1f8c(%rip), %xmm1     # 0x21100
ucomisd %xmm0, %xmm1
jbe 0x1f18e
movapd 0x10(%rsp), %xmm0
addsd 0x40a8(%rip), %xmm0     # 0x23230
movapd %xmm0, 0x10(%rsp)
addq $0x40, %rbp
jmp 0x1f0e7
movq 0x110(%rsp), %rax
subq 0x108(%rsp), %rax
sarq $0x6, %rax
movq %rax, %xmm0
punpckldq 0x4258(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4260(%rip), %xmm0     # 0x23420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd 0x421a(%rip), %xmm0     # 0x233f8
movapd %xmm2, 0x10(%rsp)
ucomisd %xmm2, %xmm0
jbe 0x1f1f5
movq %r15, %r12
movq %r14, %rbp
jmp 0x1f481
movq 0xd0(%rsp), %rsi
movq (%rsi), %rax
movq %r13, %rdi
movq %r14, %rbp
movq %r14, %rdx
callq *0x18(%rax)
movq %r15, %r12
movsd 0x120(%rsp), %xmm0
movaps %xmm0, 0x1c0(%rsp)
movsd 0x128(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0xf8(%rsp)
movq (%rbx), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x20(%rax)
movsd 0x120(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0x128(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movq (%rbx), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x18(%rax)
movaps 0x120(%rsp), %xmm0
movaps %xmm0, 0x1b0(%rsp)
movsd 0x130(%rsp), %xmm0
movaps %xmm0, 0x1a0(%rsp)
movq 0xc8(%rsp), %rax
movups 0x18(%rax), %xmm0
movaps %xmm0, 0x90(%rsp)
movsd 0x28(%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xa1b2
movsd 0xf0(%rsp), %xmm4
movapd %xmm4, %xmm0
movsd 0xe0(%rsp), %xmm6
mulsd %xmm6, %xmm0
movsd 0xe8(%rsp), %xmm5
movapd %xmm5, %xmm1
movapd 0x1c0(%rsp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xd8(%rsp), %xmm7
movapd %xmm7, %xmm0
movsd 0xf8(%rsp), %xmm3
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
mulsd %xmm0, %xmm3
addsd %xmm3, %xmm3
subsd %xmm7, %xmm3
mulsd %xmm0, %xmm4
addsd %xmm4, %xmm4
subsd %xmm6, %xmm4
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm2
subsd %xmm5, %xmm2
xorpd 0x1dc6(%rip), %xmm2     # 0x21130
movapd 0x30(%rsp), %xmm1
mulsd 0x10(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
mulsd 0x98(%rsp), %xmm4
mulsd 0x90(%rsp), %xmm2
mulsd 0xa0(%rsp), %xmm3
subsd %xmm4, %xmm2
subsd %xmm3, %xmm2
xorpd %xmm0, %xmm0
maxsd %xmm2, %xmm0
movsd 0xc0(%rsp), %xmm1
callq 0x45b0
movapd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm1
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
movapd 0x30(%rsp), %xmm5
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
mulpd 0x190(%rsp), %xmm5
movapd 0x180(%rsp), %xmm2
mulpd %xmm5, %xmm2
movapd 0x1a0(%rsp), %xmm3
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm2, %xmm3
movapd %xmm4, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm3, %xmm2
movapd %xmm2, %xmm4
movapd %xmm5, %xmm2
unpcklpd %xmm5, %xmm2            # xmm2 = xmm2[0],xmm5[0]
mulpd 0x40(%rsp), %xmm2
movapd 0x1b0(%rsp), %xmm3
mulpd %xmm3, %xmm2
mulpd %xmm1, %xmm2
movapd 0x70(%rsp), %xmm1
addpd %xmm2, %xmm1
movapd %xmm1, 0x70(%rsp)
movapd %xmm5, %xmm1
unpckhpd %xmm5, %xmm1            # xmm1 = xmm1[1],xmm5[1]
mulpd 0x50(%rsp), %xmm1
mulpd %xmm3, %xmm1
mulpd %xmm0, %xmm1
movapd 0x80(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x80(%rsp)
movapd 0x60(%rsp), %xmm0
addpd %xmm4, %xmm0
movapd %xmm0, 0x60(%rsp)
movq %r12, %rdi
callq 0x1d47a
movq 0x100(%rsp), %rcx
addq $0x8, %rcx
movq 0x100ac(%rip), %rax     # 0x2f548
jmp 0x1f06a
cvtss2sd 0x2c(%rsp), %xmm0
movapd 0x160(%rsp), %xmm2
mulsd %xmm0, %xmm2
mulsd 0xb0(%rsp), %xmm2
movapd 0x60(%rsp), %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm2, %xmm1
addsd %xmm3, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x40(%rsp), %xmm2
mulpd %xmm0, %xmm2
mulpd 0x170(%rsp), %xmm2
addpd 0x80(%rsp), %xmm2
addpd 0x70(%rsp), %xmm2
movq 0xb8(%rsp), %rax
movupd %xmm2, (%rax)
movsd %xmm1, 0x10(%rax)
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1f524
jmp 0x1f524
jmp 0x1f524
jmp 0x1f524
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x1d47a
movq %rbx, %rdi
callq 0x45c0