// File: alan0201tw[P]RayTracer/src/sphere.cpp
// Function: moving_sphere::hit(ray const&, float, float, hit_record&) const
bool moving_sphere::hit(const ray& _ray, float t_min, float t_max, hit_record& rec) const
{
    vec3 current_center = center(_ray.get_time());
    vec3 oc = _ray.origin() - current_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 - current_center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - current_center) / radius;
            rec.material_ref = moving_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 - current_center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - current_center) / radius;
            rec.material_ref = moving_sphere_mat;
            return true;
        }
    }
    
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x18(%rbp), %rdi
callq 0x11ae0
movq -0x268(%rbp), %rdi
callq 0x119f0
movss %xmm1, -0x40(%rbp)
movlpd %xmm0, -0x48(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf870
movss %xmm1, -0x70(%rbp)
movlpd %xmm0, -0x78(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11890
movss %xmm1, -0x80(%rbp)
movlpd %xmm0, -0x88(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xa0(%rbp)
movlpd %xmm0, -0xa8(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xc0(%rbp)
movlpd %xmm0, -0xc8(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x11900
movss %xmm0, -0x8c(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xe0(%rbp)
movlpd %xmm0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xd0(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x11900
movss %xmm0, -0xcc(%rbp)
leaq -0x58(%rbp), %rsi
movq %rsi, %rdi
callq 0x11900
movq -0x268(%rbp), %rax
movaps %xmm0, %xmm1
movss 0x28(%rax), %xmm0
movss 0x28(%rax), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xec(%rbp)
movss -0xcc(%rbp), %xmm0
movss -0xcc(%rbp), %xmm2
movss -0x8c(%rbp), %xmm1
mulss -0xec(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xf0(%rbp)
movss -0xf0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x11582
movss -0xcc(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x26c(%rbp)
movss -0xf0(%rbp), %xmm0
callq 0x11960
movaps %xmm0, %xmm1
movss -0x26c(%rbp), %xmm0
subss %xmm1, %xmm0
divss -0x8c(%rbp), %xmm0
movss %xmm0, -0xf4(%rbp)
movss -0xf4(%rbp), %xmm1
movss -0x20(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11311
movss -0xf4(%rbp), %xmm0
ucomiss -0x1c(%rbp), %xmm0
jbe 0x11311
movss -0xf4(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movss (%rax), %xmm0
callq 0xf920
movss %xmm1, -0x108(%rbp)
movlpd %xmm0, -0x110(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0xf8(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x28(%rbp), %rax
movl -0xf8(%rbp), %ecx
movl %ecx, 0x14(%rax)
movq -0x100(%rbp), %rcx
movq %rcx, 0xc(%rax)
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
leaq -0x38(%rbp), %rsi
movq %rsi, -0x278(%rbp)
callq 0x11890
movq -0x268(%rbp), %rax
movss %xmm1, -0x138(%rbp)
movlpd %xmm0, -0x140(%rbp)
movl -0x138(%rbp), %ecx
movl %ecx, -0x128(%rbp)
movq -0x140(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movss 0x28(%rax), %xmm2
movl -0x128(%rbp), %eax
movl %eax, -0x148(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x150(%rbp)
movsd -0x150(%rbp), %xmm0
movss -0x148(%rbp), %xmm1
callq 0x11980
movss %xmm1, -0x158(%rbp)
movlpd %xmm0, -0x160(%rbp)
movl -0x158(%rbp), %eax
movl %eax, -0x118(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x28(%rbp), %rdx
movq %rdx, %rsi
addq $0x4, %rsi
addq $0x8, %rdx
leaq -0x120(%rbp), %rdi
callq 0x24bc0
movq -0x278(%rbp), %rsi
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
callq 0x11890
movq -0x268(%rbp), %rax
movss %xmm1, -0x180(%rbp)
movlpd %xmm0, -0x188(%rbp)
movl -0x180(%rbp), %ecx
movl %ecx, -0x170(%rbp)
movq -0x188(%rbp), %rcx
movq %rcx, -0x178(%rbp)
movss 0x28(%rax), %xmm2
movl -0x170(%rbp), %eax
movl %eax, -0x190(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x198(%rbp)
movsd -0x198(%rbp), %xmm0
movss -0x190(%rbp), %xmm1
callq 0x11980
movq -0x268(%rbp), %rsi
movss %xmm1, -0x1a0(%rbp)
movlpd %xmm0, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x16c(%rbp)
movl -0x1a0(%rbp), %eax
movl %eax, -0x164(%rbp)
movq -0x28(%rbp), %rax
movq -0x16c(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl -0x164(%rbp), %ecx
movl %ecx, 0x20(%rax)
addq $0x30, %rsi
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
callq 0xf8f0
movb $0x1, -0x1(%rbp)
jmp 0x11586
movss -0xcc(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x27c(%rbp)
movss -0xf0(%rbp), %xmm0
callq 0x11960
movaps %xmm0, %xmm1
movss -0x27c(%rbp), %xmm0
addss %xmm1, %xmm0
divss -0x8c(%rbp), %xmm0
movss %xmm0, -0xf4(%rbp)
movss -0xf4(%rbp), %xmm1
movss -0x20(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11580
movss -0xf4(%rbp), %xmm0
ucomiss -0x1c(%rbp), %xmm0
jbe 0x11580
movss -0xf4(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movss (%rax), %xmm0
callq 0xf920
movss %xmm1, -0x1c0(%rbp)
movlpd %xmm0, -0x1c8(%rbp)
movl -0x1c0(%rbp), %eax
movl %eax, -0x1b0(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x28(%rbp), %rax
movl -0x1b0(%rbp), %ecx
movl %ecx, 0x14(%rax)
movq -0x1b8(%rbp), %rcx
movq %rcx, 0xc(%rax)
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
leaq -0x38(%rbp), %rsi
movq %rsi, -0x288(%rbp)
callq 0x11890
movq -0x268(%rbp), %rax
movss %xmm1, -0x1f0(%rbp)
movlpd %xmm0, -0x1f8(%rbp)
movl -0x1f0(%rbp), %ecx
movl %ecx, -0x1e0(%rbp)
movq -0x1f8(%rbp), %rcx
movq %rcx, -0x1e8(%rbp)
movss 0x28(%rax), %xmm2
movl -0x1e0(%rbp), %eax
movl %eax, -0x200(%rbp)
movq -0x1e8(%rbp), %rax
movq %rax, -0x208(%rbp)
movsd -0x208(%rbp), %xmm0
movss -0x200(%rbp), %xmm1
callq 0x11980
movss %xmm1, -0x210(%rbp)
movlpd %xmm0, -0x218(%rbp)
movl -0x210(%rbp), %eax
movl %eax, -0x1d0(%rbp)
movq -0x218(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x28(%rbp), %rdx
movq %rdx, %rsi
addq $0x4, %rsi
addq $0x8, %rdx
leaq -0x1d8(%rbp), %rdi
callq 0x24bc0
movq -0x288(%rbp), %rsi
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
callq 0x11890
movq -0x268(%rbp), %rax
movss %xmm1, -0x238(%rbp)
movlpd %xmm0, -0x240(%rbp)
movl -0x238(%rbp), %ecx
movl %ecx, -0x228(%rbp)
movq -0x240(%rbp), %rcx
movq %rcx, -0x230(%rbp)
movss 0x28(%rax), %xmm2
movl -0x228(%rbp), %eax
movl %eax, -0x248(%rbp)
movq -0x230(%rbp), %rax
movq %rax, -0x250(%rbp)
movsd -0x250(%rbp), %xmm0
movss -0x248(%rbp), %xmm1
callq 0x11980
movq -0x268(%rbp), %rsi
movss %xmm1, -0x258(%rbp)
movlpd %xmm0, -0x260(%rbp)
movq -0x260(%rbp), %rax
movq %rax, -0x224(%rbp)
movl -0x258(%rbp), %eax
movl %eax, -0x21c(%rbp)
movq -0x28(%rbp), %rax
movq -0x224(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl -0x21c(%rbp), %ecx
movl %ecx, 0x20(%rax)
addq $0x30, %rsi
movq -0x28(%rbp), %rdi
addq $0x28, %rdi
callq 0xf8f0
movb $0x1, -0x1(%rbp)
jmp 0x11586
jmp 0x11582
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
nopw %cs:(%rax,%rax)