// File: /workspace/github_cpp_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
leaq 0xb0(%rdi), %r15
movq %rsi, %rdi
movq %r15, %rsi
callq 0x518c
movslq 0x30(%r12), %r13
testq %r13, %r13
jle 0x6d20
leaq 0x34(%r12), %rsi
leaq 0x1c(%rsp), %rdi
leaq (,%r13,4), %rdx
callq 0x20a0
xorps %xmm2, %xmm2
xorl %eax, %eax
movss 0x1c(%rsp,%rax,4), %xmm0
mulss %xmm0, %xmm0
addss %xmm0, %xmm2
incq %rax
cmpq %rax, %r13
jne 0x6d08
jmp 0x6d23
xorps %xmm2, %xmm2
movslq 0x10(%r12), %rbx
testq %rbx, %rbx
movss %xmm2, 0xc(%rsp)
jle 0x6d50
leaq 0x14(%r12), %rsi
leaq 0x1c(%rsp), %rdi
leaq (,%rbx,4), %rdx
callq 0x20a0
movss 0xc(%rsp), %xmm2
testl %r13d, %r13d
jle 0x6d75
leaq 0x34(%r12), %rsi
leaq 0xf4(%rsp), %rdi
leaq (,%r13,4), %rdx
callq 0x20a0
movss 0xc(%rsp), %xmm2
cmpl %r13d, %ebx
jne 0x7263
testl %ebx, %ebx
movq %rbp, 0x68(%rsp)
jle 0x6df3
xorps %xmm1, %xmm1
xorl %eax, %eax
movss 0x1c(%rsp,%rax,4), %xmm0
mulss 0xf4(%rsp,%rax,4), %xmm0
addss %xmm0, %xmm1
incq %rax
cmpq %rax, %r13
jne 0x6d8c
movaps %xmm1, 0x50(%rsp)
leaq 0x14(%r12), %rsi
leaq 0x1c(%rsp), %rdi
leaq (,%r13,4), %rdx
callq 0x20a0
xorps %xmm1, %xmm1
xorl %eax, %eax
movss 0x1c(%rsp,%rax,4), %xmm0
mulss %xmm0, %xmm0
addss %xmm0, %xmm1
incq %rax
cmpq %rax, %r13
jne 0x6dc8
addss 0x19da(%rip), %xmm1     # 0x87c0
movss 0xc(%rsp), %xmm2
movaps 0x50(%rsp), %xmm3
jmp 0x6dfe
movss 0x19c5(%rip), %xmm1     # 0x87c0
xorps %xmm3, %xmm3
mulss %xmm2, %xmm1
movaps %xmm3, %xmm0
mulss %xmm3, %xmm0
subss %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorl %ebp, %ebp
movsd 0x1252(%rip), %xmm4     # 0x8070
ucomisd %xmm1, %xmm4
ja 0x724f
xorps %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x6e3b
ucomiss %xmm1, %xmm0
jb 0x6e4d
sqrtss %xmm0, %xmm0
jmp 0x6e6a
xorps 0x11fe(%rip), %xmm3     # 0x8040
ucomiss %xmm1, %xmm0
jb 0x6e70
sqrtss %xmm0, %xmm0
jmp 0x6e8d
movaps %xmm3, 0x50(%rsp)
callq 0x21c0
movsd 0x1211(%rip), %xmm4     # 0x8070
movaps 0x50(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
subss %xmm3, %xmm0
jmp 0x6e94
movaps %xmm3, 0x50(%rsp)
callq 0x21c0
movsd 0x11ee(%rip), %xmm4     # 0x8070
movaps 0x50(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
subss %xmm0, %xmm3
movaps %xmm3, %xmm0
divss %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
ucomisd %xmm1, %xmm4
ja 0x724f
movl 0x10(%r12), %r13d
testl %r13d, %r13d
movss %xmm0, 0xc(%rsp)
jle 0x6ed9
leaq 0x14(%r12), %rsi
leaq (,%r13,4), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x20a0
movss 0xc(%rsp), %xmm0
movslq 0x30(%r12), %rbx
testq %rbx, %rbx
movq 0x68(%rsp), %rbp
jle 0x6f2a
addq $0x34, %r12
leaq (,%rbx,4), %rdx
leaq 0x9c(%rsp), %rdi
movq %r12, %rsi
callq 0x20a0
movss 0xc(%rsp), %xmm0
xorl %eax, %eax
movss 0x9c(%rsp,%rax,4), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x9c(%rsp,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x6f0c
leaq 0x4917(%rip), %r12      # 0xb848
movq %r12, 0x30(%rsp)
movl %r13d, 0x38(%rsp)
testl %r13d, %r13d
jle 0x6f60
leaq 0x3c(%rsp), %rdi
leaq (,%r13,4), %rdx
leaq 0xb0(%rsp), %rsi
callq 0x20a0
movss 0xc(%rsp), %xmm0
testl %ebx, %ebx
jle 0x6f84
leaq 0x9c(%rsp), %rsi
leaq 0x1c(%rsp), %rdi
leaq (,%rbx,4), %rdx
callq 0x20a0
movss 0xc(%rsp), %xmm0
cmpl %ebx, %r13d
jne 0x7282
testl %r13d, %r13d
jle 0x7035
leaq 0x3c(%rsp), %rsi
xorl %eax, %eax
movss 0x1c(%rsp,%rax,4), %xmm0
addss 0x3c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x3c(%rsp,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x6f9d
cmpl $0x5, %r13d
jge 0x72fe
leaq (,%r13,4), %r12
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x20a0
cmpl $0x3, %r13d
jae 0x6ff5
leaq (%rsp,%r13,4), %rdi
addq $0x10, %rdi
movl $0xc, %edx
subl %r12d, %edx
xorl %esi, %esi
callq 0x2070
jmp 0x6ff7
jne 0x6fff
movl $0x3f800000, 0x1c(%rsp) # imm = 0x3F800000
xorl %r12d, %r12d
leaq 0x10(%rsp), %r13
movq (%r15), %rax
movq %r15, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq *0x20(%rax)
movss %xmm0, 0x3c(%rsp,%r12,4)
incq %r12
movslq 0x38(%rsp), %rax
cmpq %rax, %r12
jl 0x7007
movq 0x30(%rsp), %r12
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rbp)
movl $0x20, %edi
callq 0x20f0
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq *0xe8(%r12)
testl %eax, %eax
jle 0x72a1
leaq 0x30(%rsp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x30(%rax)
movss (%rax), %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0x30(%rsp), %rdi
movq (%rdi), %rax
callq *0xe8(%rax)
cmpl $0x1, %eax
jle 0x72c0
leaq 0x30(%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x30(%rax)
movss (%rax), %xmm0
leaq 0x47a3(%rip), %rbx      # 0xb848
movq %rbx, (%r15)
movl $0x2, 0x8(%r15)
movss 0xc(%rsp), %xmm1
movss %xmm1, 0xc(%r15)
movss %xmm0, 0x10(%r15)
movq %r15, 0x10(%rbp)
movl $0x20, %edi
callq 0x20f0
movq %rax, %r15
movslq 0x150(%r14), %r13
testq %r13, %r13
jle 0x70fe
addq $0x154, %r14            # imm = 0x154
leaq (,%r13,4), %rdx
leaq 0xd4(%rsp), %rdi
movq %r14, %rsi
callq 0x20a0
movslq 0x38(%rsp), %rbp
testq %rbp, %rbp
jle 0x711f
leaq 0x3c(%rsp), %rsi
leaq 0x7c(%rsp), %rdi
leaq (,%rbp,4), %rdx
callq 0x20a0
testl %r13d, %r13d
jle 0x7177
leaq (,%r13,4), %r14
leaq 0x1c(%rsp), %r12
leaq 0xd4(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x20a0
xorl %eax, %eax
movaps 0xef3(%rip), %xmm0      # 0x8040
movss 0x1c(%rsp,%rax,4), %xmm1
xorps %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x714d
leaq 0xbc(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x20a0
cmpl %r13d, %ebp
jne 0x7282
testl %ebp, %ebp
jle 0x71e3
leaq 0x7c(%rsp), %rsi
xorl %eax, %eax
movss 0xbc(%rsp,%rax,4), %xmm0
addss 0x7c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x7c(%rsp,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x718b
movq %rbx, (%r15)
movl %r13d, 0x8(%r15)
movq %r15, %r14
addq $0xc, %r14
leaq (,%r13,4), %rdx
movq %r14, %rdi
callq 0x20a0
xorps %xmm0, %xmm0
xorl %eax, %eax
movss (%r14,%rax,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x71cb
jmp 0x71ed
movq %rbx, (%r15)
movl %r13d, 0x8(%r15)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x71fb
sqrtss %xmm0, %xmm0
jmp 0x7200
callq 0x21c0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
ucomisd 0xe61(%rip), %xmm1      # 0x8070
jbe 0x72df
testl %ebp, %ebp
jle 0x7241
movss 0xe37(%rip), %xmm1      # 0x8058
divss %xmm0, %xmm1
xorl %eax, %eax
movss 0xc(%r15,%rax,4), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, 0xc(%r15,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x7227
movq 0x68(%rsp), %rax
movq %r15, 0x8(%rax)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe66(%rip), %rdi       # 0x80d0
leaq 0xe35(%rip), %rsi       # 0x80a6
leaq 0xe63(%rip), %rcx       # 0x80db
movl $0x4f, %edx
callq 0x2080
leaq 0xe47(%rip), %rdi       # 0x80d0
leaq 0xe16(%rip), %rsi       # 0x80a6
leaq 0xe6a(%rip), %rcx       # 0x8101
movl $0x66, %edx
callq 0x2080
leaq 0x1043(%rip), %rdi      # 0x82eb
leaq 0xdf7(%rip), %rsi       # 0x80a6
leaq 0x103f(%rip), %rcx      # 0x82f5
movl $0x104, %edx            # imm = 0x104
callq 0x2080
leaq 0x103f(%rip), %rdi      # 0x8306
leaq 0xdd8(%rip), %rsi       # 0x80a6
leaq 0x103b(%rip), %rcx      # 0x8310
movl $0x109, %edx            # imm = 0x109
callq 0x2080
leaq 0xef5(%rip), %rdi       # 0x81db
leaq 0xdb9(%rip), %rsi       # 0x80a6
leaq 0xef0(%rip), %rcx       # 0x81e4
movl $0xc9, %edx
callq 0x2080
leaq 0xe22(%rip), %rdi       # 0x8127
leaq 0xd9a(%rip), %rsi       # 0x80a6
leaq 0xe1b(%rip), %rcx       # 0x812e
movl $0x92, %edx
callq 0x2080
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x21a0
nop