double rungeKuttaSecondOrder(double (*f)(double, double), int timeSteps,
                             double h) {
  double y = 1.0, x, k1, k2;
  int i;
  for (i = 0; i < timeSteps; i++) {
    x = h * i;
    k1 = h * f(x, y);
    k2 = h * f(x + h, y + k1);
    y = y + ((k1 + k2) / 2);
  }
  return y;
}


x86 assembly (clang, -O0):
<rungeKuttaSecondOrder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  0xee4(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d9 <rungeKuttaSecondOrder+0xd9>
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x3c(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
addsd  -0x30(%rbp),%xmm1
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
addsd  -0x38(%rbp),%xmm1
movsd  0xe4a(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1128 <rungeKuttaSecondOrder+0x28>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rungeKuttaSecondOrder>:
push   %r14
push   %rbx
sub    $0x28,%rsp
test   %esi,%esi
jle    11aa <rungeKuttaSecondOrder+0xaa>
movapd %xmm0,%xmm2
mov    %esi,%ebx
mov    %rdi,%r14
movsd  0xee0(%rip),%xmm1
xorpd  %xmm3,%xmm3
movsd  %xmm0,(%rsp)
nopl   0x0(%rax)
movsd  %xmm3,0x20(%rsp)
movsd  %xmm1,0x8(%rsp)
movapd %xmm3,%xmm0
mulsd  %xmm2,%xmm0
movsd  %xmm0,0x10(%rsp)
call   *%r14
mulsd  (%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
movsd  0x10(%rsp),%xmm2
addsd  (%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movapd %xmm2,%xmm0
call   *%r14
movsd  0x20(%rsp),%xmm3
movsd  0x8(%rsp),%xmm1
movsd  (%rsp),%xmm2
mulsd  %xmm2,%xmm0
addsd  0x18(%rsp),%xmm0
mulsd  0xe71(%rip),%xmm0
addsd  %xmm0,%xmm1
addsd  0xe5d(%rip),%xmm3
add    $0xffffffff,%ebx
jne    1130 <rungeKuttaSecondOrder+0x30>
jmp    11b2 <rungeKuttaSecondOrder+0xb2>
movsd  0xe4e(%rip),%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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