#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(double left_enpt, double right_endp, int count, double base_len) {
  double estimate, x;
  int i;
  estimate = (f(left_enpt) + f(right_endp)) / 2.0;
  for (i = 1; i < count; i++) {
    x = left_enpt + i * base_len;
    estimate += f(x);
  }
  estimate *= base_len;
  return estimate;
}


x86 assembly (clang, -O0):
<trap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  0xea9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b1 <trap+0xa1>
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <f@plt>
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1167 <trap+0x57>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trap>:
push   %rbx
sub    $0x20,%rsp
movsd  %xmm2,0x10(%rsp)
mov    %edi,%ebx
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(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 i32
store double %0, double* %5
store double %1, double* %6
store i32 %2, i32* %7
store double %3, double* %8
%12 = load double, double* %5
%13 = call double @f(double %12)
%14 = load double, double* %6
%15 = call double @f(double %14)
%16 = fadd double %13, %15
%17 = fdiv double %16, 2.000000e+00
store double %17, double* %9
store i32 1, i32* %11
br label %18
18: ; preds = %32, %4
%19 = load i32, i32* %11
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load double, double* %5
%24 = load i32, i32* %11
%25 = sitofp i32 %24 to double
%26 = load double, double* %8
%27 = call double @llvm.fmuladd.f64(double %25, double %26, double %23)
store double %27, double* %10
%28 = load double, double* %10
%29 = call double @f(double %28)
%30 = load double, double* %9
%31 = fadd double %30, %29
store double %31, double* %9
br label %32
32: ; preds = %22
%33 = load i32, i32* %11
%34 = add nsw i32 %33, 1
store i32 %34, i32* %11
br label %18
35: ; preds = %18
%36 = load double, double* %8
%37 = load double, double* %9
%38 = fmul double %37, %36
store double %38, double* %9
%39 = load double, double* %9
ret double %39
}

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