double _quadratic_diff(double x) {
  double factor;
  if (x < 0) {
    x = -x;
    factor = -1;
  } else {
    factor = +1;
  }
  if (x <= 0.5) {
    return factor * (-2 * x);
  }
  if (x < 1.5) {
    return factor * (-(1.5 - x));
  }
  return 0;
}


x86 assembly (clang, -O0):
<_quadratic_diff>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    114a <_quadratic_diff+0x4a>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xec8(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1157 <_quadratic_diff+0x57>
movsd  0xeae(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xeb1(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     118a <_quadratic_diff+0x8a>
movsd  -0x18(%rbp),%xmm0
movsd  0xea9(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dc <_quadratic_diff+0xdc>
movsd  0xe86(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11d4 <_quadratic_diff+0xd4>
movsd  -0x18(%rbp),%xmm0
movsd  0xe6e(%rip),%xmm1
subsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dc <_quadratic_diff+0xdc>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<_quadratic_diff>:
xorpd  %xmm1,%xmm1
xor    %eax,%eax
ucomisd %xmm0,%xmm1
seta   %cl
jbe    1117 <_quadratic_diff+0x17>
xorpd  0xee9(%rip),%xmm0
mov    %cl,%al
lea    0xef0(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm2
movsd  0xef3(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    1157 <_quadratic_diff+0x57>
xorpd  %xmm1,%xmm1
movsd  0xef1(%rip),%xmm3
ucomisd %xmm0,%xmm3
jbe    1167 <_quadratic_diff+0x67>
subsd  %xmm0,%xmm3
xorpd  0xeaf(%rip),%xmm3
mulsd  %xmm3,%xmm2
jmp    1163 <_quadratic_diff+0x63>
mulsd  0xec9(%rip),%xmm0
mulsd  %xmm0,%xmm2
movapd %xmm2,%xmm1
movapd %xmm1,%xmm0
ret

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

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