double clamp(double input, double low, double high) {
  if (low < high) {
    if (input > high)
      return high;
    if (input < low)
      return low;
    return input;
  } else {
    if (input < high)
      return high;
    if (input > low)
      return low;
    return input;
  }
}


x86 assembly (clang, -O0):
<clamp>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1178 <clamp+0x78>
movsd  -0x10(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1146 <clamp+0x46>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c4 <clamp+0xc4>
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1169 <clamp+0x69>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c4 <clamp+0xc4>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c4 <clamp+0xc4>
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    119b <clamp+0x9b>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c4 <clamp+0xc4>
movsd  -0x10(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11ba <clamp+0xba>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c4 <clamp+0xc4>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<clamp>:
ucomisd %xmm1,%xmm2
jbe    1112 <clamp+0x12>
ucomisd %xmm2,%xmm0
ja     1120 <clamp+0x20>
maxsd  %xmm0,%xmm1
jmp    111c <clamp+0x1c>
ucomisd %xmm0,%xmm2
ja     1120 <clamp+0x20>
minsd  %xmm0,%xmm1
movapd %xmm1,%xmm2
movapd %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @clamp(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%8 = load double, double* %6
%9 = load double, double* %7
%10 = fcmp olt double %8, %9
br i1 %10, label %11, label %25
11: ; preds = %3
%12 = load double, double* %5
%13 = load double, double* %7
%14 = fcmp ogt double %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load double, double* %7
store double %16, double* %4
br label %39
17: ; preds = %11
%18 = load double, double* %5
%19 = load double, double* %6
%20 = fcmp olt double %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load double, double* %6
store double %22, double* %4
br label %39
23: ; preds = %17
%24 = load double, double* %5
store double %24, double* %4
br label %39
25: ; preds = %3
%26 = load double, double* %5
%27 = load double, double* %7
%28 = fcmp olt double %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load double, double* %7
store double %30, double* %4
br label %39
31: ; preds = %25
%32 = load double, double* %5
%33 = load double, double* %6
%34 = fcmp ogt double %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load double, double* %6
store double %36, double* %4
br label %39
37: ; preds = %31
%38 = load double, double* %5
store double %38, double* %4
br label %39
39: ; preds = %37, %35, %29, %23, %21, %15
%40 = load double, double* %4
ret double %40
}

LLVM IR (-O2):
define double @clamp(double %0, double %1, double %2) {
%4 = fcmp olt double %1, %2
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = fcmp ogt double %0, %2
br i1 %6, label %15, label %7
7: ; preds = %5
%8 = fcmp olt double %0, %1
%9 = select i1 %8, double %1, double %0
br label %15
10: ; preds = %3
%11 = fcmp olt double %0, %2
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = fcmp ogt double %0, %1
%14 = select i1 %13, double %1, double %0
br label %15
15: ; preds = %12, %10, %7, %5
%16 = phi double [ %2, %5 ], [ %9, %7 ], [ %2, %10 ], [ %14, %12 ]
ret double %16
}