#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*/ printf(char *, ...);
scalar_t__ rand();
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ srand(int);
int main(int argc, char **argv) {
  int niter = 0;
  double x, y;
  int i, count = 0;
  double z;
  double pi;
  printf("Enter the number of iterations used to estimate pi: ");
  scanf("%d", &niter);
  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 %g \n", niter, pi);
}


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   $0x0,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xecc(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <scanf@plt>
mov    $0x222218e,%edi
call   1040 <srand@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122e <main+0xee>
mov    $0x0,%al
call   1060 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e2f(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e13(%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  0xe01(%rip),%xmm0
ucomisd -0x38(%rbp),%xmm0
jb     121b <main+0xdb>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1220 <main+0xe0>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119a <main+0x5a>
cvtsi2sdl -0x30(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xdbc(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x14(%rbp),%esi
movsd  -0x40(%rbp),%xmm0
lea    0xdec(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x4(%rsp)
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xee0(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
mov    $0x222218e,%edi
call   1040 <srand@plt>
mov    0x4(%rsp),%esi
test   %esi,%esi
jle    11f3 <main+0xb3>
mov    0x2e60(%rip),%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
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  0xe28(%rip),%xmm0
ucomisd %xmm1,%xmm0
sbb    $0xffffffff,%ebp
add    $0x1,%ebx
mov    0x4(%rsp),%esi
cmp    %esi,%ebx
jl     1190 <main+0x50>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
jmp    11f7 <main+0xb7>
xorpd  %xmm0,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %esi,%xmm1
divsd  %xmm1,%xmm0
mulsd  0xdfe(%rip),%xmm0
lea    0xe37(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [53 x i8] c"Enter the number of iterations used to estimate pi: \00"
@.str.1 = [3 x i8] c"%d\00"
@RAND_MAX = global double 0.000000e+00
@.str.2 = [41 x i8] c"# of trials= %d , estimate of pi is %g \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%7 = call i32 @srand(i32 35791246)
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %2, %10
%11 = phi i32 [ %26, %10 ], [ 0, %2 ]
%12 = phi i32 [ %25, %10 ], [ 0, %2 ]
%13 = call i64 (...) @rand()
%14 = sitofp i64 %13 to double
%15 = load double, double* @RAND_MAX
%16 = fdiv double %14, %15
%17 = call i64 (...) @rand()
%18 = sitofp i64 %17 to double
%19 = load double, double* @RAND_MAX
%20 = fdiv double %18, %19
%21 = fmul double %20, %20
%22 = call double @llvm.fmuladd.f64(double %16, double %16, double %21)
%23 = fcmp ole double %22, 1.000000e+00
%24 = zext i1 %23 to i32
%25 = add nuw nsw i32 %12, %24
%26 = add nuw nsw i32 %11, 1
%27 = load i32, i32* %3
%28 = icmp slt i32 %26, %27
br i1 %28, label %10, label %29
29: ; preds = %10
%30 = sitofp i32 %25 to double
br label %31
31: ; preds = %29, %2
%32 = phi double [ 0.000000e+00, %2 ], [ %30, %29 ]
%33 = phi i32 [ %8, %2 ], [ %27, %29 ]
%34 = sitofp i32 %33 to double
%35 = fdiv double %32, %34
%36 = fmul double %35, 4.000000e+00
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %33, double %36)
ret i32 0
}