// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Dist.cpp
// Function: gaps::R2Distance(gaps::R2Box const&, gaps::R2Box const&)
RNLength R2Distance(const R2Box& box1, const R2Box& box2)
{
    // Find axial distances from box1 to box2
    RNLength dx, dy;
    if (RNIsGreater(box1.XMin(), box2.XMax())) dx = box1.XMin() - box2.XMax();
    else if (RNIsGreater(box2.XMin(), box1.XMax())) dx = box2.XMin() - box1.XMax();
    else dx = 0.0;
    if (RNIsGreater(box1.YMin(), box2.YMax())) dy = box1.YMin() - box2.YMax();
    else if (RNIsGreater(box2.YMin(), box1.YMax())) dy = box2.YMin() - box1.YMax();
    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 0xa3750
movsd %xmm0, 0x38(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa3790
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
leaq 0xab6d8(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xcce45
movq 0x58(%rsp), %rdi
callq 0xa3750
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 0xcceb7
movq 0x50(%rsp), %rdi
callq 0xa3750
movsd %xmm0, 0x28(%rsp)
movq 0x58(%rsp), %rdi
callq 0xa3790
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
leaq 0xab671(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xcceac
movq 0x50(%rsp), %rdi
callq 0xa3750
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rdi
callq 0xa3790
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xcceb5
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xcceb7
movq 0x58(%rsp), %rdi
callq 0xa3770
movsd %xmm0, 0x18(%rsp)
movq 0x50(%rsp), %rdi
callq 0xa37b0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
leaq 0xab5ff(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xccf1e
movq 0x58(%rsp), %rdi
callq 0xa3770
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 0xccf8e
movq 0x50(%rsp), %rdi
callq 0xa3770
movsd %xmm0, 0x8(%rsp)
movq 0x58(%rsp), %rdi
callq 0xa37b0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
leaq 0xab598(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0xccf83
movq 0x50(%rsp), %rdi
callq 0xa3770
movsd %xmm0, (%rsp)
movq 0x58(%rsp), %rdi
callq 0xa37b0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xccf8c
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0xccf8e
movsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xccfad
jp 0xccfad
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xccff7
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xccfcc
jp 0xccfcc
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xccff7
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)