// File: /workspace/github_cpp_makefile_star2_O3/srajangarg[P]raytracer/src/Primitives.cpp
// Function: Triangle::calculateNormal(Vec3 const&) const
Vec3
Triangle::calculateNormal(Vec3 const & position) const
{ 
  Vec3 pos = worldToModel_ * position;
  Vec3 norm(0, 0, 0);

  if (smooth_normals) {

    // get the areas of each of the subtriangles, as divided by pos
    double a[3];
    for (int i = 0; i < 3; ++i)
      a[i] = ((verts[(i+1)%3] - pos) ^ (verts[(i+2)%3] - pos)).length();

    // weigh the normals according to these areas
    for (int i = 0; i < 3; ++i)
      norm += a[i] * norms[i];

  } else {

    // simple normal to the triangle
    norm = (verts[1] - verts[0]) ^ (verts[2] - verts[0]);
  }

  return Vec3(worldToModel_.transpose() * Vec4(norm, 0), 3).normalize();
}

// Assembly:
movq %rdi, %rax
movsd (%rdx), %xmm8
movsd 0x8(%rdx), %xmm6
movsd 0x10(%rdx), %xmm11
movsd 0x98(%rsi), %xmm2
movsd 0xb8(%rsi), %xmm14
movsd 0xd8(%rsi), %xmm1
movupd 0x88(%rsi), %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
mulsd %xmm6, %xmm3
movapd %xmm0, %xmm5
mulsd %xmm8, %xmm5
addsd %xmm3, %xmm5
movapd %xmm2, %xmm9
mulsd %xmm11, %xmm9
addsd %xmm5, %xmm9
addsd 0xa0(%rsi), %xmm9
movupd 0xa8(%rsi), %xmm15
movapd %xmm15, %xmm3
unpckhpd %xmm15, %xmm3           # xmm3 = xmm3[1],xmm15[1]
mulsd %xmm6, %xmm3
movapd %xmm15, %xmm7
mulsd %xmm8, %xmm7
addsd %xmm3, %xmm7
movapd %xmm14, %xmm10
mulsd %xmm11, %xmm10
addsd %xmm7, %xmm10
addsd 0xc0(%rsi), %xmm10
movupd 0xc8(%rsi), %xmm7
movapd %xmm7, %xmm3
unpckhpd %xmm7, %xmm3            # xmm3 = xmm3[1],xmm7[1]
mulsd %xmm6, %xmm3
movapd %xmm7, %xmm13
mulsd %xmm8, %xmm13
addsd %xmm3, %xmm13
movapd %xmm1, %xmm12
mulsd %xmm11, %xmm12
addsd %xmm13, %xmm12
movupd 0xe8(%rsi), %xmm3
movapd %xmm3, %xmm13
unpckhpd %xmm3, %xmm13           # xmm13 = xmm13[1],xmm3[1]
mulsd %xmm6, %xmm13
mulsd %xmm3, %xmm8
addsd %xmm13, %xmm8
movsd 0xf8(%rsi), %xmm6
mulsd %xmm6, %xmm11
addsd %xmm8, %xmm11
addsd 0x100(%rsi), %xmm11
xorpd %xmm8, %xmm8
ucomisd %xmm8, %xmm11
movapd %xmm9, %xmm8
divsd %xmm11, %xmm8
jne 0x277c3
jp 0x277c3
movapd %xmm9, %xmm8
addsd 0xe0(%rsi), %xmm12
movapd %xmm10, %xmm9
divsd %xmm11, %xmm9
jne 0x277df
jp 0x277df
movapd %xmm10, %xmm9
movapd %xmm12, %xmm10
divsd %xmm11, %xmm10
jne 0x277f2
jp 0x277f2
movapd %xmm12, %xmm10
cmpb $0x1, 0x188(%rsi)
jne 0x27947
movapd %xmm15, -0x28(%rsp)
movsd %xmm14, -0x30(%rsp)
leaq 0x190(%rsi), %rcx
xorl %edi, %edi
movl $0x2, %edx
leaq 0x1(%rdi), %r8
cmpq $0x2, %rdi
movl $0x0, %r9d
cmovneq %r8, %r9
leaq (%r9,%r9,2), %r9
movsd (%rcx,%r9,8), %xmm12
movsd 0x8(%rcx,%r9,8), %xmm14
subsd %xmm8, %xmm12
subsd %xmm9, %xmm14
movsd 0x10(%rcx,%r9,8), %xmm11
leal -0x1(%rdi), %r9d
testq %rdi, %rdi
cmoveq %rdx, %r9
subsd %xmm10, %xmm11
leaq (%r9,%r9,2), %r9
movsd (%rcx,%r9,8), %xmm15
movsd 0x8(%rcx,%r9,8), %xmm13
subsd %xmm8, %xmm15
subsd %xmm9, %xmm13
movsd 0x10(%rcx,%r9,8), %xmm4
subsd %xmm10, %xmm4
movapd %xmm13, %xmm5
mulsd %xmm11, %xmm5
mulsd %xmm15, %xmm11
mulsd %xmm14, %xmm15
mulsd %xmm4, %xmm14
subsd %xmm5, %xmm14
mulsd %xmm12, %xmm4
subsd %xmm4, %xmm11
mulsd %xmm12, %xmm13
subsd %xmm15, %xmm13
mulsd %xmm11, %xmm11
mulsd %xmm14, %xmm14
addsd %xmm11, %xmm14
mulsd %xmm13, %xmm13
addsd %xmm14, %xmm13
xorps %xmm4, %xmm4
sqrtsd %xmm13, %xmm4
movsd %xmm4, -0x18(%rsp,%rdi,8)
movq %r8, %rdi
cmpq $0x3, %r8
jne 0x2781b
addq $0x150, %rsi            # imm = 0x150
xorpd %xmm9, %xmm9
xorpd %xmm8, %xmm8
xorl %ecx, %ecx
movsd -0x18(%rsp,%rcx,8), %xmm4
movupd -0x10(%rsi), %xmm5
movapd %xmm4, %xmm10
unpcklpd %xmm4, %xmm10           # xmm10 = xmm10[0],xmm4[0]
mulpd %xmm5, %xmm10
addpd %xmm10, %xmm9
mulsd (%rsi), %xmm4
addsd %xmm4, %xmm8
incq %rcx
addq $0x18, %rsi
cmpq $0x3, %rcx
jne 0x278ff
movsd -0x30(%rsp), %xmm14
movapd -0x28(%rsp), %xmm15
jmp 0x279d5
movsd 0x1a8(%rsi), %xmm8
movsd 0x190(%rsi), %xmm4
subsd %xmm4, %xmm8
movsd 0x1c8(%rsi), %xmm5
movupd 0x1b0(%rsi), %xmm10
movupd 0x198(%rsi), %xmm9
subpd %xmm9, %xmm10
subsd %xmm9, %xmm5
movsd 0x1d0(%rsi), %xmm11
movhpd 0x1c0(%rsi), %xmm11     # xmm11 = xmm11[0],mem[0]
shufpd $0x1, %xmm4, %xmm9      # xmm9 = xmm9[1],xmm4[0]
subpd %xmm9, %xmm11
movapd %xmm10, %xmm4
shufpd $0x1, %xmm8, %xmm4      # xmm4 = xmm4[1],xmm8[0]
mulsd %xmm5, %xmm8
unpcklpd %xmm11, %xmm5           # xmm5 = xmm5[0],xmm11[0]
mulpd %xmm4, %xmm5
movapd %xmm10, %xmm9
mulpd %xmm11, %xmm9
subpd %xmm5, %xmm9
unpckhpd %xmm11, %xmm11          # xmm11 = xmm11[1,1]
mulsd %xmm10, %xmm11
subsd %xmm11, %xmm8
movapd %xmm9, %xmm4
unpckhpd %xmm9, %xmm4            # xmm4 = xmm4[1],xmm9[1]
mulpd %xmm4, %xmm15
mulsd %xmm4, %xmm14
mulsd %xmm9, %xmm2
addsd %xmm14, %xmm2
mulsd %xmm8, %xmm1
addsd %xmm2, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm2, %xmm6
addsd %xmm1, %xmm6
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
mulpd %xmm9, %xmm0
addpd %xmm15, %xmm0
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
mulpd %xmm8, %xmm7
addpd %xmm0, %xmm7
xorpd %xmm0, %xmm0
mulpd %xmm0, %xmm3
addpd %xmm7, %xmm3
movapd %xmm3, %xmm0
mulpd %xmm3, %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movapd %xmm6, %xmm0
mulsd %xmm6, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0x36e7(%rip), %xmm1     # 0x2b148
divsd %xmm0, %xmm1
mulsd %xmm1, %xmm6
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd %xmm3, %xmm1
movupd %xmm1, (%rax)
movsd %xmm6, 0x10(%rax)
retq
nop