// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Dist.cpp
// Function: gaps::R3Distance(gaps::R3Point const&, gaps::R3Box const&)
RNLength R3Distance(const R3Point& point, const R3Box& box)
{
    // Check if box is empty
    if (box.IsEmpty()) return FLT_MAX;

    // Find axial distances from point to box
    RNLength dx, dy, dz;
    if (RNIsGreater(point.X(), box.XMax())) dx = point.X() - box.XMax();
    else if (RNIsLess(point.X(), box.XMin())) dx = box.XMin()- point.X();
    else dx = 0.0;
    if (RNIsGreater(point.Y(), box.YMax())) dy = point.Y() - box.YMax();
    else if (RNIsLess(point.Y(), box.YMin())) dy = box.YMin()- point.Y();
    else dy = 0.0;
    if (RNIsGreater(point.Z(), box.ZMax())) dz = point.Z() - box.ZMax();
    else if (RNIsLess(point.Z(), box.ZMin())) dz = box.ZMin()- point.Z();
    else dz = 0.0;
    
    // Return distance between point and closest point in box 
    if ((dy == 0.0) && (dz == 0.0)) return dx;
    else if ((dx == 0.0) && (dz == 0.0)) return dy;
    else if ((dx == 0.0) && (dy == 0.0)) return dz;
    else return sqrt(dx*dx + dy*dy + dz*dz);
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44d80
cmpl $0x0, %eax
je 0x8711f
movsd 0xabf0f(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x90(%rsp)
jmp 0x874d0
movq 0x88(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x60(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44f80
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
leaq 0xf1391(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x87192
movq 0x88(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x58(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44f80
movaps %xmm0, %xmm1
movsd 0x58(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x87210
movq 0x88(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x50(%rsp)
movq 0x80(%rsp), %rdi
callq 0x37830
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
leaq 0xf131e(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x39630
cmpl $0x0, %eax
je 0x87205
movq 0x80(%rsp), %rdi
callq 0x37830
movsd %xmm0, 0x48(%rsp)
movq 0x88(%rsp), %rdi
callq 0x37820
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x8720e
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x87210
movq 0x88(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x40(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fa0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
leaq 0xf12a0(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x87283
movq 0x88(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x38(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fa0
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x87301
movq 0x88(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x30(%rsp)
movq 0x80(%rsp), %rdi
callq 0x37880
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
leaq 0xf122d(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x39630
cmpl $0x0, %eax
je 0x872f6
movq 0x80(%rsp), %rdi
callq 0x37880
movsd %xmm0, 0x28(%rsp)
movq 0x88(%rsp), %rdi
callq 0x37870
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x872ff
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x87301
movq 0x88(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x20(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fc0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
leaq 0xf11af(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x87374
movq 0x88(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x18(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fc0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x873f2
movq 0x88(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x10(%rsp)
movq 0x80(%rsp), %rdi
callq 0x378d0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
leaq 0xf113c(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x39630
cmpl $0x0, %eax
je 0x873e7
movq 0x80(%rsp), %rdi
callq 0x378d0
movsd %xmm0, 0x8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x378c0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x873f0
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x873f2
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x87428
jp 0x87428
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x87428
jp 0x87428
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x874d0
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8745b
jp 0x8745b
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8745b
jp 0x8745b
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x874d0
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8748e
jp 0x8748e
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8748e
jp 0x8748e
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x874d0
movsd 0x78(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movsd 0x70(%rsp), %xmm0
mulsd 0x70(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x68(%rsp), %xmm0
movsd 0x68(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0xf390
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)