double trapzd(double (*func)(double), double a, double b, int n) {
  double del;
  double sum;
  double tnm;
  double x;
  int j;
  static double s;
  static int it;
  if (n <= 0) {
    s = 0.5 * (b - a) * ((*func)(a) + (*func)(b));
    it = 1;
  } else {
    tnm = it;
    del = (b - a) / tnm;
    x = a + 0.5 * del;
    sum = 0;
    for (j = 0; j < it; j++) {
      sum += (*func)(x);
      x += del;
    }
    s = 0.5 * (s + (b - a) * sum / tnm);
    it *= 2;
  }
  return (s);
}


x86 assembly (clang, -O0):
<trapzd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1185 <trapzd+0x85>
movsd  -0x18(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movsd  0xecb(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x58(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  -0x58(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x50(%rbp),%xmm0
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2eb2(%rip)
movl   $0x1,0x2eb0(%rip)
jmp    1258 <trapzd+0x158>
cvtsi2sdl 0x2ea3(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  0xe48(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    0x2e53(%rip),%eax
jge    1215 <trapzd+0x115>
mov    -0x8(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11d4 <trapzd+0xd4>
movsd  0x2e0b(%rip),%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
addsd  %xmm0,%xmm1
movsd  0xdc3(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2ddf(%rip)
mov    0x2de1(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2dd8(%rip)
movsd  0x2dc8(%rip),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapzd>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
test   %esi,%esi
jle    1187 <trapzd+0x87>
mov    0x2f1d(%rip),%eax
cvtsi2sd %eax,%xmm2
subsd  %xmm0,%xmm1
test   %eax,%eax
movsd  %xmm1,(%rsp)
movsd  %xmm2,0x18(%rsp)
jle    11cc <trapzd+0xcc>
divsd  %xmm2,%xmm1
movsd  0xec6(%rip),%xmm2
movsd  %xmm1,0x20(%rsp)
mulsd  %xmm1,%xmm2
addsd  %xmm0,%xmm2
xorpd  %xmm1,%xmm1
xor    %ebp,%ebp
xchg   %ax,%ax
movsd  %xmm2,0x8(%rsp)
movsd  %xmm1,0x10(%rsp)
movapd %xmm2,%xmm0
call   *%rbx
movsd  0x8(%rsp),%xmm2
movsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
addsd  0x20(%rsp),%xmm2
add    $0x1,%ebp
mov    0x2eaf(%rip),%eax
cmp    %eax,%ebp
jl     1150 <trapzd+0x50>
jmp    11d0 <trapzd+0xd0>
movapd %xmm1,%xmm2
subsd  %xmm0,%xmm2
mulsd  0xe69(%rip),%xmm2
movsd  %xmm2,0x10(%rsp)
movsd  %xmm1,(%rsp)
call   *%rbx
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   *%rbx
addsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x2e63(%rip)
mov    $0x1,%eax
jmp    11f9 <trapzd+0xf9>
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm0
mulsd  %xmm1,%xmm0
divsd  0x18(%rsp),%xmm0
addsd  0x2e41(%rip),%xmm0
mulsd  0xe11(%rip),%xmm0
movsd  %xmm0,0x2e31(%rip)
add    %eax,%eax
mov    %eax,0x2e31(%rip)
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@trapzd.s = internal global double 0.000000e+00
@trapzd.it = internal global i32 0
define double @trapzd(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 double
%12 = alloca double
%13 = alloca i32
store double (double)* %0, double (double)** %5
store double %1, double* %6
store double %2, double* %7
store i32 %3, i32* %8
%14 = load i32, i32* %8
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %4
%17 = load double, double* %7
%18 = load double, double* %6
%19 = fsub double %17, %18
%20 = fmul double 5.000000e-01, %19
%21 = load double (double)*, double (double)** %5
%22 = load double, double* %6
%23 = call double %21(double %22)
%24 = load double (double)*, double (double)** %5
%25 = load double, double* %7
%26 = call double %24(double %25)
%27 = fadd double %23, %26
%28 = fmul double %20, %27
store double %28, double* @trapzd.s
store i32 1, i32* @trapzd.it
br label %69
29: ; preds = %4
%30 = load i32, i32* @trapzd.it
%31 = sitofp i32 %30 to double
store double %31, double* %11
%32 = load double, double* %7
%33 = load double, double* %6
%34 = fsub double %32, %33
%35 = load double, double* %11
%36 = fdiv double %34, %35
store double %36, double* %9
%37 = load double, double* %6
%38 = load double, double* %9
%39 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %38, double %37)
store double %39, double* %12
store double 0.000000e+00, double* %10
store i32 0, i32* %13
br label %40
40: ; preds = %53, %29
%41 = load i32, i32* %13
%42 = load i32, i32* @trapzd.it
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %56
44: ; preds = %40
%45 = load double (double)*, double (double)** %5
%46 = load double, double* %12
%47 = call double %45(double %46)
%48 = load double, double* %10
%49 = fadd double %48, %47
store double %49, double* %10
%50 = load double, double* %9
%51 = load double, double* %12
%52 = fadd double %51, %50
store double %52, double* %12
br label %53
53: ; preds = %44
%54 = load i32, i32* %13
%55 = add nsw i32 %54, 1
store i32 %55, i32* %13
br label %40
56: ; preds = %40
%57 = load double, double* @trapzd.s
%58 = load double, double* %7
%59 = load double, double* %6
%60 = fsub double %58, %59
%61 = load double, double* %10
%62 = fmul double %60, %61
%63 = load double, double* %11
%64 = fdiv double %62, %63
%65 = fadd double %57, %64
%66 = fmul double 5.000000e-01, %65
store double %66, double* @trapzd.s
%67 = load i32, i32* @trapzd.it
%68 = mul nsw i32 %67, 2
store i32 %68, i32* @trapzd.it
br label %69
69: ; preds = %56, %16
%70 = load double, double* @trapzd.s
ret double %70
}

LLVM IR (-O2):
@trapzd.s = internal unnamed_addr global double 0.000000e+00
@trapzd.it = internal unnamed_addr global i32 0
define double @trapzd(double (double)* nocapture %0, double %1, double %2, i32 %3) {
%5 = icmp slt i32 %3, 1
br i1 %5, label %6, label %13
6: ; preds = %4
%7 = fsub double %2, %1
%8 = fmul double %7, 5.000000e-01
%9 = tail call double %0(double %1)
%10 = tail call double %0(double %2)
%11 = fadd double %9, %10
%12 = fmul double %8, %11
store double %12, double* @trapzd.s
br label %40
13: ; preds = %4
%14 = load i32, i32* @trapzd.it
%15 = sitofp i32 %14 to double
%16 = fsub double %2, %1
%17 = fdiv double %16, %15
%18 = icmp sgt i32 %14, 0
br i1 %18, label %19, label %31
19: ; preds = %13
%20 = tail call double @llvm.fmuladd.f64(double %17, double 5.000000e-01, double %1)
br label %21
21: ; preds = %19, %21
%22 = phi i32 [ %28, %21 ], [ 0, %19 ]
%23 = phi double [ %27, %21 ], [ %20, %19 ]
%24 = phi double [ %26, %21 ], [ 0.000000e+00, %19 ]
%25 = tail call double %0(double %23)
%26 = fadd double %24, %25
%27 = fadd double %17, %23
%28 = add nuw nsw i32 %22, 1
%29 = load i32, i32* @trapzd.it
%30 = icmp slt i32 %28, %29
br i1 %30, label %21, label %31
31: ; preds = %21, %13
%32 = phi double [ 0.000000e+00, %13 ], [ %26, %21 ]
%33 = phi i32 [ %14, %13 ], [ %29, %21 ]
%34 = load double, double* @trapzd.s
%35 = fmul double %16, %32
%36 = fdiv double %35, %15
%37 = fadd double %36, %34
%38 = fmul double %37, 5.000000e-01
store double %38, double* @trapzd.s
%39 = shl nsw i32 %33, 1
br label %40
40: ; preds = %31, %6
%41 = phi double [ %38, %31 ], [ %12, %6 ]
%42 = phi i32 [ %39, %31 ], [ 1, %6 ]
store i32 %42, i32* @trapzd.it
ret double %41
}