// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Dist.cpp
// Function: gaps::R2Distance(gaps::R2Point const&, gaps::R2Box const&)
RNLength R2Distance(const R2Point& point, const R2Box& box)
{
    // Find axial distances from point to box
    RNLength dx, dy;
    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;
    
    // Return distance between point and closest point in box 
    if (dy == 0.0) return dx;
    else if (dx == 0.0) return dy;
    else return sqrt(dx*dx + dy*dy);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x38(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa3790
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
leaq 0xacb18(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xcba05
movq 0x58(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x30(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa3790
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xcba77
movq 0x58(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x28(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa3750
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
leaq 0xacab1(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x39630
cmpl $0x0, %eax
je 0xcba6c
movq 0x50(%rsp), %rdi
callq 0xa3750
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rdi
callq 0x38c50
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xcba75
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xcba77
movq 0x58(%rsp), %rdi
callq 0x38c60
movsd %xmm0, 0x18(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa37b0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
leaq 0xaca3f(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xcbade
movq 0x58(%rsp), %rdi
callq 0x38c60
movsd %xmm0, 0x10(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa37b0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xcbb4e
movq 0x58(%rsp), %rdi
callq 0x38c60
movsd %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa3770
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
leaq 0xac9d8(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x39630
cmpl $0x0, %eax
je 0xcbb43
movq 0x50(%rsp), %rdi
callq 0xa3770
movsd %xmm0, (%rsp)
movq 0x58(%rsp), %rdi
callq 0x38c60
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xcbb4c
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xcbb4e
movsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xcbb6d
jp 0xcbb6d
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xcbbb7
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xcbb8c
jp 0xcbb8c
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xcbbb7
movsd 0x48(%rsp), %xmm0
movsd 0x48(%rsp), %xmm2
movsd 0x40(%rsp), %xmm1
mulsd 0x40(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0xf390
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)