double integrate(double a, double b, double (*f)(double), long nstep) {
  double x, sum = 0.0, dx = (b - a) / (double)nstep;
  for (x = a + 0.5 * dx; (b - x) * (x - a) > 0.0; x += dx)
    sum += f(x);
  return sum / (double)nstep;
}


x86 assembly (clang, -O0):
<integrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdq -0x20(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  0xeb3(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
subsd  -0x8(%rbp),%xmm1
mulsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11a8 <integrate+0xa8>
mov    -0x18(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x38(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    115a <integrate+0x5a>
movsd  -0x30(%rbp),%xmm0
cvtsi2sdq -0x20(%rbp),%xmm1
divsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate>:
push   %rbx
sub    $0x30,%rsp
movapd %xmm0,%xmm3
movapd %xmm1,%xmm4
subsd  %xmm0,%xmm4
xorps  %xmm0,%xmm0
cvtsi2sd %rsi,%xmm0
movsd  %xmm0,(%rsp)
divsd  %xmm0,%xmm4
movsd  0xed6(%rip),%xmm5
mulsd  %xmm4,%xmm5
addsd  %xmm3,%xmm5
movapd %xmm1,%xmm0
subsd  %xmm5,%xmm0
movapd %xmm5,%xmm2
subsd  %xmm3,%xmm2
mulsd  %xmm0,%xmm2
xorpd  %xmm6,%xmm6
ucomisd %xmm6,%xmm2
jbe    11c6 <integrate+0xc6>
mov    %rdi,%rbx
xorpd  %xmm6,%xmm6
movsd  %xmm1,0x18(%rsp)
movsd  %xmm3,0x10(%rsp)
movsd  %xmm4,0x8(%rsp)
nopl   0x0(%rax)
movsd  %xmm6,0x20(%rsp)
movsd  %xmm5,0x28(%rsp)
movapd %xmm5,%xmm0
call   *%rbx
movsd  0x20(%rsp),%xmm6
movsd  0x28(%rsp),%xmm5
movsd  0x8(%rsp),%xmm4
movsd  0x10(%rsp),%xmm3
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm6
addsd  %xmm4,%xmm5
movapd %xmm1,%xmm0
subsd  %xmm5,%xmm0
movapd %xmm5,%xmm2
subsd  %xmm3,%xmm2
mulsd  %xmm0,%xmm2
ucomisd 0xe44(%rip),%xmm2
ja     1170 <integrate+0x70>
divsd  (%rsp),%xmm6
movapd %xmm6,%xmm0
add    $0x30,%rsp
pop    %rbx
ret

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

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