double trapezoidal_rule(double a, double b, int n, double (*func)(double)) {
  double trapez_sum;
  double fa, fb, x, step;
  int j;
  step = (b - a) / ((double)n);
  fa = (*func)(a) / 2.;
  fb = (*func)(b) / 2.;
  trapez_sum = 0.;
  for (j = 1; j <= n - 1; j++) {
    x = j * step + a;
    trapez_sum += (*func)(x);
  }
  trapez_sum = (trapez_sum + fb + fa) * step;
  return trapez_sum;
}


x86 assembly (clang, -O0):
<trapezoidal_rule>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  0xebc(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  0xea0(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     11c8 <trapezoidal_rule+0xc8>
cvtsi2sdl -0x4c(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1178 <trapezoidal_rule+0x78>
movsd  -0x28(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapezoidal_rule>:
push   %rbp
push   %rbx
sub    $0x38,%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,0x10(%rsp)
movsd  %xmm0,0x28(%rsp)
call   *%rsi
movsd  %xmm0,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%rbx
movsd  %xmm0,0x18(%rsp)
xorpd  %xmm1,%xmm1
cmp    $0x2,%ebp
jl     119d <trapezoidal_rule+0x9d>
add    $0xffffffff,%ebp
xorpd  %xmm1,%xmm1
movsd  0xea5(%rip),%xmm0
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
movsd  %xmm1,0x30(%rsp)
movsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0x28(%rsp),%xmm0
call   *%rbx
movsd  0x30(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
addsd  0xe68(%rip),%xmm0
add    $0xffffffff,%ebp
jne    1160 <trapezoidal_rule+0x60>
movsd  0xe63(%rip),%xmm0
movsd  0x18(%rsp),%xmm2
mulsd  %xmm0,%xmm2
addsd  %xmm1,%xmm2
movsd  0x20(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm2,%xmm1
movsd  0x10(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

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