double chrange(double x, double l, double m, double h) {
  if (x >= 0.) {
    if (x > 1.)
      x = 1.;
    x = m + (h - m) * x;
  } else {
    if (x < -1.)
      x = -1.;
    x = m + (m - l) * x;
  }
  return x;
}


x86 assembly (clang, -O0):
<chrange>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  %xmm3,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1174 <chrange+0x74>
movsd  -0x8(%rbp),%xmm0
movsd  0xed1(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    114e <chrange+0x4e>
movsd  0xebf(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b5 <chrange+0xb5>
movsd  0xe84(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    1194 <chrange+0x94>
movsd  0xe71(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrange>:
xorpd  %xmm4,%xmm4
ucomisd %xmm4,%xmm0
jae    1127 <chrange+0x27>
movsd  0xef6(%rip),%xmm3
maxsd  %xmm0,%xmm3
movapd %xmm2,%xmm0
subsd  %xmm1,%xmm0
mulsd  %xmm3,%xmm0
addsd  %xmm2,%xmm0
ret
movsd  0xed1(%rip),%xmm1
minsd  %xmm0,%xmm1
subsd  %xmm2,%xmm3
mulsd  %xmm1,%xmm3
addsd  %xmm2,%xmm3
movapd %xmm3,%xmm0
ret

LLVM IR (-O0):
define double @chrange(double %0, double %1, double %2, double %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
store double %3, double* %8
%9 = load double, double* %5
%10 = fcmp oge double %9, 0.000000e+00
br i1 %10, label %11, label %22
11: ; preds = %4
%12 = load double, double* %5
%13 = fcmp ogt double %12, 1.000000e+00
br i1 %13, label %14, label %15
14: ; preds = %11
store double 1.000000e+00, double* %5
br label %15
15: ; preds = %14, %11
%16 = load double, double* %7
%17 = load double, double* %8
%18 = load double, double* %7
%19 = fsub double %17, %18
%20 = load double, double* %5
%21 = call double @llvm.fmuladd.f64(double %19, double %20, double %16)
store double %21, double* %5
br label %33
22: ; preds = %4
%23 = load double, double* %5
%24 = fcmp olt double %23, -1.000000e+00
br i1 %24, label %25, label %26
25: ; preds = %22
store double -1.000000e+00, double* %5
br label %26
26: ; preds = %25, %22
%27 = load double, double* %7
%28 = load double, double* %7
%29 = load double, double* %6
%30 = fsub double %28, %29
%31 = load double, double* %5
%32 = call double @llvm.fmuladd.f64(double %30, double %31, double %27)
store double %32, double* %5
br label %33
33: ; preds = %26, %15
%34 = load double, double* %5
ret double %34
}

LLVM IR (-O2):
define double @chrange(double %0, double %1, double %2, double %3) {
%5 = fcmp ult double %0, 0.000000e+00
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = fcmp ogt double %0, 1.000000e+00
%8 = select i1 %7, double 1.000000e+00, double %0
%9 = fsub double %3, %2
%10 = tail call double @llvm.fmuladd.f64(double %9, double %8, double %2)
br label %16
11: ; preds = %4
%12 = fcmp olt double %0, -1.000000e+00
%13 = select i1 %12, double -1.000000e+00, double %0
%14 = fsub double %2, %1
%15 = tail call double @llvm.fmuladd.f64(double %14, double %13, double %2)
br label %16
16: ; preds = %11, %6
%17 = phi double [ %10, %6 ], [ %15, %11 ]
ret double %17
}