#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 RAND_MAX;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, double);
scalar_t__ rand();
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int niter = 100000;
  double x, y;
  int i, count = 0;
  double z;
  double pi;
  srand(35791246);
  count = 0;
  for (i = 0; i < niter; i++) {
    x = (double)rand() / RAND_MAX;
    y = (double)rand() / RAND_MAX;
    z = x * x + y * y;
    if (z <= 1)
      count++;
  }
  pi = (double)count / niter * 4;
  printf("# of trials= %d , estimate of pi is %f \n", niter, pi);
  fflush(stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x186a0,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x222218e,%edi
call   1040 <srand@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1210 <main+0xd0>
mov    $0x0,%al
call   1060 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e4d(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e31(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe1f(%rip),%xmm0
ucomisd -0x38(%rbp),%xmm0
jb     11fd <main+0xbd>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1202 <main+0xc2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    117c <main+0x3c>
cvtsi2sdl -0x30(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xdda(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x14(%rbp),%esi
movsd  -0x40(%rbp),%xmm0
lea    0xdd2(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x222218e,%edi
call   1040 <srand@plt>
mov    $0x186a0,%ebx
mov    0x2e8a(%rip),%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1060 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
mulsd  %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe58(%rip),%xmm0
ucomisd %xmm1,%xmm0
sbb    $0xffffffff,%ebp
add    $0xffffffff,%ebx
jne    1160 <main+0x20>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
divsd  0xe45(%rip),%xmm0
mulsd  0xe45(%rip),%xmm0
lea    0xe46(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
@.str = [41 x i8] c"# of trials= %d , estimate of pi is %f \0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 100000, i32* %6
store i32 0, i32* %10
%13 = call i32 @srand(i32 35791246)
store i32 0, i32* %10
store i32 0, i32* %9
br label %14
14: ; preds = %39, %2
%15 = load i32, i32* %9
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %42
18: ; preds = %14
%19 = call i64 (...) @rand()
%20 = sitofp i64 %19 to double
%21 = load double, double* @RAND_MAX
%22 = fdiv double %20, %21
store double %22, double* %7
%23 = call i64 (...) @rand()
%24 = sitofp i64 %23 to double
%25 = load double, double* @RAND_MAX
%26 = fdiv double %24, %25
store double %26, double* %8
%27 = load double, double* %7
%28 = load double, double* %7
%29 = load double, double* %8
%30 = load double, double* %8
%31 = fmul double %29, %30
%32 = call double @llvm.fmuladd.f64(double %27, double %28, double %31)
store double %32, double* %11
%33 = load double, double* %11
%34 = fcmp ole double %33, 1.000000e+00
br i1 %34, label %35, label %38
35: ; preds = %18
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
br label %38
38: ; preds = %35, %18
br label %39
39: ; preds = %38
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %14
42: ; preds = %14
%43 = load i32, i32* %10
%44 = sitofp i32 %43 to double
%45 = load i32, i32* %6
%46 = sitofp i32 %45 to double
%47 = fdiv double %44, %46
%48 = fmul double %47, 4.000000e+00
store double %48, double* %12
%49 = load i32, i32* %6
%50 = load double, double* %12
%51 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %49, double %50)
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
ret i32 0
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
@.str = [41 x i8] c"# of trials= %d , estimate of pi is %f \0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @srand(i32 35791246)
br label %4
4: ; preds = %2, %4
%5 = phi i32 [ 0, %2 ], [ %20, %4 ]
%6 = phi i32 [ 0, %2 ], [ %19, %4 ]
%7 = tail call i64 (...) @rand()
%8 = sitofp i64 %7 to double
%9 = load double, double* @RAND_MAX
%10 = fdiv double %8, %9
%11 = tail call i64 (...) @rand()
%12 = sitofp i64 %11 to double
%13 = load double, double* @RAND_MAX
%14 = fdiv double %12, %13
%15 = fmul double %14, %14
%16 = tail call double @llvm.fmuladd.f64(double %10, double %10, double %15)
%17 = fcmp ole double %16, 1.000000e+00
%18 = zext i1 %17 to i32
%19 = add nuw nsw i32 %6, %18
%20 = add nuw nsw i32 %5, 1
%21 = icmp eq i32 %20, 100000
br i1 %21, label %22, label %4
22: ; preds = %4
%23 = sitofp i32 %19 to double
%24 = fdiv double %23, 1.000000e+05
%25 = fmul double %24, 4.000000e+00
%26 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 100000, double %25)
%27 = load i32, i32* @stdout
%28 = tail call i32 @fflush(i32 %27)
ret i32 0
}