double int_midrect(double from, double to, int n, double (*func)()) {
  double h = (to - from) / n;
  double sum = 0.0, x;
  for (x = from; x <= (to - h); x += h)
    sum += func(x + h / 2.0);
  return h * sum;
}


x86 assembly (clang, -O0):
<int_midrect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     119c <int_midrect+0x9c>
mov    -0x20(%rbp),%rcx
movsd  -0x38(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  0xe8e(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
mov    $0x1,%al
call   *%rcx
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    1143 <int_midrect+0x43>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_midrect>:
push   %rbx
sub    $0x30,%rsp
movapd %xmm0,%xmm3
movapd %xmm1,%xmm2
subsd  %xmm0,%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %edi,%xmm0
divsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
subsd  %xmm2,%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm3,%xmm1
jb     119c <int_midrect+0x9c>
mov    %rsi,%rbx
movsd  0xec5(%rip),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm2,%xmm2
movsd  %xmm1,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm2,0x20(%rsp)
movsd  %xmm3,0x28(%rsp)
movsd  0x10(%rsp),%xmm0
addsd  %xmm3,%xmm0
mov    $0x1,%al
call   *%rbx
movsd  0x20(%rsp),%xmm2
movsd  0x28(%rsp),%xmm3
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm2
addsd  0x8(%rsp),%xmm3
ucomisd %xmm3,%xmm1
jae    1160 <int_midrect+0x60>
movsd  0x8(%rsp),%xmm0
mulsd  %xmm2,%xmm0
add    $0x30,%rsp
pop    %rbx
ret

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

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