double min_pt2bx_dist2(double bx, double by, double bz, double dim, double x,
                       double y, double z) {
  double dx, dy, dz;
  double dl, dr;
  dl = bx - x;
  dr = -dim - dl;
  dx = (dl > 0) ? dl : ((dr > 0) ? dr : 0);
  dl = by - y;
  dr = -dim - dl;
  dy = (dl > 0) ? dl : ((dr > 0) ? dr : 0);
  dl = bz - z;
  dr = -dim - dl;
  dz = (dl > 0) ? dl : ((dr > 0) ? dr : 0);
  return (dx * dx + dy * dy + dz * dz);
}


x86 assembly (clang, -O0):
<min_pt2bx_dist2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  %xmm3,-0x20(%rbp)
movsd  %xmm4,-0x28(%rbp)
movsd  %xmm5,-0x30(%rbp)
movsd  %xmm6,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
subsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x58(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1181 <min_pt2bx_dist2+0x81>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
jmp    11b9 <min_pt2bx_dist2+0xb9>
movsd  -0x60(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11a2 <min_pt2bx_dist2+0xa2>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
jmp    11af <min_pt2bx_dist2+0xaf>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x70(%rbp)
jmp    11af <min_pt2bx_dist2+0xaf>
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
subsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x58(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1219 <min_pt2bx_dist2+0x119>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
jmp    1251 <min_pt2bx_dist2+0x151>
movsd  -0x60(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    123a <min_pt2bx_dist2+0x13a>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
jmp    1247 <min_pt2bx_dist2+0x147>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x80(%rbp)
jmp    1247 <min_pt2bx_dist2+0x147>
movsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
subsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x58(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12b4 <min_pt2bx_dist2+0x1b4>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
jmp    12f8 <min_pt2bx_dist2+0x1f8>
movsd  -0x60(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12d8 <min_pt2bx_dist2+0x1d8>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
jmp    12e8 <min_pt2bx_dist2+0x1e8>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
jmp    12e8 <min_pt2bx_dist2+0x1e8>
movsd  -0x90(%rbp),%xmm0
ff
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x50(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<min_pt2bx_dist2>:
subsd  %xmm4,%xmm0
xorpd  0xef4(%rip),%xmm3
movapd %xmm3,%xmm8
subsd  %xmm0,%xmm8
xorpd  %xmm4,%xmm4
maxsd  %xmm4,%xmm8
xorpd  %xmm7,%xmm7
cmpltsd %xmm0,%xmm7
andpd  %xmm7,%xmm0
andnpd %xmm8,%xmm7
orpd   %xmm7,%xmm0
subsd  %xmm5,%xmm1
movapd %xmm3,%xmm5
subsd  %xmm1,%xmm5
maxsd  %xmm4,%xmm5
xorpd  %xmm7,%xmm7
cmpltsd %xmm1,%xmm7
andpd  %xmm7,%xmm1
andnpd %xmm5,%xmm7
orpd   %xmm1,%xmm7
subsd  %xmm6,%xmm2
subsd  %xmm2,%xmm3
maxsd  %xmm4,%xmm3
cmpltsd %xmm2,%xmm4
andpd  %xmm4,%xmm2
andnpd %xmm3,%xmm4
orpd   %xmm2,%xmm4
mulsd  %xmm7,%xmm7
mulsd  %xmm0,%xmm0
addsd  %xmm7,%xmm0
mulsd  %xmm4,%xmm4
addsd  %xmm4,%xmm0
ret

LLVM IR (-O0):
define double @min_pt2bx_dist2(double %0, double %1, double %2, double %3, double %4, double %5, double %6) {
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca double
store double %0, double* %8
store double %1, double* %9
store double %2, double* %10
store double %3, double* %11
store double %4, double* %12
store double %5, double* %13
store double %6, double* %14
%20 = load double, double* %8
%21 = load double, double* %12
%22 = fsub double %20, %21
store double %22, double* %18
%23 = load double, double* %11
%24 = fneg double %23
%25 = load double, double* %18
%26 = fsub double %24, %25
store double %26, double* %19
%27 = load double, double* %18
%28 = fcmp ogt double %27, 0.000000e+00
br i1 %28, label %29, label %31
29: ; preds = %7
%30 = load double, double* %18
br label %39
31: ; preds = %7
%32 = load double, double* %19
%33 = fcmp ogt double %32, 0.000000e+00
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load double, double* %19
br label %37
36: ; preds = %31
br label %37
37: ; preds = %36, %34
%38 = phi double [ %35, %34 ], [ 0.000000e+00, %36 ]
br label %39
39: ; preds = %37, %29
%40 = phi double [ %30, %29 ], [ %38, %37 ]
store double %40, double* %15
%41 = load double, double* %9
%42 = load double, double* %13
%43 = fsub double %41, %42
store double %43, double* %18
%44 = load double, double* %11
%45 = fneg double %44
%46 = load double, double* %18
%47 = fsub double %45, %46
store double %47, double* %19
%48 = load double, double* %18
%49 = fcmp ogt double %48, 0.000000e+00
br i1 %49, label %50, label %52
50: ; preds = %39
%51 = load double, double* %18
br label %60
52: ; preds = %39
%53 = load double, double* %19
%54 = fcmp ogt double %53, 0.000000e+00
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = load double, double* %19
br label %58
57: ; preds = %52
br label %58
58: ; preds = %57, %55
%59 = phi double [ %56, %55 ], [ 0.000000e+00, %57 ]
br label %60
60: ; preds = %58, %50
%61 = phi double [ %51, %50 ], [ %59, %58 ]
store double %61, double* %16
%62 = load double, double* %10
%63 = load double, double* %14
%64 = fsub double %62, %63
store double %64, double* %18
%65 = load double, double* %11
%66 = fneg double %65
%67 = load double, double* %18
%68 = fsub double %66, %67
store double %68, double* %19
%69 = load double, double* %18
%70 = fcmp ogt double %69, 0.000000e+00
br i1 %70, label %71, label %73
71: ; preds = %60
%72 = load double, double* %18
br label %81
73: ; preds = %60
%74 = load double, double* %19
%75 = fcmp ogt double %74, 0.000000e+00
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = load double, double* %19
br label %79
78: ; preds = %73
br label %79
79: ; preds = %78, %76
%80 = phi double [ %77, %76 ], [ 0.000000e+00, %78 ]
br label %81
81: ; preds = %79, %71
%82 = phi double [ %72, %71 ], [ %80, %79 ]
store double %82, double* %17
%83 = load double, double* %15
%84 = load double, double* %15
%85 = load double, double* %16
%86 = load double, double* %16
%87 = fmul double %85, %86
%88 = call double @llvm.fmuladd.f64(double %83, double %84, double %87)
%89 = load double, double* %17
%90 = load double, double* %17
%91 = call double @llvm.fmuladd.f64(double %89, double %90, double %88)
ret double %91
}

LLVM IR (-O2):
define double @min_pt2bx_dist2(double %0, double %1, double %2, double %3, double %4, double %5, double %6) {
%8 = fsub double %0, %4
%9 = fneg double %3
%10 = fsub double %9, %8
%11 = fcmp ogt double %8, 0.000000e+00
%12 = fcmp ogt double %10, 0.000000e+00
%13 = select i1 %12, double %10, double 0.000000e+00
%14 = select i1 %11, double %8, double %13
%15 = fsub double %1, %5
%16 = fsub double %9, %15
%17 = fcmp ogt double %15, 0.000000e+00
%18 = fcmp ogt double %16, 0.000000e+00
%19 = select i1 %18, double %16, double 0.000000e+00
%20 = select i1 %17, double %15, double %19
%21 = fsub double %2, %6
%22 = fsub double %9, %21
%23 = fcmp ogt double %21, 0.000000e+00
%24 = fcmp ogt double %22, 0.000000e+00
%25 = select i1 %24, double %22, double 0.000000e+00
%26 = select i1 %23, double %21, double %25
%27 = fmul double %20, %20
%28 = tail call double @llvm.fmuladd.f64(double %14, double %14, double %27)
%29 = tail call double @llvm.fmuladd.f64(double %26, double %26, double %28)
ret double %29
}