#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 f(double);
double Trap(int rank, double a, double b, double h, int trapCount) {
  double estimate, x, sum = 0;
  int i;
  estimate = (f(a) + f(b)) / 2.0;
  for (i = 1; i < trapCount; i++) {
    x = a + i * h;
    estimate += f(x);
  }
  estimate = estimate * h;
  return estimate;
}


x86 assembly (clang, -O0):
<Trap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  0xe9e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11bc <Trap+0xac>
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x44(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <f@plt>
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1172 <Trap+0x62>
movsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Trap>:
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
movsd  %xmm2,0x10(%rsp)
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x18(%rsp)
call   1030 <f@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   1030 <f@plt>
addsd  0x8(%rsp),%xmm0
mulsd  0xeb5(%rip),%xmm0
cmp    $0x2,%ebx
jl     11a6 <Trap+0x96>
add    $0xffffffff,%ebx
movsd  0xead(%rip),%xmm1
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,(%rsp)
movapd %xmm1,%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0x18(%rsp),%xmm0
call   1030 <f@plt>
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x8(%rsp),%xmm1
movsd  (%rsp),%xmm0
addsd  0xe67(%rip),%xmm1
add    $0xffffffff,%ebx
jne    1160 <Trap+0x50>
mulsd  0x10(%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
ret

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

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