#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 */
int /*<<< orphan*/ printf(char *, int, ...);
double trapezoid_numerical_integration(double x0, double xn, int ndivisions,
                                       double (*fp)(double)) {
  int i;
  double result = 0, h;
  h = (xn - x0) / ndivisions;
  printf("Value of h is %lf:\n", h);
  for (i = 0; i <= ndivisions; i++) {
    if (i == 0 || i == ndivisions)
      result += fp(x0 + i * h);
    else
      result += 2.0 * fp(i);
    printf("%lf ", 2.0 * fp(i));
    printf("Iterations: %d and f(x) = %lf and sum = %lf \n", i, fp(x0 + i * h),
           result);
  }
  result = result * 0.5 * h;
  return result;
}


x86 assembly (clang, -O0):
<trapezoid_numerical_integration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
cvttsd2si -0x38(%rbp),%esi
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1241 <trapezoid_numerical_integration+0x131>
cmpl   $0x0,-0x24(%rbp)
je     1185 <trapezoid_numerical_integration+0x75>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11b1 <trapezoid_numerical_integration+0xa1>
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x24(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11ce <trapezoid_numerical_integration+0xbe>
mov    -0x20(%rbp),%rax
cvtsi2sdl -0x24(%rbp),%xmm0
call   *%rax
movsd  -0x30(%rbp),%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cvtsi2sdl -0x24(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  0xe24(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x24(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
mov    -0x3c(%rbp),%esi
movsd  -0x30(%rbp),%xmm1
lea    0xdfd(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1163 <trapezoid_numerical_integration+0x53>
movsd  0xdb7(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trapezoid_numerical_integration>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
cvtsi2sd %edi,%xmm2
movsd  %xmm0,0x18(%rsp)
subsd  %xmm0,%xmm1
divsd  %xmm2,%xmm1
movsd  %xmm1,0x8(%rsp)
cvttsd2si %xmm1,%esi
lea    0xec1(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
test   %r14d,%r14d
js     1232 <trapezoid_numerical_integration+0x122>
lea    0x1(%r14),%r13d
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
lea    0xeb1(%rip),%r15
lea    0xeaf(%rip),%r12
xor    %ebp,%ebp
jmp    11e1 <trapezoid_numerical_integration+0xd1>
cs nopw 0x0(%rax,%rax,1)
movapd %xmm1,%xmm0
mulsd  0x8(%rsp),%xmm0
addsd  0x18(%rsp),%xmm0
movsd  %xmm0,0x20(%rsp)
call   *%rbx
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x10(%rsp),%xmm0
call   *%rbx
addsd  %xmm0,%xmm0
cvttsd2si %xmm0,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movsd  0x20(%rsp),%xmm0
call   *%rbx
mov    %r12,%rdi
mov    %ebp,%esi
movsd  (%rsp),%xmm1
mov    $0x2,%al
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
je     123b <trapezoid_numerical_integration+0x12b>
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
test   %ebp,%ebp
movsd  %xmm1,0x10(%rsp)
je     1180 <trapezoid_numerical_integration+0x70>
cmp    %ebp,%r14d
je     1180 <trapezoid_numerical_integration+0x70>
movsd  0x10(%rsp),%xmm0
call   *%rbx
addsd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x10(%rsp),%xmm0
movapd %xmm0,%xmm1
mulsd  0x8(%rsp),%xmm1
addsd  0x18(%rsp),%xmm1
movsd  %xmm1,0x20(%rsp)
jmp    11ac <trapezoid_numerical_integration+0x9c>
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
movsd  (%rsp),%xmm1
mulsd  0xdb8(%rip),%xmm1
movsd  0x8(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Value of h is %lf:\0A\00"
@.str.1 = [5 x i8] c"%lf \00"
@.str.2 = [46 x i8] c"Iterations: %d and f(x) = %lf and sum = %lf \0A\00"
define double @trapezoid_numerical_integration(double %0, double %1, i32 %2, double (double)* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double (double)*
%9 = alloca i32
%10 = alloca double
%11 = alloca double
store double %0, double* %5
store double %1, double* %6
store i32 %2, i32* %7
store double (double)* %3, double (double)** %8
store double 0.000000e+00, double* %10
%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* %11
%18 = load double, double* %11
%19 = fptosi double %18 to i32
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 0, i32* %9
br label %21
21: ; preds = %67, %4
%22 = load i32, i32* %9
%23 = load i32, i32* %7
%24 = icmp sle i32 %22, %23
br i1 %24, label %25, label %70
25: ; preds = %21
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* %9
%30 = load i32, i32* %7
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %28, %25
%33 = load double (double)*, double (double)** %8
%34 = load double, double* %5
%35 = load i32, i32* %9
%36 = sitofp i32 %35 to double
%37 = load double, double* %11
%38 = call double @llvm.fmuladd.f64(double %36, double %37, double %34)
%39 = call double %33(double %38)
%40 = load double, double* %10
%41 = fadd double %40, %39
store double %41, double* %10
br label %49
42: ; preds = %28
%43 = load double (double)*, double (double)** %8
%44 = load i32, i32* %9
%45 = sitofp i32 %44 to double
%46 = call double %43(double %45)
%47 = load double, double* %10
%48 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %46, double %47)
store double %48, double* %10
br label %49
49: ; preds = %42, %32
%50 = load double (double)*, double (double)** %8
%51 = load i32, i32* %9
%52 = sitofp i32 %51 to double
%53 = call double %50(double %52)
%54 = fmul double 2.000000e+00, %53
%55 = fptosi double %54 to i32
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %9
%58 = load double (double)*, double (double)** %8
%59 = load double, double* %5
%60 = load i32, i32* %9
%61 = sitofp i32 %60 to double
%62 = load double, double* %11
%63 = call double @llvm.fmuladd.f64(double %61, double %62, double %59)
%64 = call double %58(double %63)
%65 = load double, double* %10
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %57, double %64, double %65)
br label %67
67: ; preds = %49
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %21
70: ; preds = %21
%71 = load double, double* %10
%72 = fmul double %71, 5.000000e-01
%73 = load double, double* %11
%74 = fmul double %72, %73
store double %74, double* %10
%75 = load double, double* %10
ret double %75
}

LLVM IR (-O2):
@.str = [20 x i8] c"Value of h is %lf:\0A\00"
@.str.1 = [5 x i8] c"%lf \00"
@.str.2 = [46 x i8] c"Iterations: %d and f(x) = %lf and sum = %lf \0A\00"
define double @trapezoid_numerical_integration(double %0, double %1, i32 %2, double (double)* nocapture %3) {
%5 = fsub double %1, %0
%6 = sitofp i32 %2 to double
%7 = fdiv double %5, %6
%8 = fptosi double %7 to i32
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %2, 0
br i1 %10, label %37, label %11
11: ; preds = %4, %26
%12 = phi i32 [ %35, %26 ], [ 0, %4 ]
%13 = phi double [ %28, %26 ], [ 0.000000e+00, %4 ]
%14 = icmp eq i32 %12, 0
%15 = icmp eq i32 %12, %2
%16 = or i1 %14, %15
%17 = sitofp i32 %12 to double
br i1 %16, label %18, label %22
18: ; preds = %11
%19 = tail call double @llvm.fmuladd.f64(double %17, double %7, double %0)
%20 = tail call double %3(double %19)
%21 = fadd double %13, %20
br label %26
22: ; preds = %11
%23 = tail call double %3(double %17)
%24 = tail call double @llvm.fmuladd.f64(double %23, double 2.000000e+00, double %13)
%25 = tail call double @llvm.fmuladd.f64(double %17, double %7, double %0)
br label %26
26: ; preds = %22, %18
%27 = phi double [ %25, %22 ], [ %19, %18 ]
%28 = phi double [ %24, %22 ], [ %21, %18 ]
%29 = tail call double %3(double %17)
%30 = fmul double %29, 2.000000e+00
%31 = fptosi double %30 to i32
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = tail call double %3(double %27)
%34 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %12, double %33, double %28)
%35 = add nuw i32 %12, 1
%36 = icmp eq i32 %12, %2
br i1 %36, label %37, label %11
37: ; preds = %26, %4
%38 = phi double [ 0.000000e+00, %4 ], [ %28, %26 ]
%39 = fmul double %38, 5.000000e-01
%40 = fmul double %7, %39
ret double %40
}