double function_triangle(long long cnt, double peak, double time_peak) {
  double res;
  double cnt_peak = time_peak / 4.0 * 10000.0;
  if (cnt < cnt_peak)
    res = peak / cnt_peak * (double)cnt;
  else if (cnt < (cnt_peak * 2))
    res = -peak / cnt_peak * (double)cnt + 2 * peak;
  else
    res = 0;
  return res;
}


x86 assembly (clang, -O0):
<function_triangle>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xee9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xed5(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
cvtsi2sdq -0x8(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1167 <function_triangle+0x67>
movsd  -0x10(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
cvtsi2sdq -0x8(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11d7 <function_triangle+0xd7>
cvtsi2sdq -0x8(%rbp),%xmm1
movsd  0xe9b(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ca <function_triangle+0xca>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
divsd  -0x28(%rbp),%xmm0
cvtsi2sdq -0x8(%rbp),%xmm2
movsd  0xe5d(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11d2 <function_triangle+0xd2>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11d7 <function_triangle+0xd7>
movsd  -0x20(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<function_triangle>:
mulsd  0xef8(%rip),%xmm1
mulsd  0xef8(%rip),%xmm1
cvtsi2sd %rdi,%xmm3
ucomisd %xmm3,%xmm1
jbe    1129 <function_triangle+0x29>
divsd  %xmm1,%xmm0
mulsd  %xmm3,%xmm0
movapd %xmm0,%xmm2
jmp    1150 <function_triangle+0x50>
movapd %xmm1,%xmm4
addsd  %xmm1,%xmm4
xorpd  %xmm2,%xmm2
ucomisd %xmm3,%xmm4
jbe    1150 <function_triangle+0x50>
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
addsd  %xmm0,%xmm0
mulsd  %xmm3,%xmm2
subsd  %xmm2,%xmm0
ret
movapd %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @function_triangle(i64 %0, double %1, double %2) {
%4 = alloca i64
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store i64 %0, i64* %4
store double %1, double* %5
store double %2, double* %6
%9 = load double, double* %6
%10 = fdiv double %9, 4.000000e+00
%11 = fmul double %10, 1.000000e+04
store double %11, double* %8
%12 = load i64, i64* %4
%13 = sitofp i64 %12 to double
%14 = load double, double* %8
%15 = fcmp olt double %13, %14
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = load double, double* %5
%18 = load double, double* %8
%19 = fdiv double %17, %18
%20 = load i64, i64* %4
%21 = sitofp i64 %20 to double
%22 = fmul double %19, %21
store double %22, double* %7
br label %41
23: ; preds = %3
%24 = load i64, i64* %4
%25 = sitofp i64 %24 to double
%26 = load double, double* %8
%27 = fmul double %26, 2.000000e+00
%28 = fcmp olt double %25, %27
br i1 %28, label %29, label %39
29: ; preds = %23
%30 = load double, double* %5
%31 = fneg double %30
%32 = load double, double* %8
%33 = fdiv double %31, %32
%34 = load i64, i64* %4
%35 = sitofp i64 %34 to double
%36 = load double, double* %5
%37 = fmul double 2.000000e+00, %36
%38 = call double @llvm.fmuladd.f64(double %33, double %35, double %37)
store double %38, double* %7
br label %40
39: ; preds = %23
store double 0.000000e+00, double* %7
br label %40
40: ; preds = %39, %29
br label %41
41: ; preds = %40, %16
%42 = load double, double* %7
ret double %42
}

LLVM IR (-O2):
define double @function_triangle(i64 %0, double %1, double %2) {
%4 = fmul double %2, 2.500000e-01
%5 = fmul double %4, 1.000000e+04
%6 = sitofp i64 %0 to double
%7 = fcmp ogt double %5, %6
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = fdiv double %1, %5
%10 = fmul double %9, %6
br label %19
11: ; preds = %3
%12 = fmul double %5, 2.000000e+00
%13 = fcmp ogt double %12, %6
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = fneg double %1
%16 = fdiv double %15, %5
%17 = fmul double %1, 2.000000e+00
%18 = tail call double @llvm.fmuladd.f64(double %16, double %6, double %17)
br label %19
19: ; preds = %11, %14, %8
%20 = phi double [ %10, %8 ], [ %18, %14 ], [ 0.000000e+00, %11 ]
ret double %20
}