// File: /workspace/github_cpp_makefile_star2_O3/Crawler995[P]Cpp-Ray-Tracing/libs/sphere.cpp
// Function: Sphere::intersect(Ray3)
IntersectResult Sphere::intersect(Ray3 ray) {
    Vector3 v = ray.get_origin().subtract(center);
    double a = ray.get_dir().square_length();
    double b = 2 * v.dot(ray.get_dir());
    double c = v.square_length() - radius * radius;

    double delta = b * b - 4 * a * c;

    IntersectResult res(0, 0, Vector3(0, 0, 0), Vector3(0, 0, 0));
    if(delta >= 0 && b <= 0) {
        res.geometry = this;
        res.distance = (-b - sqrt(delta)) / (2 * a);
        res.position = ray.get_point(res.distance);
        res.normal = res.position.subtract(center).normalize();
    }

    return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x10(%rbp), %r14
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b74
movq 0x20(%r12), %rax
movq %rax, 0x10(%rsp)
movups 0x10(%r12), %xmm0
movups %xmm0, (%rsp)
leaq -0xa8(%rbp), %rdi
movq %r15, %rsi
callq 0x29a6
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b8c
movq %r15, %rdi
callq 0x28aa
movsd %xmm0, -0x30(%rbp)
leaq -0xf0(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %r14
callq 0x2b8c
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movupd (%r13), %xmm0
movupd %xmm0, (%rsp)
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
callq 0x2a3a
addsd %xmm0, %xmm0
movapd %xmm0, -0x40(%rbp)
movq %r12, %rdi
callq 0x28aa
movsd %xmm0, -0x50(%rbp)
movsd 0x28(%r14), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq -0xd8(%rbp), %r12
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r12, %rdi
callq 0x2838
leaq -0xc0(%rbp), %r13
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r13, %rdi
callq 0x2838
movq 0x10(%r13), %rax
movq %rax, 0x10(%r15)
movups (%r13), %xmm0
movaps %xmm0, (%r15)
movups (%r12), %xmm0
movaps %xmm0, -0x70(%rbp)
movq 0x10(%r12), %rax
movq %rax, -0x60(%rbp)
leaq 0x10(%rbx), %r13
movq %r13, %rdi
callq 0x284c
leaq 0x28(%rbx), %r12
movq %r12, %rdi
callq 0x284c
xorpd %xmm4, %xmm4
movapd -0x40(%rbp), %xmm3
movsd -0x30(%rbp), %xmm2
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movaps -0x70(%rbp), %xmm0
movups %xmm0, 0x10(%rbx)
movq -0x60(%rbp), %rax
movq %rax, 0x20(%rbx)
movapd (%r15), %xmm0
movupd %xmm0, 0x28(%rbx)
movq 0x10(%r15), %rax
movq %rax, 0x38(%rbx)
ucomisd %xmm3, %xmm4
jb 0x2fb3
movsd -0x48(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd -0x50(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd 0x31ff(%rip), %xmm1     # 0x60f0
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm4, %xmm0
jb 0x2fb3
movq %r14, (%rbx)
xorpd 0x31c6(%rip), %xmm3     # 0x60e0
addq $0x10, %r14
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2f2e
sqrtsd %xmm0, %xmm0
jmp 0x2f42
movapd %xmm3, -0x40(%rbp)
callq 0x20b0
movapd -0x40(%rbp), %xmm3
movsd -0x30(%rbp), %xmm2
subsd %xmm0, %xmm3
addsd %xmm2, %xmm2
divsd %xmm2, %xmm3
movsd %xmm3, 0x8(%rbx)
movq %r15, %rdi
leaq 0x10(%rbp), %rsi
movapd %xmm3, %xmm0
callq 0x2b26
movq 0x10(%r15), %rax
movq %rax, 0x10(%r13)
movups (%r15), %xmm0
movups %xmm0, (%r13)
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
leaq -0x70(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x29a6
movq %r15, %rdi
movq %r14, %rsi
callq 0x28d2
movq 0x10(%r15), %rax
movq %rax, 0x10(%r12)
movupd (%r15), %xmm0
movupd %xmm0, (%r12)
movq %rbx, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq