double trap(double a, double b, int n, double (*f)(double)) {
  double T = 0, sum = 0, h = (b - a) / n;
  int i = 1;
  for (; i <= n - 1; ++i) {
    sum += f(a + i * h);
  }
  T = h / 2 * (f(a) + f(b) + 2 * sum);
  return T;
}


x86 assembly (clang, -O0):
<trap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     118e <trap+0x8e>
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x3c(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1148 <trap+0x48>
movsd  -0x38(%rbp),%xmm0
movsd  0xe65(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x50(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  -0x50(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
addsd  %xmm1,%xmm2
movsd  -0x30(%rbp),%xmm1
addsd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trap>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
movapd %xmm0,%xmm2
movsd  %xmm1,0x18(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %edi,%xmm0
subsd  %xmm2,%xmm1
divsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
cmp    $0x2,%edi
jl     119b <trap+0x9b>
mov    %edi,%ebp
add    $0xffffffff,%ebp
xorpd  %xmm0,%xmm0
movsd  0xec4(%rip),%xmm3
movsd  %xmm1,0x8(%rsp)
movsd  %xmm2,0x20(%rsp)
nopl   0x0(%rax,%rax,1)
movsd  %xmm3,0x10(%rsp)
movsd  %xmm0,(%rsp)
movapd %xmm3,%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
call   *%rbx
movsd  0x10(%rsp),%xmm3
movsd  0x8(%rsp),%xmm1
movsd  (%rsp),%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,(%rsp)
movsd  0x20(%rsp),%xmm2
movsd  (%rsp),%xmm0
addsd  0xe6a(%rip),%xmm3
add    $0xffffffff,%ebp
jne    1150 <trap+0x50>
movsd  %xmm0,(%rsp)
mulsd  0xe60(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
movapd %xmm2,%xmm0
call   *%rbx
movsd  %xmm0,0x10(%rsp)
movsd  0x18(%rsp),%xmm0
call   *%rbx
addsd  0x10(%rsp),%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm1,%xmm1
addsd  %xmm1,%xmm0
mulsd  0x8(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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