#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 fabsf(double);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  double integral;
  int a = 1, b = 6;
  int n = 2000;
  double h;
  double x;
  h = (double)(b - a) / n;
  integral = (f(a) + f(b)) / 2.0;
  x = a;
  for (int i = 1; i < n; i++) {
    x = x + h;
    integral = integral + f(x);
  }
  integral = integral * h;
  double expected = 1.79175946923;
  printf("Integral (%d to %d) of 1/x \n\n", a, b);
  printf("Expected  result  = %.11f\n", expected);
  printf("Estimated result  = %.11f\n", integral);
  printf("                    -------------\n");
  double difference = expected - integral;
  difference = fabsf(difference);
  printf("Difference        = %.11f\n", difference);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x6,-0x20(%rbp)
movl   $0x7d0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
cvtsi2sd %eax,%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
call   1040 <f@plt>
movsd  %xmm0,-0x58(%rbp)
cvtsi2sdl -0x20(%rbp),%xmm0
call   1040 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  0xe60(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1208 <main+0xd8>
movsd  -0x38(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1040 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x60(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ba <main+0x8a>
movsd  -0x18(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xde9(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movsd  -0x48(%rbp),%xmm0
lea    0xdea(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movsd  -0x18(%rbp),%xmm0
lea    0xdf2(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movsd  -0x48(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
call   1030 <fabsf@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
lea    0xdf1(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movsd  0xec3(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1040 <f@plt>
movsd  %xmm0,(%rsp)
movsd  0xeb3(%rip),%xmm0
call   1040 <f@plt>
addsd  (%rsp),%xmm0
mulsd  0xea9(%rip),%xmm0
mov    $0x7cf,%ebx
nopl   0x0(%rax)
movsd  %xmm0,(%rsp)
movsd  0x8(%rsp),%xmm0
addsd  0xe95(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1040 <f@plt>
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0xffffffff,%ebx
jne    1170 <main+0x40>
mulsd  0xe6a(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0xe86(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe8c(%rip),%rdi
movsd  0xe46(%rip),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xe91(%rip),%rdi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
movsd  0xe18(%rip),%xmm0
subsd  (%rsp),%xmm0
andpd  0xe1b(%rip),%xmm0
lea    0xe78(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Integral (%d to %d) of 1/x \0A\0A\00"
@.str.1 = [27 x i8] c"Expected result = %.11f\0A\00"
@.str.2 = [27 x i8] c"Estimated result = %.11f\0A\00"
@.str.3 = [35 x i8] c" -------------\0A\00"
@.str.4 = [27 x i8] c"Difference = %.11f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca i32
%13 = alloca double
%14 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 6, i32* %8
store i32 2000, i32* %9
%15 = load i32, i32* %8
%16 = load i32, i32* %7
%17 = sub nsw i32 %15, %16
%18 = sitofp i32 %17 to double
%19 = load i32, i32* %9
%20 = sitofp i32 %19 to double
%21 = fdiv double %18, %20
store double %21, double* %10
%22 = load i32, i32* %7
%23 = sitofp i32 %22 to double
%24 = call double @f(double %23)
%25 = load i32, i32* %8
%26 = sitofp i32 %25 to double
%27 = call double @f(double %26)
%28 = fadd double %24, %27
%29 = fdiv double %28, 2.000000e+00
store double %29, double* %6
%30 = load i32, i32* %7
%31 = sitofp i32 %30 to double
store double %31, double* %11
store i32 1, i32* %12
br label %32
32: ; preds = %44, %2
%33 = load i32, i32* %12
%34 = load i32, i32* %9
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load double, double* %11
%38 = load double, double* %10
%39 = fadd double %37, %38
store double %39, double* %11
%40 = load double, double* %6
%41 = load double, double* %11
%42 = call double @f(double %41)
%43 = fadd double %40, %42
store double %43, double* %6
br label %44
44: ; preds = %36
%45 = load i32, i32* %12
%46 = add nsw i32 %45, 1
store i32 %46, i32* %12
br label %32
47: ; preds = %32
%48 = load double, double* %6
%49 = load double, double* %10
%50 = fmul double %48, %49
store double %50, double* %6
store double 0x3FFCAB0BFA2A423A, double* %13
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %51, i32 %52)
%54 = load double, double* %13
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double %54)
%56 = load double, double* %6
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), double %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%59 = load double, double* %13
%60 = load double, double* %6
%61 = fsub double %59, %60
store double %61, double* %14
%62 = load double, double* %14
%63 = call double @fabsf(double %62)
store double %63, double* %14
%64 = load double, double* %14
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"Integral (%d to %d) of 1/x \0A\0A\00"
@.str.1 = [27 x i8] c"Expected result = %.11f\0A\00"
@.str.2 = [27 x i8] c"Estimated result = %.11f\0A\00"
@.str.4 = [27 x i8] c"Difference = %.11f\0A\00"
@str = [34 x i8] c" -------------\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call double @f(double 1.000000e+00)
%4 = tail call double @f(double 6.000000e+00)
%5 = fadd double %3, %4
%6 = fmul double %5, 5.000000e-01
br label %16
7: ; preds = %16
%8 = fmul double %22, 2.500000e-03
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 1, i32 6)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double 0x3FFCAB0BFA2A423A)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), double %8)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%13 = fsub double 0x3FFCAB0BFA2A423A, %8
%14 = tail call double @llvm.fabs.f64(double %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %14)
ret i32 0
16: ; preds = %2, %16
%17 = phi double [ %6, %2 ], [ %22, %16 ]
%18 = phi double [ 1.000000e+00, %2 ], [ %20, %16 ]
%19 = phi i32 [ 1, %2 ], [ %23, %16 ]
%20 = fadd double %18, 2.500000e-03
%21 = tail call double @f(double %20)
%22 = fadd double %17, %21
%23 = add nuw nsw i32 %19, 1
%24 = icmp eq i32 %23, 2000
br i1 %24, label %7, label %16
}