double simpson(double a, double b, int n, double (*func)(double)) {
  double simpson_sum;
  double fa, fb, x, step, fac;
  int j;
  step = (b - a) / ((double)n);
  fa = (*func)(a);
  fb = (*func)(b);
  simpson_sum = fa;
  fac = 2.;
  for (j = 1; j <= n - 1; j++) {
    if (fac == 2.) {
      fac = 4.;
    } else {
      fac = 2.;
    }
    x = j * step + a;
    simpson_sum += (*func)(x)*fac;
  }
  simpson_sum = (simpson_sum + fb) * step / 3.;
  return simpson_sum;
}


x86 assembly (clang, -O0):
<simpson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe9d(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     1208 <simpson+0x108>
movsd  -0x50(%rbp),%xmm0
movsd  0xe73(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11af <simpson+0xaf>
jp     11af <simpson+0xaf>
movsd  0xe6b(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    11bc <simpson+0xbc>
movsd  0xe49(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdl -0x54(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
movsd  -0x50(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    116f <simpson+0x6f>
movsd  -0x28(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  0xde9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simpson>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm3
subsd  %xmm0,%xmm3
cvtsi2sd %edi,%xmm2
divsd  %xmm2,%xmm3
movsd  %xmm3,0x10(%rsp)
movsd  %xmm0,0x20(%rsp)
call   *%rsi
movsd  %xmm0,(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%rbx
movsd  %xmm0,0x18(%rsp)
cmp    $0x2,%ebp
jl     11dc <simpson+0xdc>
add    $0xffffffff,%ebp
movsd  0xea8(%rip),%xmm2
movsd  0xea8(%rip),%xmm1
lea    0xeb1(%rip),%r14
movsd  (%rsp),%xmm0
nopl   0x0(%rax)
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,(%rsp)
cmpeqsd 0xe7c(%rip),%xmm2
movq   %xmm2,%rax
and    $0x1,%eax
movsd  (%r14,%rax,8),%xmm0
movsd  %xmm0,0x28(%rsp)
movapd %xmm1,%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
call   *%rbx
movsd  0x28(%rsp),%xmm2
mulsd  %xmm2,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x8(%rsp),%xmm1
movsd  (%rsp),%xmm0
addsd  0xe33(%rip),%xmm1
add    $0xffffffff,%ebp
jne    1170 <simpson+0x70>
jmp    11e1 <simpson+0xe1>
movsd  (%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x10(%rsp),%xmm0
mulsd  %xmm1,%xmm0
divsd  0xe13(%rip),%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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