extern double potential_derivative(double x, double x0) {
  if (x0 < 1) {
    if (x < x0)
      return -1.0;
    if (x < 1)
      return -(1 - x) / (1 - x0);
  } else {
    if (x < 1)
      return -1.0;
  }
  return 0.0;
}


x86 assembly (clang, -O0):
<potential_derivative>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  0xeea(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    119e <potential_derivative+0x9e>
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1147 <potential_derivative+0x47>
movsd  0xecb(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d0 <potential_derivative+0xd0>
movsd  0xeb1(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1199 <potential_derivative+0x99>
movsd  0xe9e(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xe7a(%rip),%xmm1
subsd  -0x18(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d0 <potential_derivative+0xd0>
jmp    11c8 <potential_derivative+0xc8>
movsd  0xe5a(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11c3 <potential_derivative+0xc3>
movsd  0xe4f(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d0 <potential_derivative+0xd0>
jmp    11c8 <potential_derivative+0xc8>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<potential_derivative>:
movapd %xmm0,%xmm2
movsd  0xef4(%rip),%xmm3
ucomisd %xmm1,%xmm3
jbe    1121 <potential_derivative+0x21>
ucomisd %xmm2,%xmm1
jbe    1130 <potential_derivative+0x30>
movsd  0xee8(%rip),%xmm0
ret
ucomisd %xmm2,%xmm3
jbe    114f <potential_derivative+0x4f>
movsd  0xed9(%rip),%xmm0
ret
ucomisd %xmm2,%xmm3
jbe    114f <potential_derivative+0x4f>
movapd %xmm3,%xmm0
subsd  %xmm2,%xmm0
xorpd  0xeca(%rip),%xmm0
subsd  %xmm1,%xmm3
divsd  %xmm3,%xmm0
ret
xorpd  %xmm0,%xmm0
ret

LLVM IR (-O0):
define double @potential_derivative(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 = fcmp olt double %6, 1.000000e+00
br i1 %7, label %8, label %24
8: ; preds = %2
%9 = load double, double* %4
%10 = load double, double* %5
%11 = fcmp olt double %9, %10
br i1 %11, label %12, label %13
12: ; preds = %8
store double -1.000000e+00, double* %3
br label %30
13: ; preds = %8
%14 = load double, double* %4
%15 = fcmp olt double %14, 1.000000e+00
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = load double, double* %4
%18 = fsub double 1.000000e+00, %17
%19 = fneg double %18
%20 = load double, double* %5
%21 = fsub double 1.000000e+00, %20
%22 = fdiv double %19, %21
store double %22, double* %3
br label %30
23: ; preds = %13
br label %29
24: ; preds = %2
%25 = load double, double* %4
%26 = fcmp olt double %25, 1.000000e+00
br i1 %26, label %27, label %28
27: ; preds = %24
store double -1.000000e+00, double* %3
br label %30
28: ; preds = %24
br label %29
29: ; preds = %28, %23
store double 0.000000e+00, double* %3
br label %30
30: ; preds = %29, %27, %16, %12
%31 = load double, double* %3
ret double %31
}

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