#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 M_PI;
int fabs(double);
double monte_carlo(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  double estimate = monte_carlo(1000000);
  printf("The estimate of pi is %g\n", estimate);
  printf("Percentage error: %0.2f%%\n", 100 * fabs(M_PI - estimate) / M_PI);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0xf4240,%edi
call   1070 <monte_carlo@plt>
movsd  %xmm0,-0x10(%rbp)
cvttsd2si -0x10(%rbp),%esi
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e52(%rip),%rax
movsd  (%rax),%xmm0
subsd  -0x10(%rbp),%xmm0
call   1050 <fabs@plt>
imul   $0x64,%eax,%eax
cvtsi2sd %eax,%xmm0
mov    0x2e36(%rip),%rax
divsd  (%rax),%xmm0
cvttsd2si %xmm0,%esi
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0xf4240,%edi
call   1070 <monte_carlo@plt>
movsd  %xmm0,0x8(%rsp)
cvttsd2si %xmm0,%esi
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e5e(%rip),%rbx
movsd  (%rbx),%xmm0
subsd  0x8(%rsp),%xmm0
call   1050 <fabs@plt>
imul   $0x64,%eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  (%rbx),%xmm0
cvttsd2si %xmm0,%esi
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"The estimate of pi is %g\0A\00"
@.str.1 = [27 x i8] c"Percentage error: %0.2f%%\0A\00"
@M_PI = global double 0.000000e+00
define i32 @main() {
%1 = alloca i32
%2 = alloca double
store i32 0, i32* %1
%3 = call i32 @time(i32* null)
%4 = call i32 @srand(i32 %3)
%5 = call double @monte_carlo(i32 1000000)
store double %5, double* %2
%6 = load double, double* %2
%7 = fptosi double %6 to i32
%8 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load double, double* @M_PI
%10 = load double, double* %2
%11 = fsub double %9, %10
%12 = call i32 @fabs(double %11)
%13 = mul nsw i32 100, %12
%14 = sitofp i32 %13 to double
%15 = load double, double* @M_PI
%16 = fdiv double %14, %15
%17 = fptosi double %16 to i32
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %17)
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"The estimate of pi is %g\0A\00"
@.str.1 = [27 x i8] c"Percentage error: %0.2f%%\0A\00"
@M_PI = global double 0.000000e+00
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = tail call double @monte_carlo(i32 1000000)
%4 = fptosi double %3 to i32
%5 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load double, double* @M_PI
%7 = fsub double %6, %3
%8 = tail call i32 @fabs(double %7)
%9 = mul nsw i32 %8, 100
%10 = sitofp i32 %9 to double
%11 = load double, double* @M_PI
%12 = fdiv double %10, %11
%13 = fptosi double %12 to i32
%14 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %13)
ret i32 0
}