// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/src/sphere.cpp
// Function: sphere::hit(ray const&, float, float, hit_record&) const
bool sphere::hit(const ray& _ray, float t_min, float t_max, hit_record& rec) const
{
    vec3 oc = _ray.origin() - center;
    float a = dot(_ray.direction(), _ray.direction());
    float b = dot(oc, _ray.direction());
    float c = dot(oc, oc) - radius * radius;
    float discriminant = b*b - a*c;

    if(discriminant > 0.0f)
    {
        float temp = (-b - sqrt(discriminant)) / a;

        if(temp < t_max && temp > t_min)
        {
            rec.t = temp;
            rec.hit_point = _ray.point_at_parameter(rec.t);
            // obtaining texture coordinate
            get_sphere_uv((rec.hit_point - center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - center) / radius;
            rec.material_ref = sphere_mat;
            return true;
        }

        temp = (-b + sqrt(discriminant)) / a;

        if(temp < t_max && temp > t_min)
        {
            rec.t = temp;
            rec.hit_point = _ray.point_at_parameter(rec.t);
            // obtaining texture coordinate
            get_sphere_uv((rec.hit_point - center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - center) / radius;
            rec.material_ref = sphere_mat;
            return true;
        }
    }
    
    return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x8(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movsd (%rsi), %xmm0
movss 0x8(%rsi), %xmm1
leaq 0x10(%rsp), %rdi
movsd %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x8(%r14), %r15
movq %r15, %rsi
callq 0x9449
movsd 0xc(%r12), %xmm2
movss 0x14(%r12), %xmm5
movaps %xmm2, %xmm6
shufps $0x55, %xmm2, %xmm6     # xmm6 = xmm6[1,1],xmm2[1,1]
movaps %xmm6, %xmm3
mulss %xmm6, %xmm3
movaps %xmm0, %xmm4
mulss %xmm2, %xmm4
mulss %xmm2, %xmm2
addss %xmm3, %xmm2
movaps %xmm1, %xmm3
mulss %xmm5, %xmm3
mulss %xmm5, %xmm5
addss %xmm2, %xmm5
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
mulss %xmm2, %xmm6
addss %xmm4, %xmm6
addss %xmm3, %xmm6
mulss %xmm2, %xmm2
mulss %xmm0, %xmm0
addss %xmm2, %xmm0
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
movss 0x14(%r14), %xmm0
mulss %xmm0, %xmm0
subss %xmm0, %xmm1
mulss %xmm5, %xmm1
movaps %xmm6, %xmm2
mulss %xmm6, %xmm2
subss %xmm1, %xmm2
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x8f13
movaps 0x27420(%rip), %xmm1    # 0x30320
xorps %xmm6, %xmm1
ucomiss %xmm0, %xmm2
movaps %xmm6, 0x20(%rsp)
jb 0x8f1a
sqrtss %xmm2, %xmm0
jmp 0x8f42
xorl %eax, %eax
jmp 0x903a
movaps %xmm2, %xmm0
movss %xmm5, 0x4(%rsp)
movaps %xmm2, 0x40(%rsp)
movaps %xmm1, 0x30(%rsp)
callq 0x6750
movaps 0x30(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movss 0x4(%rsp), %xmm5
subss %xmm0, %xmm1
divss %xmm5, %xmm1
movss 0xc(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x8f68
ucomiss 0x8(%rsp), %xmm1
jbe 0x8f68
movss %xmm1, (%rbx)
movq %r12, %rdi
movaps %xmm1, %xmm0
jmp 0x8fba
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
jb 0x8f76
sqrtss %xmm2, %xmm0
jmp 0x8f8a
movaps %xmm2, %xmm0
movss %xmm5, 0x4(%rsp)
callq 0x6750
movss 0x4(%rsp), %xmm5
movaps 0x20(%rsp), %xmm1
subss %xmm1, %xmm0
divss %xmm5, %xmm0
xorl %eax, %eax
movss 0xc(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x903a
ucomiss 0x8(%rsp), %xmm0
jbe 0x903a
movss %xmm0, (%rbx)
movq %r12, %rdi
callq 0x8924
leaq 0xc(%rbx), %r12
movlps %xmm0, 0xc(%rbx)
movss %xmm1, 0x14(%rbx)
movq %r12, %rdi
movq %r15, %rsi
callq 0x9449
movss 0x14(%r14), %xmm2
divss %xmm2, %xmm1
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
divps %xmm2, %xmm0
leaq 0x10(%rsp), %rdi
movlps %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x4(%rbx), %rsi
leaq 0x8(%rbx), %rdx
callq 0x11f9f
movq %r12, %rdi
movq %r15, %rsi
callq 0x9449
movss 0x14(%r14), %xmm2
divss %xmm2, %xmm1
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
divps %xmm2, %xmm0
movlps %xmm0, 0x18(%rbx)
movss %xmm1, 0x20(%rbx)
addq $0x18, %r14
addq $0x28, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x894e
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq