// File: srajangarg[P]raytracer/src/main.cpp
// Function: getRefractedRay(Primitive*, Vec3&, Ray&)
inline Ray
getRefractedRay(Primitive *z, Vec3 &pos, Ray &ray)
{
  Vec3 r, n, u;
  double cost1, cost2, to_mu, fr_mu;

  u = ray.direction();
  u.normalize();
  n = z->calculateNormal(pos);

  // is the ray entering the primitve or leaving it?
  if (ray.mu() < 1 + EPS) {
    to_mu = z->getMaterial().getMTN();
    fr_mu = 1;
  } else {
    to_mu = 1;
    fr_mu = z->getMaterial().getMTN();
    n = -n;
  }

  // refraction formulas
  cost1 = - (u * n);
  cost2 = sqrt(1 - ((fr_mu/to_mu) * (fr_mu/to_mu) * (1 - cost1*cost1)));
  r = (fr_mu/to_mu)*u + (((cost1 * fr_mu)/to_mu) - cost2)*n;

  return Ray::fromOriginAndDirection(pos + EPS*r, r, std::numeric_limits<double>::infinity(), to_mu);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x140(%rbp)
movq %rdi, %rax
movq %rax, -0x138(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6fc0
leaq -0x50(%rbp), %rdi
callq 0x6fc0
leaq -0x68(%rbp), %rdi
callq 0x6fc0
movq -0x20(%rbp), %rdi
callq 0x3e290
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x72d0
leaq -0x68(%rbp), %rdi
callq 0x14e00
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq (%rsi), %rax
leaq -0xa0(%rbp), %rdi
callq *0x18(%rax)
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x72d0
movq -0x20(%rbp), %rdi
callq 0x43b00
movaps %xmm0, %xmm1
movsd 0x5a92(%rip), %xmm0     # 0x48db0
ucomisd %xmm1, %xmm0
jbe 0x4334c
movq -0x10(%rbp), %rdi
callq 0x42cc0
movq %rax, %rdi
callq 0x43b20
movsd %xmm0, -0x80(%rbp)
movsd 0x3df6(%rip), %xmm0     # 0x47138
movsd %xmm0, -0x88(%rbp)
jmp 0x43392
movsd 0x3de4(%rip), %xmm0     # 0x47138
movsd %xmm0, -0x80(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42cc0
movq %rax, %rdi
callq 0x43b20
movsd %xmm0, -0x88(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3c030
leaq -0x50(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x72d0
leaq -0x68(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3e2b0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x88(%rbp), %xmm0
divsd -0x80(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
divsd -0x80(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm1
movsd 0x3d32(%rip), %xmm2     # 0x47138
addsd %xmm2, %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x3d0b(%rip), %xmm1     # 0x47138
addsd %xmm1, %xmm0
callq 0x5330
movsd %xmm0, -0x78(%rbp)
movsd -0x88(%rbp), %xmm0
divsd -0x80(%rbp), %xmm0
leaq -0xe8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3ea20
movsd -0x70(%rbp), %xmm0
mulsd -0x88(%rbp), %xmm0
divsd -0x80(%rbp), %xmm0
subsd -0x78(%rbp), %xmm0
leaq -0x100(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3ea20
leaq -0xd0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x3bfd0
leaq -0x38(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x72d0
movq -0x18(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x130(%rbp), %rdi
movsd 0x58e5(%rip), %xmm0     # 0x48da8
leaq -0x38(%rbp), %rsi
callq 0x3ea20
movq -0x148(%rbp), %rsi
leaq -0x118(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq 0x3bfd0
callq 0x3be50
movq -0x140(%rbp), %rdi
movsd -0x80(%rbp), %xmm1
leaq -0x118(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x3c0c0
movq -0x138(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)