double calc_linear_factor(double middle, double z) {
  if (z <= middle) {
    if (middle < 1e-10)
      return 0.0;
    else
      return 0.5 * z / middle;
  } else {
    z -= middle;
    middle = 1.0 - middle;
    if (middle < 1e-10)
      return 1.0;
    else
      return 0.5 + 0.5 * z / middle;
  }
}


x86 assembly (clang, -O0):
<calc_linear_factor>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     115e <calc_linear_factor+0x5e>
movsd  0xed6(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1142 <calc_linear_factor+0x42>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11cb <calc_linear_factor+0xcb>
movsd  0xec6(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
divsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11cb <calc_linear_factor+0xcb>
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe8f(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xe75(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11a8 <calc_linear_factor+0xa8>
movsd  0xe6a(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11cb <calc_linear_factor+0xcb>
movsd  0xe60(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
divsd  -0x10(%rbp),%xmm1
movsd  0xe4e(%rip),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_linear_factor>:
ucomisd %xmm1,%xmm0
jae    113e <calc_linear_factor+0x3e>
movsd  0xf02(%rip),%xmm2
movapd %xmm2,%xmm3
subsd  %xmm0,%xmm3
movsd  0xee2(%rip),%xmm4
ucomisd %xmm3,%xmm4
ja     1160 <calc_linear_factor+0x60>
subsd  %xmm0,%xmm1
movsd  0xed8(%rip),%xmm0
mulsd  %xmm0,%xmm1
divsd  %xmm3,%xmm1
addsd  %xmm0,%xmm1
jmp    115c <calc_linear_factor+0x5c>
xorpd  %xmm2,%xmm2
movsd  0xeb6(%rip),%xmm3
ucomisd %xmm0,%xmm3
ja     1160 <calc_linear_factor+0x60>
mulsd  0xeb0(%rip),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm2
movapd %xmm2,%xmm0
ret

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

LLVM IR (-O2):
define double @calc_linear_factor(double %0, double %1) {
%3 = fcmp ugt double %1, %0
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = fcmp olt double %0, 1.000000e-10
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = fmul double %1, 5.000000e-01
%8 = fdiv double %7, %0
br label %17
9: ; preds = %2
%10 = fsub double 1.000000e+00, %0
%11 = fcmp olt double %10, 1.000000e-10
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = fsub double %1, %0
%14 = fmul double %13, 5.000000e-01
%15 = fdiv double %14, %10
%16 = fadd double %15, 5.000000e-01
br label %17
17: ; preds = %9, %4, %12, %6
%18 = phi double [ %8, %6 ], [ %16, %12 ], [ 0.000000e+00, %4 ], [ 1.000000e+00, %9 ]
ret double %18
}