double simpson(double (*f)(double), double a, double b) {
  double result = 0;
  double n = 100;
  double h = (b - a) / n;
  double x = 0;
  int i = 0;
  for (; i <= n; i++) {
    x = a + i * h;
    if (i == 0 || i == n) {
      result += f(x);
      continue;
    } else if (i % 2 != 0) {
      result += 4 * f(x);
    } else {
      result += 2 * f(x);
    }
  }
  result = (h * result) / 3;
  return result;
}


x86 assembly (clang, -O0):
<simpson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xeda(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cvtsi2sdl -0x3c(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     122b <simpson+0x12b>
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x3c(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     119e <simpson+0x9e>
cvtsi2sdl -0x3c(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jne    11b8 <simpson+0xb8>
jp     11b8 <simpson+0xb8>
mov    -0x8(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    121d <simpson+0x11d>
mov    -0x3c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11f6 <simpson+0xf6>
mov    -0x8(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
movsd  -0x20(%rbp),%xmm1
movsd  0xe2c(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1213 <simpson+0x113>
mov    -0x8(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
movsd  -0x20(%rbp),%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1218 <simpson+0x118>
jmp    121d <simpson+0x11d>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    114e <simpson+0x4e>
movsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  0xdcb(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simpson>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movsd  %xmm0,0x20(%rsp)
subsd  %xmm0,%xmm1
divsd  0xee5(%rip),%xmm1
movsd  %xmm1,0x18(%rsp)
xor    %ebp,%ebp
xorpd  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
jmp    1152 <simpson+0x52>
nopl   (%rax)
call   *%rbx
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x10(%rsp),%xmm0
addsd  0xec6(%rip),%xmm0
add    $0x1,%ebp
cmp    $0x65,%ebp
je     118b <simpson+0x8b>
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x10(%rsp)
mulsd  0x18(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
cmp    $0x64,%ebp
je     1130 <simpson+0x30>
test   %ebp,%ebp
je     1130 <simpson+0x30>
call   *%rbx
test   $0x1,%bpl
jne    1181 <simpson+0x81>
addsd  %xmm0,%xmm0
jmp    1132 <simpson+0x32>
mulsd  0xe7f(%rip),%xmm0
jmp    1132 <simpson+0x32>
movsd  0x18(%rsp),%xmm0
mulsd  %xmm1,%xmm0
divsd  0xe7b(%rip),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define double @simpson(double (double)* nocapture %0, double %1, double %2) {
%4 = fsub double %2, %1
%5 = fdiv double %4, 1.000000e+02
br label %6
6: ; preds = %3, %22
%7 = phi i32 [ 0, %3 ], [ %24, %22 ]
%8 = phi double [ 0.000000e+00, %3 ], [ %23, %22 ]
%9 = sitofp i32 %7 to double
%10 = tail call double @llvm.fmuladd.f64(double %9, double %5, double %1)
switch i32 %7, label %14 [
i32 0, label %11
i32 100, label %11
]
11: ; preds = %6, %6
%12 = tail call double %0(double %10)
%13 = fadd double %8, %12
br label %22
14: ; preds = %6
%15 = and i32 %7, 1
%16 = icmp eq i32 %15, 0
%17 = tail call double %0(double %10)
br i1 %16, label %20, label %18
18: ; preds = %14
%19 = tail call double @llvm.fmuladd.f64(double %17, double 4.000000e+00, double %8)
br label %22
20: ; preds = %14
%21 = tail call double @llvm.fmuladd.f64(double %17, double 2.000000e+00, double %8)
br label %22
22: ; preds = %20, %18, %11
%23 = phi double [ %13, %11 ], [ %19, %18 ], [ %21, %20 ]
%24 = add nuw nsw i32 %7, 1
%25 = icmp eq i32 %24, 101
br i1 %25, label %26, label %6
26: ; preds = %22
%27 = fmul double %5, %23
%28 = fdiv double %27, 3.000000e+00
ret double %28
}