// File: IsaacLaw12[P]RayTracer/./src/BoundingBox.cpp
// Function: BoundingBox::ray_intersects(Ray&, double&, double&)
bool BoundingBox::ray_intersects(Ray& ray, double &tmin, double &tmax){
    const Eigen::Vector3d& ray_pt = ray.get_point();
    const Eigen::Vector3d& ray_dir = ray.get_dir();
    tmin = (min_corner(0) - ray_pt(0)) / ray_dir(0);
    tmax = (max_corner(0) - ray_pt(0)) / ray_dir(0);

    if (tmin > tmax) std::swap(tmin, tmax);

    double tymin = (min_corner(1) - ray_pt(1)) / ray_dir(1);
    double tymax = (max_corner(1) - ray_pt(1)) / ray_dir(1);

    if (tymin > tymax) std::swap(tymin, tymax);

    if ((tmin > tymax) || (tymin > tmax))
        return false;

    if (tymin > tmin)
        tmin = tymin;

    if (tymax < tmax)
        tmax = tymax;

    double tzmin = (min_corner(2) - ray_pt(2)) / ray_dir(2);
    double tzmax = (max_corner(2) - ray_pt(2)) / ray_dir(2);

    if (tzmin > tzmax) std::swap(tzmin, tzmax);

    if ((tmin > tzmax) || (tzmin > tmax))
        return false;

    if (tzmin > tmin)
        tmin = tzmin;

    if (tzmax < tmax)
        tmax = tzmax;

    return true;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x5cb90
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x5cba0
movq 0x58(%rsp), %rdi
movq %rax, 0x90(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x98(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16a90
movsd 0x48(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x90(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16a90
movsd 0x50(%rsp), %xmm0
movq 0x58(%rsp), %rdi
divsd (%rax), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x98(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16a90
movsd 0x60(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x90(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16a90
movsd 0x68(%rsp), %xmm0
divsd (%rax), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
movq 0xa0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x16791
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0xbb30
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x98(%rsp), %rdi
movl $0x1, %esi
callq 0x16a90
movsd 0x28(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x90(%rsp), %rdi
movl $0x1, %esi
callq 0x16a90
movsd 0x30(%rsp), %xmm0
movq 0x58(%rsp), %rdi
divsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
addq $0x18, %rdi
movl $0x1, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x98(%rsp), %rdi
movl $0x1, %esi
callq 0x16a90
movsd 0x38(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x90(%rsp), %rdi
movl $0x1, %esi
callq 0x16a90
movsd 0x40(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
ucomisd 0x80(%rsp), %xmm0
jbe 0x1687e
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xbb30
movq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
ucomisd 0x80(%rsp), %xmm0
ja 0x168ac
movsd 0x88(%rsp), %xmm0
movq 0xa0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x168b9
movb $0x0, 0xc7(%rsp)
jmp 0x16a7a
movsd 0x88(%rsp), %xmm0
movq 0xa8(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x168e5
movsd 0x88(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x80(%rsp), %xmm1
movq 0xa0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16915
movsd 0x80(%rsp), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rdi
movl $0x2, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x98(%rsp), %rdi
movl $0x2, %esi
callq 0x16a90
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x90(%rsp), %rdi
movl $0x2, %esi
callq 0x16a90
movsd 0x10(%rsp), %xmm0
movq 0x58(%rsp), %rdi
divsd (%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
addq $0x18, %rdi
movl $0x2, %esi
callq 0x6130
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x98(%rsp), %rdi
movl $0x2, %esi
callq 0x16a90
movsd 0x18(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x90(%rsp), %rdi
movl $0x2, %esi
callq 0x16a90
movsd 0x20(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x78(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x169f0
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xbb30
movq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
ucomisd 0x70(%rsp), %xmm0
ja 0x16a18
movsd 0x78(%rsp), %xmm0
movq 0xa0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x16a22
movb $0x0, 0xc7(%rsp)
jmp 0x16a7a
movsd 0x78(%rsp), %xmm0
movq 0xa8(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x16a48
movsd 0x78(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x70(%rsp), %xmm1
movq 0xa0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16a72
movsd 0x70(%rsp), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, (%rax)
movb $0x1, 0xc7(%rsp)
movb 0xc7(%rsp), %al
andb $0x1, %al
addq $0xc8, %rsp
retq
nopl (%rax,%rax)