double integral_trapezoid(double a, double b, double step,
                          double (*f)(double)) {
  double fragment = (b - a) / step;
  double res = step * (f(a) + f(b)) / 2.0;
  int i;
  for (i = 1; i <= fragment - 1; ++i)
    res = res + step * f(a + step * i);
  return (res);
}


x86 assembly (clang, -O0):
<integral_trapezoid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  -0x48(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x40(%rbp),%xmm0
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe8f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  0xe75(%rip),%xmm2
subsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     11fa <integral_trapezoid+0xfa>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
movsd  -0x58(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1181 <integral_trapezoid+0x81>
movsd  -0x30(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integral_trapezoid>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movsd  %xmm2,0x10(%rsp)
movsd  %xmm1,0x8(%rsp)
subsd  %xmm0,%xmm1
divsd  %xmm2,%xmm1
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x20(%rsp)
call   *%rdi
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%rbx
movsd  (%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
movapd %xmm0,%xmm3
addsd  0x18(%rsp),%xmm3
mulsd  %xmm2,%xmm3
mulsd  0xea7(%rip),%xmm3
addsd  0xea7(%rip),%xmm1
ucomisd 0xea7(%rip),%xmm1
jb     11bf <integral_trapezoid+0xbf>
movsd  0xe9d(%rip),%xmm0
mov    $0x2,%ebp
movsd  %xmm1,(%rsp)
nopl   (%rax)
movsd  %xmm3,0x8(%rsp)
mulsd  %xmm2,%xmm0
addsd  0x20(%rsp),%xmm0
call   *%rbx
movsd  0x8(%rsp),%xmm3
movsd  0x10(%rsp),%xmm2
movapd %xmm0,%xmm1
mulsd  %xmm2,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
addsd  %xmm1,%xmm3
movsd  (%rsp),%xmm1
add    $0x1,%ebp
ucomisd %xmm0,%xmm1
jae    1180 <integral_trapezoid+0x80>
movapd %xmm3,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define double @integral_trapezoid(double %0, double %1, double %2, double (double)* nocapture %3) {
%5 = fsub double %1, %0
%6 = fdiv double %5, %2
%7 = tail call double %3(double %0)
%8 = tail call double %3(double %1)
%9 = fadd double %7, %8
%10 = fmul double %9, %2
%11 = fmul double %10, 5.000000e-01
%12 = fadd double %6, -1.000000e+00
%13 = fcmp ult double %12, 1.000000e+00
br i1 %13, label %24, label %14
14: ; preds = %4, %14
%15 = phi double [ %22, %14 ], [ 1.000000e+00, %4 ]
%16 = phi i32 [ %21, %14 ], [ 1, %4 ]
%17 = phi double [ %20, %14 ], [ %11, %4 ]
%18 = tail call double @llvm.fmuladd.f64(double %2, double %15, double %0)
%19 = tail call double %3(double %18)
%20 = tail call double @llvm.fmuladd.f64(double %2, double %19, double %17)
%21 = add nuw nsw i32 %16, 1
%22 = sitofp i32 %21 to double
%23 = fcmp ult double %12, %22
br i1 %23, label %24, label %14
24: ; preds = %14, %4
%25 = phi double [ %11, %4 ], [ %20, %14 ]
ret double %25
}