double doublet(double t0, double currentTime, double duration,
               double amplitude) {
  double t = currentTime;
  if (t < t0)
    return 0;
  else if ((t >= t0) && (t < t0 + 0.5 * duration))
    return amplitude;
  else if ((t >= t0 + 0.5 * duration) && (t < t0 + duration))
    return -amplitude;
  if (t >= t0 + duration)
    return 0;
  return 0;
}


x86 assembly (clang, -O0):
<doublet>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1143 <doublet+0x43>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    122b <doublet+0x12b>
movsd  -0x30(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     118b <doublet+0x8b>
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm0
movsd  0xe96(%rip),%xmm3
mulsd  %xmm3,%xmm0
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    118b <doublet+0x8b>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    122b <doublet+0x12b>
movsd  -0x30(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
movsd  0xe5e(%rip),%xmm3
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     11f3 <doublet+0xf3>
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11f3 <doublet+0xf3>
movsd  -0x28(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    122b <doublet+0x12b>
jmp    11f8 <doublet+0xf8>
jmp    11fd <doublet+0xfd>
movsd  -0x30(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
addsd  -0x20(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jb     1223 <doublet+0x123>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    122b <doublet+0x12b>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<doublet>:
xorpd  %xmm4,%xmm4
ucomisd %xmm1,%xmm0
ja     114c <doublet+0x4c>
ucomisd %xmm0,%xmm1
movsd  0xeea(%rip),%xmm4
mulsd  %xmm2,%xmm4
addsd  %xmm0,%xmm4
jb     112a <doublet+0x2a>
ucomisd %xmm1,%xmm4
jbe    112a <doublet+0x2a>
movaps %xmm3,%xmm0
ret
addsd  %xmm2,%xmm0
xorpd  0xeda(%rip),%xmm3
cmplesd %xmm1,%xmm4
cmpltsd %xmm0,%xmm1
andpd  %xmm3,%xmm1
andpd  %xmm4,%xmm1
movapd %xmm1,%xmm4
movapd %xmm4,%xmm0
ret

LLVM IR (-O0):
define double @doublet(double %0, double %1, double %2, double %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
%11 = load double, double* %7
store double %11, double* %10
%12 = load double, double* %10
%13 = load double, double* %6
%14 = fcmp olt double %12, %13
br i1 %14, label %15, label %16
15: ; preds = %4
store double 0.000000e+00, double* %5
br label %53
16: ; preds = %4
%17 = load double, double* %10
%18 = load double, double* %6
%19 = fcmp oge double %17, %18
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load double, double* %10
%22 = load double, double* %6
%23 = load double, double* %8
%24 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %23, double %22)
%25 = fcmp olt double %21, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load double, double* %9
store double %27, double* %5
br label %53
28: ; preds = %20, %16
%29 = load double, double* %10
%30 = load double, double* %6
%31 = load double, double* %8
%32 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %31, double %30)
%33 = fcmp oge double %29, %32
br i1 %33, label %34, label %43
34: ; preds = %28
%35 = load double, double* %10
%36 = load double, double* %6
%37 = load double, double* %8
%38 = fadd double %36, %37
%39 = fcmp olt double %35, %38
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load double, double* %9
%42 = fneg double %41
store double %42, double* %5
br label %53
43: ; preds = %34, %28
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = load double, double* %10
%47 = load double, double* %6
%48 = load double, double* %8
%49 = fadd double %47, %48
%50 = fcmp oge double %46, %49
br i1 %50, label %51, label %52
51: ; preds = %45
store double 0.000000e+00, double* %5
br label %53
52: ; preds = %45
store double 0.000000e+00, double* %5
br label %53
53: ; preds = %52, %51, %40, %26, %15
%54 = load double, double* %5
ret double %54
}

LLVM IR (-O2):
define double @doublet(double %0, double %1, double %2, double %3) {
%5 = fcmp olt double %1, %0
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = fcmp oge double %1, %0
%8 = tail call double @llvm.fmuladd.f64(double %2, double 5.000000e-01, double %0)
%9 = fcmp ogt double %8, %1
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %18, label %11
11: ; preds = %6
%12 = fcmp ole double %8, %1
%13 = fadd double %0, %2
%14 = fcmp ogt double %13, %1
%15 = and i1 %12, %14
%16 = fneg double %3
%17 = select i1 %15, double %16, double 0.000000e+00
br label %18
18: ; preds = %11, %6, %4
%19 = phi double [ 0.000000e+00, %4 ], [ %3, %6 ], [ %17, %11 ]
ret double %19
}