double trap(double (*f)(double, void *), double xa, double xb, int N,
            void *args) {
  double dx = (xb - xa) / N;
  double I = 0.5 * (f(xa, args) + f(xb, args));
  int i;
  for (i = 1; i < N; i++)
    I += f(xa + i * dx, args);
  return I * dx;
}


x86 assembly (clang, -O0):
<trap>:
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)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
movsd  -0x48(%rbp),%xmm1
addsd  %xmm0,%xmm1
movsd  0xe97(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11be <trap+0xbe>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x3c(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x28(%rbp),%rdi
call   *%rax
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1179 <trap+0x79>
movsd  -0x38(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trap>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
movsd  %xmm1,(%rsp)
mov    %rdi,%rbx
movapd %xmm1,%xmm3
subsd  %xmm0,%xmm3
cvtsi2sd %esi,%xmm2
divsd  %xmm2,%xmm3
movsd  %xmm3,0x10(%rsp)
movsd  %xmm0,0x18(%rsp)
mov    %rdx,%rdi
call   *%rbx
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
mov    %r14,%rdi
call   *%rbx
addsd  0x8(%rsp),%xmm0
mulsd  0xeac(%rip),%xmm0
cmp    $0x2,%ebp
jl     11b6 <trap+0xb6>
add    $0xffffffff,%ebp
movsd  0xea4(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,(%rsp)
movapd %xmm1,%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0x18(%rsp),%xmm0
mov    %r14,%rdi
call   *%rbx
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x8(%rsp),%xmm1
movsd  (%rsp),%xmm0
addsd  0xe57(%rip),%xmm1
add    $0xffffffff,%ebp
jne    1170 <trap+0x70>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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