double triangular_fun(double b) {
  b = b * 1.5 / 2.0;
  if (-1.0 < b && b <= 0.0) {
    return b + 1.0;
  } else if (0.0 < b && b <= 1.0) {
    return 1.0 - b;
  }
  return 0;
}


x86 assembly (clang, -O0):
<triangular_fun>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  0xeff(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  0xeea(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xecc(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1163 <triangular_fun+0x63>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     1163 <triangular_fun+0x63>
movsd  0xec4(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ac <triangular_fun+0xac>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    119f <triangular_fun+0x9f>
movsd  0xe9b(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     119f <triangular_fun+0x9f>
movsd  0xe88(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ac <triangular_fun+0xac>
jmp    11a4 <triangular_fun+0xa4>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<triangular_fun>:
mulsd  0xef8(%rip),%xmm0
mulsd  0xef8(%rip),%xmm0
ucomisd 0xef8(%rip),%xmm0
jbe    112d <triangular_fun+0x2d>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jb     112d <triangular_fun+0x2d>
addsd  0xeec(%rip),%xmm0
ret
movsd  0xee3(%rip),%xmm2
movapd %xmm0,%xmm1
cmplesd %xmm2,%xmm1
subsd  %xmm0,%xmm2
andpd  %xmm1,%xmm2
xorpd  %xmm1,%xmm1
cmpltsd %xmm0,%xmm1
andpd  %xmm2,%xmm1
movapd %xmm1,%xmm0
ret

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

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