double qtrap(double (*func)(double, void *), double a, double b, int n,
             void *params) {
  int i;
  double x;
  double h;
  double sum;
  sum = 0.0;
  h = (b - a) / (double)(n - 1);
  for (i = 1; i < n - 1; i++) {
    x = a + i * h;
    sum += func(x, params);
  }
  sum += ((func(a, params) + func(b, params)) * 0.5);
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<qtrap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %rdx,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    119d <qtrap+0x9d>
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1149 <qtrap+0x49>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  -0x48(%rbp),%xmm1
movsd  0xe27(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qtrap>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
movapd %xmm0,%xmm2
mov    %rdi,%r15
movsd  %xmm1,0x20(%rsp)
lea    -0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
subsd  %xmm2,%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
xorpd  %xmm0,%xmm0
cmp    $0x3,%esi
jl     11af <qtrap+0xaf>
add    $0xfffffffe,%ebx
movsd  0xeb9(%rip),%xmm1
xorpd  %xmm0,%xmm0
movsd  %xmm2,0x28(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,%xmm0
mulsd  0x18(%rsp),%xmm0
addsd  %xmm2,%xmm0
mov    %r14,%rdi
call   *%r15
movsd  0x28(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
addsd  0xe56(%rip),%xmm1
add    $0xffffffff,%ebx
jne    1160 <qtrap+0x60>
movsd  %xmm0,0x8(%rsp)
movapd %xmm2,%xmm0
mov    %r14,%rdi
call   *%r15
movsd  %xmm0,0x10(%rsp)
movsd  0x20(%rsp),%xmm0
mov    %r14,%rdi
call   *%r15
addsd  0x10(%rsp),%xmm0
mulsd  0xe29(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
mulsd  0x18(%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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