double int_trapezium(double from, double to, int n, double (*func)()) {
  double h = (to - from) / n;
  double sum = func(from) + func(to);
  int i;
  for (i = 1; i < n; i++)
    sum += 2.0 * func(from + i * h);
  return h * sum / 2.0;
}


x86 assembly (clang, -O0):
<int_trapezium>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   *%rcx
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rcx
movsd  -0x10(%rbp),%xmm0
mov    $0x1,%al
call   *%rcx
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b3 <int_trapezium+0xb3>
mov    -0x20(%rbp),%rcx
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    $0x1,%al
call   *%rcx
movsd  -0x30(%rbp),%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1168 <int_trapezium+0x68>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  0xe3b(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_trapezium>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm3
subsd  %xmm0,%xmm3
cvtsi2sd %edi,%xmm2
divsd  %xmm2,%xmm3
movsd  %xmm3,0x18(%rsp)
movsd  %xmm0,0x20(%rsp)
mov    $0x1,%al
call   *%rsi
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   *%rbx
addsd  0x10(%rsp),%xmm0
cmp    $0x2,%ebp
jl     11ad <int_trapezium+0xad>
add    $0xffffffff,%ebp
movsd  0xea9(%rip),%xmm1
nopw   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,%xmm0
mulsd  0x18(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
mov    $0x1,%al
call   *%rbx
addsd  %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
addsd  0xe58(%rip),%xmm1
add    $0xffffffff,%ebp
jne    1160 <int_trapezium+0x60>
movsd  0x18(%rsp),%xmm1
mulsd  %xmm0,%xmm1
mulsd  0xe49(%rip),%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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