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

    // Find axial distances from box1 to box2
    RNLength dx, dy, dz;
    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;
    if (RNIsGreater(box1.ZMin(), box2.ZMax())) dz = box1.ZMin() - box2.ZMax();
    else if (RNIsGreater(box2.ZMin(), box1.ZMax())) dz = box2.ZMin() - box1.ZMax();
    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 0x88(%rsp), %rdi
callq 0x44d80
cmpl $0x0, %eax
je 0x8ae0f
movsd 0xa821f(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x90(%rsp)
jmp 0x8b1e8
movq 0x80(%rsp), %rdi
callq 0x44d80
cmpl $0x0, %eax
je 0x8ae37
movsd 0xa81f7(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x90(%rsp)
jmp 0x8b1e8
movq 0x88(%rsp), %rdi
callq 0x37830
movsd %xmm0, 0x60(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44f80
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
leaq 0xed679(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8aeaa
movq 0x88(%rsp), %rdi
callq 0x37830
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 0x8af28
movq 0x80(%rsp), %rdi
callq 0x37830
movsd %xmm0, 0x50(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44f80
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
leaq 0xed606(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8af1d
movq 0x80(%rsp), %rdi
callq 0x37830
movsd %xmm0, 0x48(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44f80
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x8af26
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x8af28
movq 0x88(%rsp), %rdi
callq 0x37880
movsd %xmm0, 0x40(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fa0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
leaq 0xed588(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8af9b
movq 0x88(%rsp), %rdi
callq 0x37880
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 0x8b019
movq 0x80(%rsp), %rdi
callq 0x37880
movsd %xmm0, 0x30(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44fa0
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
leaq 0xed515(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8b00e
movq 0x80(%rsp), %rdi
callq 0x37880
movsd %xmm0, 0x28(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44fa0
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x8b017
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x8b019
movq 0x88(%rsp), %rdi
callq 0x378d0
movsd %xmm0, 0x20(%rsp)
movq 0x80(%rsp), %rdi
callq 0x44fc0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
leaq 0xed497(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8b08c
movq 0x88(%rsp), %rdi
callq 0x378d0
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 0x8b10a
movq 0x80(%rsp), %rdi
callq 0x378d0
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44fc0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
leaq 0xed424(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x8b0ff
movq 0x80(%rsp), %rdi
callq 0x378d0
movsd %xmm0, 0x8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x44fc0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x8b108
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x8b10a
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b140
jp 0x8b140
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b140
jp 0x8b140
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x8b1e8
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b173
jp 0x8b173
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b173
jp 0x8b173
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x8b1e8
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b1a6
jp 0x8b1a6
movsd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b1a6
jp 0x8b1a6
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x8b1e8
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
nopl (%rax)