// File: 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
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rsi, -0x1b0(%rbp)
movq %rdi, %rax
movq -0x1b0(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x1a8(%rbp)
addq $0xb0, %rsi
callq 0x6c40
movq -0x1a0(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x5140
movq -0x198(%rbp), %rdi
callq 0x3b50
movss %xmm0, -0x190(%rbp)
jmp 0x8b53
leaq -0x48(%rbp), %rdi
callq 0x2fd0
movss -0x190(%rbp), %xmm0
movq -0x1a0(%rbp), %rsi
movss %xmm0, -0x24(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5170
movq -0x1a0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x5140
jmp 0x8b8e
leaq -0x78(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x32f0
movss %xmm0, -0x1b4(%rbp)
jmp 0x8ba8
leaq -0x98(%rbp), %rdi
callq 0x2fd0
leaq -0x78(%rbp), %rdi
callq 0x2fd0
movss -0x1b4(%rbp), %xmm0
movq -0x1a0(%rbp), %rsi
movss %xmm0, -0x58(%rbp)
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x5170
movq -0x1c0(%rbp), %rdi
callq 0x3b50
movss %xmm0, -0x1b8(%rbp)
jmp 0x8bfa
movss -0x1b8(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2432(%rip), %xmm1     # 0xb040
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x1c4(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x2fd0
movss -0x1c4(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
movss -0x58(%rbp), %xmm0
movss -0x58(%rbp), %xmm2
movss -0x24(%rbp), %xmm1
mulss -0x9c(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xc4(%rbp)
movss -0xc4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x23b6(%rip), %xmm0     # 0xb038
ucomisd %xmm1, %xmm0
jbe 0x8cff
movl $0x0, -0x4(%rbp)
jmp 0x906a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2fd0
jmp 0x9076
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8cd4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2fd0
leaq -0x78(%rbp), %rdi
callq 0x2fd0
jmp 0x9076
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x2fd0
jmp 0x9076
xorps %xmm0, %xmm0
ucomiss -0x24(%rbp), %xmm0
jbe 0x8d4d
movss -0x58(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x1c8(%rbp)
movss -0xc4(%rbp), %xmm0
callq 0x3fc0
movaps %xmm0, %xmm1
movss -0x1c8(%rbp), %xmm0
addss %xmm1, %xmm0
divss -0x24(%rbp), %xmm0
movss %xmm0, -0xc8(%rbp)
jmp 0x8d90
movss -0x58(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x1cc(%rbp)
movss -0xc4(%rbp), %xmm0
callq 0x3fc0
movaps %xmm0, %xmm1
movss -0x1cc(%rbp), %xmm0
subss %xmm1, %xmm0
divss -0x24(%rbp), %xmm0
movss %xmm0, -0xc8(%rbp)
movss -0xc8(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x2294(%rip), %xmm0     # 0xb038
ucomisd %xmm1, %xmm0
jbe 0x8db6
movl $0x0, -0x4(%rbp)
jmp 0x906a
movq -0x1a0(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x5170
movq -0x1a0(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x5140
jmp 0x8dde
movss -0xc8(%rbp), %xmm0
leaq -0x128(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x3760
jmp 0x8dfb
leaq -0xe8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0x3530
jmp 0x8e17
leaq -0x128(%rbp), %rdi
callq 0x2fd0
leaq -0x148(%rbp), %rdi
callq 0x2fd0
leaq -0x108(%rbp), %rdi
callq 0x2fd0
movq -0x1a8(%rbp), %rsi
addq $0xb0, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x37c0
jmp 0x8e57
movss -0xc8(%rbp), %xmm0
movq -0x20(%rbp), %rax
movss %xmm0, (%rax)
movl $0x20, %edi
callq 0x2110
movq %rax, -0x1d8(%rbp)
jmp 0x8e7a
leaq -0xe8(%rbp), %rdi
callq 0x5080
movss %xmm0, -0x1dc(%rbp)
jmp 0x8e90
leaq -0xe8(%rbp), %rdi
callq 0x50e0
movss %xmm0, -0x1e0(%rbp)
jmp 0x8ea6
movss -0x1e0(%rbp), %xmm1
movss -0x1dc(%rbp), %xmm0
movq -0x1d8(%rbp), %rdi
callq 0x5220
jmp 0x8ec4
movq -0x1d8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x20, %edi
callq 0x2110
movq %rax, -0x1e8(%rbp)
jmp 0x8ee6
movq -0x1a8(%rbp), %rsi
movb $0x1, -0x189(%rbp)
addq $0x148, %rsi            # imm = 0x148
leaq -0x188(%rbp), %rdi
callq 0x2ef0
jmp 0x8f09
leaq -0x168(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x188(%rbp), %rdx
callq 0x3480
jmp 0x8f25
movq -0x1e8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x3c50
jmp 0x8f3a
movq -0x1e8(%rbp), %rcx
movb $0x0, -0x189(%rbp)
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq -0x168(%rbp), %rdi
callq 0x2fd0
leaq -0x188(%rbp), %rdi
callq 0x2fd0
movl $0x1, -0x4(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x2fd0
jmp 0x906a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8fc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8fb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x2fd0
leaq -0x148(%rbp), %rdi
callq 0x2fd0
leaq -0x108(%rbp), %rdi
callq 0x2fd0
jmp 0x9076
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x905c
movq -0x1d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x20, %esi
callq 0x2120
jmp 0x905c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x903e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x9032
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x2fd0
leaq -0x188(%rbp), %rdi
callq 0x2fd0
testb $0x1, -0x189(%rbp)
jne 0x9049
jmp 0x905a
movq -0x1e8(%rbp), %rdi
movl $0x20, %esi
callq 0x2120
jmp 0x905c
leaq -0xe8(%rbp), %rdi
callq 0x2fd0
jmp 0x9076
movl -0x4(%rbp), %eax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x21c0
nop