#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double abs(double);
double stub1(double);
double stub2(double);
double stub3(double);
double integrate(double a, double b, double (*fun)(double)) {
  double f, x, h, s, res;
  int i, n;
  n = 5;
  h = (b - a) / n;
  res = 0;
  s = 1;
  while (abs(res - s) > 0.00001) {
    n *= 2;
    h *= 0.5;
    x = a + h;
    s = 0;
    for (i = 1; i < n; ++i) {
      s += (*fun)(x);
      x += h;
    }
    s += 0.5 * ((*fun)(a) + (*fun)(b));
    s *= h;
    res = s;
  }
  return s;
}


x86 assembly (clang, -O0):
<integrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
movl   $0x5,-0x48(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x48(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xeab(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x40(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
call   1030 <abs@plt>
movsd  0xe97(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1251 <integrate+0x141>
mov    -0x48(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x48(%rbp)
movsd  0xe84(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    11f2 <integrate+0xe2>
mov    -0x18(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11b4 <integrate+0xa4>
mov    -0x18(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x50(%rbp)
mov    -0x18(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  -0x38(%rbp),%xmm1
movsd  0xdea(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    115a <integrate+0x4a>
movsd  -0x38(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movsd  %xmm1,0x28(%rsp)
movsd  %xmm0,0x10(%rsp)
movsd  0xed1(%rip),%xmm0
call   1030 <abs@plt>
ucomisd 0xed4(%rip),%xmm0
jbe    124e <integrate+0x13e>
movsd  0x28(%rsp),%xmm2
subsd  0x10(%rsp),%xmm2
divsd  0xec2(%rip),%xmm2
mov    $0x5,%r14d
movsd  0xebc(%rip),%xmm1
jmp    11dd <integrate+0xcd>
cs nopw 0x0(%rax,%rax,1)
xorpd  %xmm1,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
call   *%rbx
movsd  %xmm0,0x20(%rsp)
movsd  0x28(%rsp),%xmm0
call   *%rbx
movapd %xmm0,%xmm1
addsd  0x20(%rsp),%xmm1
movsd  0xe7e(%rip),%xmm0
mulsd  %xmm0,%xmm1
addsd  0x8(%rsp),%xmm1
mulsd  0x18(%rsp),%xmm1
movsd  %xmm1,0x8(%rsp)
subsd  %xmm1,%xmm1
movapd %xmm1,%xmm0
call   1030 <abs@plt>
movsd  0xe53(%rip),%xmm1
ucomisd 0xe3b(%rip),%xmm0
movsd  0x18(%rsp),%xmm2
jbe    125c <integrate+0x14c>
mov    %r14d,%eax
lea    (%rax,%rax,1),%r14d
mulsd  %xmm1,%xmm2
test   %eax,%eax
movsd  %xmm2,0x18(%rsp)
jle    1170 <integrate+0x60>
cmp    $0x3,%r14d
mov    $0x2,%ebp
cmovge %r14d,%ebp
add    $0xffffffff,%ebp
xorpd  %xmm1,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
cs nopw 0x0(%rax,%rax,1)
addsd  0x18(%rsp),%xmm0
movsd  %xmm0,0x20(%rsp)
call   *%rbx
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x20(%rsp),%xmm0
add    $0xffffffff,%ebp
jne    1220 <integrate+0x110>
jmp    117a <integrate+0x6a>
movsd  0xdb2(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define double @integrate(double %0, double %1, double (double)* nocapture %2) {
%4 = tail call double @abs(double -1.000000e+00)
%5 = fcmp ogt double %4, 1.000000e-05
br i1 %5, label %6, label %36
6: ; preds = %3
%7 = fsub double %1, %0
%8 = fdiv double %7, 5.000000e+00
br label %9
9: ; preds = %6, %26
%10 = phi i32 [ %12, %26 ], [ 5, %6 ]
%11 = phi double [ %13, %26 ], [ %8, %6 ]
%12 = shl i32 %10, 1
%13 = fmul double %11, 5.000000e-01
%14 = icmp sgt i32 %10, 0
br i1 %14, label %15, label %26
15: ; preds = %9
%16 = call i32 @llvm.smax.i32(i32 %12, i32 2)
br label %17
17: ; preds = %15, %17
%18 = phi i32 [ %24, %17 ], [ 1, %15 ]
%19 = phi double [ %23, %17 ], [ 0.000000e+00, %15 ]
%20 = phi double [ %21, %17 ], [ %0, %15 ]
%21 = fadd double %13, %20
%22 = tail call double %2(double %21)
%23 = fadd double %19, %22
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, %16
br i1 %25, label %26, label %17
26: ; preds = %17, %9
%27 = phi double [ 0.000000e+00, %9 ], [ %23, %17 ]
%28 = tail call double %2(double %0)
%29 = tail call double %2(double %1)
%30 = fadd double %28, %29
%31 = tail call double @llvm.fmuladd.f64(double %30, double 5.000000e-01, double %27)
%32 = fmul double %13, %31
%33 = fsub double %32, %32
%34 = tail call double @abs(double %33)
%35 = fcmp ogt double %34, 1.000000e-05
br i1 %35, label %9, label %36
36: ; preds = %26, %3
%37 = phi double [ 1.000000e+00, %3 ], [ %32, %26 ]
ret double %37
}