double integrate(double (*func)(double), double a, double b, int n) {
  double h = (b - a) / n;
  double sum = 0.0;
  for (int i = 0; i < n; i++)
    sum += func(a + h * (i + 0.5));
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<integrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118d <integrate+0x8d>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm2
movsd  0xe99(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1140 <integrate+0x40>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movapd %xmm0,%xmm2
subsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
divsd  %xmm0,%xmm1
test   %esi,%esi
jle    119a <integrate+0x9a>
mov    %esi,%ebx
mov    %rdi,%r14
xorpd  %xmm3,%xmm3
xorpd  %xmm0,%xmm0
movsd  %xmm1,0x18(%rsp)
movsd  %xmm2,0x10(%rsp)
nopw   0x0(%rax,%rax,1)
movsd  %xmm3,0x20(%rsp)
movsd  %xmm0,0x8(%rsp)
movapd %xmm3,%xmm0
addsd  0xea8(%rip),%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
call   *%r14
movsd  0x10(%rsp),%xmm2
movsd  0x18(%rsp),%xmm1
movsd  0x8(%rsp),%xmm3
addsd  %xmm0,%xmm3
movsd  %xmm3,0x8(%rsp)
movsd  0x20(%rsp),%xmm3
movsd  0x8(%rsp),%xmm0
addsd  0xe75(%rip),%xmm3
add    $0xffffffff,%ebx
jne    1140 <integrate+0x40>
jmp    119e <integrate+0x9e>
xorpd  %xmm0,%xmm0
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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