double trap(double a, double b, int n, double (*f)(double)) {
  int i;
  double area = f(a) + f(b), h = (b - a) / n;
  for (i = 1; i < n; i++) {
    double xi = a + i * h;
    area += 2 * f(xi);
  }
  return area *= h / 2;
}


x86 assembly (clang, -O0):
<trap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%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
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b7 <trap+0xb7>
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x24(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
movsd  -0x30(%rbp),%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1164 <trap+0x64>
movsd  -0x38(%rbp),%xmm0
movsd  0xe3c(%rip),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trap>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x18(%rsp)
call   *%rsi
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%rbx
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
movapd %xmm0,%xmm3
addsd  0x10(%rsp),%xmm3
subsd  %xmm2,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
divsd  %xmm0,%xmm1
cmp    $0x2,%ebp
jl     11b9 <trap+0xb9>
add    $0xffffffff,%ebp
movsd  0xea4(%rip),%xmm0
movsd  %xmm1,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  %xmm0,0x10(%rsp)
movsd  %xmm3,0x20(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
call   *%rbx
movsd  0x20(%rsp),%xmm3
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm0,%xmm3
movsd  0x10(%rsp),%xmm0
addsd  0xe4c(%rip),%xmm0
add    $0xffffffff,%ebp
jne    1170 <trap+0x70>
mulsd  0xe47(%rip),%xmm1
mulsd  %xmm3,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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