double trapizoidalRule(double (*f)(double), double a, double b, int n) {
  int i;
  double sum = 0, h, integral, x;
  sum = (f(a) + f(b)) / 2;
  h = (b - a) / (double)n;
  for (i = 1; i < n; i++) {
    x = a + (i * h);
    sum += f(x);
  }
  integral = h * sum;
  return integral;
}


x86 assembly (clang, -O0):
<trapizoidalRule>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  0xeb0(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c3 <trapizoidalRule+0xc3>
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x20(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1178 <trapizoidalRule+0x78>
movsd  -0x30(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapizoidalRule>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x18(%rsp)
mov    %rdi,%r14
call   *%rdi
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%r14
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
movapd %xmm0,%xmm3
addsd  0x10(%rsp),%xmm3
mulsd  0xeb9(%rip),%xmm3
subsd  %xmm2,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
divsd  %xmm0,%xmm1
cmp    $0x2,%ebx
jl     11b6 <trapizoidalRule+0xb6>
add    $0xffffffff,%ebx
movsd  0xea2(%rip),%xmm0
movsd  %xmm1,0x8(%rsp)
nopl   0x0(%rax)
movsd  %xmm0,0x10(%rsp)
movsd  %xmm3,0x20(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
call   *%r14
movsd  0x20(%rsp),%xmm3
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm3
movsd  0x10(%rsp),%xmm0
addsd  0xe57(%rip),%xmm0
add    $0xffffffff,%ebx
jne    1170 <trapizoidalRule+0x70>
mulsd  %xmm3,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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