double trapezoid(double (*fp)(double), double a, double b, int N) {
  double h = (b - a) / (N - 1), total = 0.0;
#pragma omp parallel for reduction(+ : total)
  for (int m = 0; m < N; m++) {
    double x = m * h + a;
    total += fp(x);
  }
  total -= 0.5 * (fp(a) + fp(b));
  return h * total;
}


x86 assembly (clang, -O0):
<trapezoid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1190 <trapezoid+0x90>
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1145 <trapezoid+0x45>
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  -0x30(%rbp),%xmm1
movsd  0xe3c(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapezoid>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
movapd %xmm0,%xmm2
mov    %rdi,%r14
movsd  %xmm1,0x18(%rsp)
subsd  %xmm0,%xmm1
lea    -0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  %xmm0,%xmm1
test   %esi,%esi
movsd  %xmm1,0x10(%rsp)
xorpd  %xmm0,%xmm0
jle    1192 <trapezoid+0x92>
movsd  %xmm0,(%rsp)
xorpd  %xmm0,%xmm0
movsd  %xmm2,0x20(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm0,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  %xmm2,%xmm0
call   *%r14
movsd  0x20(%rsp),%xmm2
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
movsd  %xmm1,(%rsp)
addsd  0xe75(%rip),%xmm0
add    $0xffffffff,%ebx
jne    1150 <trapezoid+0x50>
jmp    1197 <trapezoid+0x97>
movsd  %xmm0,(%rsp)
movapd %xmm2,%xmm0
call   *%r14
movsd  %xmm0,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
call   *%r14
addsd  0x8(%rsp),%xmm0
mulsd  0xe4d(%rip),%xmm0
addsd  (%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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