// File: GUI-Graphics[P]cpuraytracing/sphere.cpp
// Function: gui::Sphere::intersect(gui::Ray const&, float, float, gui::Intersection&) const
bool Sphere::intersect(
		const Ray& ray,
		float tMin,
		float tMax,
		Intersection& intersection) const {
			
			++Renderer::nIntersects;

			Vector3 co = ray.origin - center;
			float a = ray.direction.lengthSquared();
			float b = ray.direction.dot(co);
			float c = co.lengthSquared() - radius * radius;
			float delta = b * b - a * c;

			if(delta < 0) return false;

			delta = sqrt(delta);
			float t = (-b - delta) / a;

			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			t = (-b + delta) / a;
			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			return false;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x150(%rbp)
leaq 0x8ced(%rip), %rax      # 0xe8d0
movq (%rax), %rcx
incq %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x10, %rsi
callq 0x4830
movss %xmm1, -0x40(%rbp)
movlpd %xmm0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x34(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
callq 0x4c20
movss %xmm0, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
leaq -0x34(%rbp), %rsi
callq 0x6000
movss %xmm0, -0x50(%rbp)
leaq -0x34(%rbp), %rdi
callq 0x4c20
movq -0x150(%rbp), %rax
movaps %xmm0, %xmm1
movss 0x1c(%rax), %xmm0
movss 0x1c(%rax), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x54(%rbp)
movss -0x50(%rbp), %xmm0
movss -0x50(%rbp), %xmm2
movss -0x4c(%rbp), %xmm1
mulss -0x54(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x58(%rbp), %xmm0
jbe 0x5cb0
movb $0x0, -0x1(%rbp)
jmp 0x5fed
movss -0x58(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2110
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x58(%rbp)
movss -0x50(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
subss -0x58(%rbp), %xmm0
divss -0x4c(%rbp), %xmm0
movss %xmm0, -0x5c(%rbp)
movss -0x5c(%rbp), %xmm0
ucomiss -0x1c(%rbp), %xmm0
jbe 0x5e4c
movss -0x5c(%rbp), %xmm1
movss -0x20(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x5e4c
movss -0x5c(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq %rdi, -0x158(%rbp)
addq $0xc, %rdi
movss -0x5c(%rbp), %xmm0
callq 0x47d0
movq -0x158(%rbp), %rdi
movss %xmm1, -0x80(%rbp)
movlpd %xmm0, -0x88(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x5970
movq -0x150(%rbp), %rsi
movss %xmm1, -0x90(%rbp)
movlpd %xmm0, -0x98(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movl -0x60(%rbp), %ecx
movl %ecx, 0xc(%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x4(%rax)
movq -0x28(%rbp), %rdi
addq $0x4, %rdi
addq $0x10, %rsi
callq 0x4830
movq -0x150(%rbp), %rax
movss %xmm1, -0xb8(%rbp)
movlpd %xmm0, -0xc0(%rbp)
movl -0xb8(%rbp), %ecx
movl %ecx, -0xa8(%rbp)
movq -0xc0(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movss 0x1c(%rax), %xmm0
leaq -0xb0(%rbp), %rdi
callq 0x59e0
movq -0x150(%rbp), %rax
movss %xmm1, -0xc8(%rbp)
movlpd %xmm0, -0xd0(%rbp)
movq -0xd0(%rbp), %rcx
movq %rcx, -0xa4(%rbp)
movl -0xc8(%rbp), %ecx
movl %ecx, -0x9c(%rbp)
movq -0x28(%rbp), %rcx
movq -0xa4(%rbp), %rdx
movq %rdx, 0x10(%rcx)
movl -0x9c(%rbp), %edx
movl %edx, 0x18(%rcx)
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5fed
movss -0x50(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
addss -0x58(%rbp), %xmm0
divss -0x4c(%rbp), %xmm0
movss %xmm0, -0x5c(%rbp)
movss -0x5c(%rbp), %xmm0
ucomiss -0x1c(%rbp), %xmm0
jbe 0x5fe9
movss -0x5c(%rbp), %xmm1
movss -0x20(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x5fe9
movss -0x5c(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq %rdi, -0x160(%rbp)
addq $0xc, %rdi
movss -0x5c(%rbp), %xmm0
callq 0x47d0
movq -0x160(%rbp), %rdi
movss %xmm1, -0xf8(%rbp)
movlpd %xmm0, -0x100(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xf0(%rbp), %rsi
callq 0x5970
movq -0x150(%rbp), %rsi
movss %xmm1, -0x108(%rbp)
movlpd %xmm0, -0x110(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0xd8(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x28(%rbp), %rax
movl -0xd8(%rbp), %ecx
movl %ecx, 0xc(%rax)
movq -0xe0(%rbp), %rcx
movq %rcx, 0x4(%rax)
movq -0x28(%rbp), %rdi
addq $0x4, %rdi
addq $0x10, %rsi
callq 0x4830
movq -0x150(%rbp), %rax
movss %xmm1, -0x130(%rbp)
movlpd %xmm0, -0x138(%rbp)
movl -0x130(%rbp), %ecx
movl %ecx, -0x120(%rbp)
movq -0x138(%rbp), %rcx
movq %rcx, -0x128(%rbp)
movss 0x1c(%rax), %xmm0
leaq -0x128(%rbp), %rdi
callq 0x59e0
movq -0x150(%rbp), %rax
movss %xmm1, -0x140(%rbp)
movlpd %xmm0, -0x148(%rbp)
movq -0x148(%rbp), %rcx
movq %rcx, -0x11c(%rbp)
movl -0x140(%rbp), %ecx
movl %ecx, -0x114(%rbp)
movq -0x28(%rbp), %rcx
movq -0x11c(%rbp), %rdx
movq %rdx, 0x10(%rcx)
movl -0x114(%rbp), %edx
movl %edx, 0x18(%rcx)
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5fed
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopl (%rax,%rax)