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


x86 assembly (clang, -O0):
<trapzd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    118e <trapzd+0x8e>
movl   $0x1,0x2f03(%rip)
movsd  -0x20(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movsd  0xec1(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rcx
movsd  -0x18(%rbp),%xmm0
mov    $0x1,%al
call   *%rcx
movsd  %xmm0,-0x60(%rbp)
mov    -0x10(%rbp),%rcx
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%al
call   *%rcx
movsd  -0x60(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x58(%rbp),%xmm0
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea4(%rip)
movsd  %xmm0,-0x8(%rbp)
jmp    1270 <trapzd+0x170>
cvtsi2sdl 0x2e9a(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm0
movsd  0xe3f(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    0x2e4a(%rip),%eax
jg     1220 <trapzd+0x120>
mov    -0x10(%rbp),%rcx
movsd  -0x30(%rbp),%xmm0
mov    $0x1,%al
call   *%rcx
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
movsd  -0x48(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11dd <trapzd+0xdd>
mov    0x2e0a(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2e01(%rip)
movsd  0x2df1(%rip),%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
addsd  %xmm0,%xmm1
movsd  0xda9(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2dc5(%rip)
movsd  0x2dbd(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapzd>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
cmp    $0x1,%esi
jne    1157 <trapzd+0x57>
movl   $0x1,0x2f18(%rip)
movapd %xmm1,%xmm2
subsd  %xmm0,%xmm2
mulsd  0xed8(%rip),%xmm2
movsd  %xmm2,0x10(%rsp)
mov    $0x1,%al
movsd  %xmm1,(%rsp)
call   *%rbx
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   *%rbx
addsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
jmp    1204 <trapzd+0x104>
mov    0x2ed3(%rip),%eax
cvtsi2sd %eax,%xmm2
subsd  %xmm0,%xmm1
test   %eax,%eax
movsd  %xmm1,(%rsp)
movsd  %xmm2,0x18(%rsp)
jle    11d9 <trapzd+0xd9>
divsd  %xmm2,%xmm1
movsd  0xe80(%rip),%xmm2
movsd  %xmm1,0x20(%rsp)
mulsd  %xmm1,%xmm2
addsd  %xmm0,%xmm2
xorpd  %xmm1,%xmm1
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm2,0x8(%rsp)
movsd  %xmm1,0x10(%rsp)
movapd %xmm2,%xmm0
mov    $0x1,%al
call   *%rbx
movsd  0x8(%rsp),%xmm2
movsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
addsd  0x20(%rsp),%xmm2
mov    0x2e60(%rip),%eax
add    $0x1,%ebp
cmp    %eax,%ebp
jl     11a0 <trapzd+0xa0>
jmp    11dd <trapzd+0xdd>
xorpd  %xmm1,%xmm1
add    %eax,%eax
mov    %eax,0x2e4b(%rip)
movsd  (%rsp),%xmm0
mulsd  %xmm1,%xmm0
divsd  0x18(%rsp),%xmm0
addsd  0x2e2c(%rip),%xmm0
mulsd  0xdfc(%rip),%xmm0
movsd  %xmm0,0x2e1c(%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 (...)* %0, double %1, double %2, i32 %3) {
%5 = alloca double
%6 = alloca double (...)*
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca i32
store double (...)* %0, double (...)** %6
store double %1, double* %7
store double %2, double* %8
store i32 %3, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %32
17: ; preds = %4
store i32 1, i32* @trapzd.it
%18 = load double, double* %8
%19 = load double, double* %7
%20 = fsub double %18, %19
%21 = fmul double 5.000000e-01, %20
%22 = load double (...)*, double (...)** %6
%23 = load double, double* %7
%24 = bitcast double (...)* %22 to double (double, ...)*
%25 = call double (double, ...) %24(double %23)
%26 = load double (...)*, double (...)** %6
%27 = load double, double* %8
%28 = bitcast double (...)* %26 to double (double, ...)*
%29 = call double (double, ...) %28(double %27)
%30 = fadd double %25, %29
%31 = fmul double %21, %30
store double %31, double* @trapzd.s
store double %31, double* %5
br label %74
32: ; preds = %4
%33 = load i32, i32* @trapzd.it
%34 = sitofp i32 %33 to double
store double %34, double* %11
%35 = load double, double* %8
%36 = load double, double* %7
%37 = fsub double %35, %36
%38 = load double, double* %11
%39 = fdiv double %37, %38
store double %39, double* %13
%40 = load double, double* %7
%41 = load double, double* %13
%42 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %41, double %40)
store double %42, double* %10
store double 0.000000e+00, double* %12
store i32 1, i32* %14
br label %43
43: ; preds = %54, %32
%44 = load i32, i32* %14
%45 = load i32, i32* @trapzd.it
%46 = icmp sle i32 %44, %45
br i1 %46, label %47, label %60
47: ; preds = %43
%48 = load double (...)*, double (...)** %6
%49 = load double, double* %10
%50 = bitcast double (...)* %48 to double (double, ...)*
%51 = call double (double, ...) %50(double %49)
%52 = load double, double* %12
%53 = fadd double %52, %51
store double %53, double* %12
br label %54
54: ; preds = %47
%55 = load i32, i32* %14
%56 = add nsw i32 %55, 1
store i32 %56, i32* %14
%57 = load double, double* %13
%58 = load double, double* %10
%59 = fadd double %58, %57
store double %59, double* %10
br label %43
60: ; preds = %43
%61 = load i32, i32* @trapzd.it
%62 = mul nsw i32 %61, 2
store i32 %62, i32* @trapzd.it
%63 = load double, double* @trapzd.s
%64 = load double, double* %8
%65 = load double, double* %7
%66 = fsub double %64, %65
%67 = load double, double* %12
%68 = fmul double %66, %67
%69 = load double, double* %11
%70 = fdiv double %68, %69
%71 = fadd double %63, %70
%72 = fmul double 5.000000e-01, %71
store double %72, double* @trapzd.s
%73 = load double, double* @trapzd.s
store double %73, double* %5
br label %74
74: ; preds = %60, %17
%75 = load double, double* %5
ret double %75
}

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