double integrate(double (*function)(double), double start, double end,
                 double stepsize) {
  int n = (end - start) / stepsize;
  double sum = 0.0;
  int i;
  for (i = 0; i < n; i++) {
    double x = start + i * stepsize;
    sum += (*function)(x);
  }
  return sum * stepsize;
}


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)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    118b <integrate+0x8b>
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x40(%rbp),%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  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
subsd  %xmm0,%xmm1
divsd  %xmm2,%xmm1
cvttsd2si %xmm1,%ebp
test   %ebp,%ebp
jle    1191 <integrate+0x91>
movapd %xmm0,%xmm3
mov    %rdi,%rbx
xorpd  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
movsd  %xmm2,0x18(%rsp)
movsd  %xmm3,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x20(%rsp)
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm3,%xmm0
call   *%rbx
movsd  0x10(%rsp),%xmm3
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x20(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
addsd  0xe76(%rip),%xmm1
add    $0xffffffff,%ebp
jne    1140 <integrate+0x40>
jmp    1195 <integrate+0x95>
xorpd  %xmm0,%xmm0
mulsd  %xmm2,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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