// File: /workspace/github_cpp_makefile_star2_O2/BartMassey[P]ancient-raytracer/sphere.cc
// Function: sphere::intersect(ray, intersection&)
int sphere::intersect( ray r, intersection &s ) {
  r.transform( toi );  // now we have the ray in object coords...
  
  float a = r.d().mag2();
  float b = r.o() * r.d();
  float c = r.o().mag2() - 1.0;
  float d = b*b - a*c;
  float t;

  if( d < TINY ) {
    // ray misses sphere
    return 0;
  }
  if( a < 0 )
    t = (-b + sqrt( d )) / a;
  else
    t = (-b - sqrt( d )) / a;
  if( t < TINY ) {
    // ray is travelling away from sphere, so no hit
    return 0;
  }
  point i( r.o() + r.d() * t );  // the intersection point in object coords
  i.transform(toi);
	
  // there are many possible mappings -- here's a lame one
  s.t = t;
  s.at = new point( X(i), Y(i) );
  s.normal = new point( (i - tr).unit() );
  
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %r14
leaq 0xb0(%rdi), %r15
movq %rsi, %rdi
movq %r15, %rsi
callq 0x4384
leaq 0x28(%r12), %r13
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2808
movq %rbp, %rdi
callq 0x2d64
movss %xmm0, 0xc(%rsp)
addq $0x8, %r12
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2808
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2808
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2998
movaps %xmm0, 0x10(%rsp)
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2808
movq %rbx, %rdi
callq 0x2d64
movaps 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
movaps %xmm0, %xmm1
addss 0x155e(%rip), %xmm1     # 0x6780
mulss %xmm2, %xmm1
movaps %xmm3, %xmm0
mulss %xmm3, %xmm0
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
xorl %ebp, %ebp
movsd 0xe21(%rip), %xmm4      # 0x6060
ucomisd %xmm1, %xmm4
ja 0x53c8
xorps %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x525c
ucomiss %xmm1, %xmm0
jb 0x526e
sqrtss %xmm0, %xmm0
jmp 0x5286
xorps 0xddd(%rip), %xmm3      # 0x6040
ucomiss %xmm1, %xmm0
jb 0x528c
sqrtss %xmm0, %xmm0
jmp 0x52a9
callq 0x21a0
movsd 0xde5(%rip), %xmm4      # 0x6060
movaps 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
subss %xmm3, %xmm0
jmp 0x52b0
movaps %xmm3, 0x10(%rsp)
callq 0x21a0
movsd 0xdc2(%rip), %xmm4      # 0x6060
movaps 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
subss %xmm0, %xmm3
movaps %xmm3, %xmm0
divss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm1
ucomisd %xmm1, %xmm4
ja 0x53c8
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
movss %xmm0, 0x10(%rsp)
callq 0x2808
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2808
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movss 0x10(%rsp), %xmm0
callq 0x2b4c
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x2a5c
movq 0x20(%rsp), %r12
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2b76
movss 0x10(%rsp), %xmm0
movss %xmm0, (%r12)
pushq $0x20
popq %rdi
callq 0x20d0
movq %rax, %r15
movq %rbx, %rdi
callq 0x3713
movss %xmm0, 0x10(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x3754
leaq 0x44ee(%rip), %rax      # 0x9848
movq %rax, (%r15)
movl $0x2, 0x8(%r15)
movss 0x10(%rsp), %xmm1
movss %xmm1, 0xc(%r15)
movss %xmm0, 0x10(%r15)
movq %r15, 0x10(%r12)
pushq $0x20
popq %rdi
callq 0x20d0
movq %rax, %r15
addq $0x148, %r14            # imm = 0x148
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2808
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x2a22
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0x2de0
movq %r15, 0x8(%r12)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53e7
jmp 0x53e7
jmp 0x53e2
movq %rax, %rbx
jmp 0x53f5
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2180
nop