#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*/ EXIT_FAILURE;
int RAND_MAX;
long atol(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int random();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int zeroDist(double, double);
int main(int argc, char *argv[]) {
  long i;
  long iterations;
  double x, y;
  double inCircle = 0.0;
  double inSquare = 0.0;
  double pCircle = 0.0;
  double piCalc = 0.0;
  if (argc < 2) {
    iterations = 1000000;
  } else {
    iterations = atol(argv[1]);
    if (iterations < 1) {
      fprintf(stderr, "Bad iterations value\n");
      exit(EXIT_FAILURE);
    }
  }
  for (i = 0; i < iterations; i++) {
    x = (random() % ((RAND_MAX / 2) * 2)) - (RAND_MAX / 2);
    y = (random() % ((RAND_MAX / 2) * 2)) - (RAND_MAX / 2);
    if (zeroDist(x, y) < (RAND_MAX / 2)) {
      inCircle++;
    }
    inSquare++;
  }
  pCircle = inCircle / inSquare;
  piCalc = pCircle * 4.0;
  fprintf(stdout, "pi = %f\n", piCalc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119d <main+0x4d>
movq   $0xf4240,-0x20(%rbp)
jmp    11e3 <main+0x93>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atol@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jge    11de <main+0x8e>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11e3 <main+0x93>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    12ee <main+0x19e>
mov    $0x0,%al
call   1050 <random@plt>
mov    %eax,-0x64(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
shl    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x60(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x60(%rbp),%edx
sub    %eax,%edx
cvtsi2sd %edx,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    %eax,-0x5c(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
shl    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x58(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x58(%rbp),%edx
sub    %eax,%edx
cvtsi2sd %edx,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
call   1040 <zeroDist@plt>
mov    %eax,-0x54(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
jge    12cb <main+0x17b>
movsd  0xd47(%rip),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xd35(%rip),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11eb <main+0x9b>
movsd  -0x38(%rbp),%xmm0
divsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0xcfb(%rip),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
movsd  -0x50(%rbp),%xmm0
lea    0xd02(%rip),%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xf4240,%ebx
cmp    $0x2,%edi
jl     1176 <main+0x26>
mov    0x8(%rsi),%rdi
call   1060 <atol@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1272 <main+0x122>
xorpd  %xmm1,%xmm1
mov    0x2e67(%rip),%r14
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
jmp    11ac <main+0x5c>
nopl   0x0(%rax)
movsd  0x8(%rsp),%xmm1
movsd  0xe62(%rip),%xmm2
addsd  %xmm2,%xmm1
add    $0xffffffffffffffff,%rbx
je     1240 <main+0xf0>
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1050 <random@plt>
mov    (%r14),%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
add    %ecx,%esi
mov    %esi,%ecx
sar    %ecx
and    $0xfffffffe,%esi
cltd
idiv   %esi
sub    %ecx,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1050 <random@plt>
mov    (%r14),%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
add    %ecx,%esi
mov    %esi,%ecx
sar    %ecx
and    $0xfffffffe,%esi
cltd
idiv   %esi
sub    %ecx,%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
movsd  0x10(%rsp),%xmm0
call   1040 <zeroDist@plt>
mov    (%r14),%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
add    %ecx,%edx
sar    %edx
cmp    %edx,%eax
jge    1190 <main+0x40>
movsd  (%rsp),%xmm0
movsd  0xdd4(%rip),%xmm2
addsd  %xmm2,%xmm0
movsd  %xmm0,(%rsp)
movsd  0x8(%rsp),%xmm1
jmp    119e <main+0x4e>
movsd  (%rsp),%xmm0
divsd  %xmm1,%xmm0
mulsd  0xdb7(%rip),%xmm0
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Bad iterations value\0A\00"
@EXIT_FAILURE = global i32 0
@RAND_MAX = global i32 0
@stdout = global i32 0
@.str.1 = [9 x i8] c"pi = %f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 0.000000e+00, double* %10
store double 0.000000e+00, double* %11
store double 0.000000e+00, double* %12
store double 0.000000e+00, double* %13
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %2
store i64 1000000, i64* %7
br label %30
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @atol(i8* %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp slt i64 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
br label %30
30: ; preds = %29, %16
store i64 0, i64* %6
br label %31
31: ; preds = %66, %30
%32 = load i64, i64* %6
%33 = load i64, i64* %7
%34 = icmp slt i64 %32, %33
br i1 %34, label %35, label %69
35: ; preds = %31
%36 = call i32 (...) @random()
%37 = load i32, i32* @RAND_MAX
%38 = sdiv i32 %37, 2
%39 = mul nsw i32 %38, 2
%40 = srem i32 %36, %39
%41 = load i32, i32* @RAND_MAX
%42 = sdiv i32 %41, 2
%43 = sub nsw i32 %40, %42
%44 = sitofp i32 %43 to double
store double %44, double* %8
%45 = call i32 (...) @random()
%46 = load i32, i32* @RAND_MAX
%47 = sdiv i32 %46, 2
%48 = mul nsw i32 %47, 2
%49 = srem i32 %45, %48
%50 = load i32, i32* @RAND_MAX
%51 = sdiv i32 %50, 2
%52 = sub nsw i32 %49, %51
%53 = sitofp i32 %52 to double
store double %53, double* %9
%54 = load double, double* %8
%55 = load double, double* %9
%56 = call i32 @zeroDist(double %54, double %55)
%57 = load i32, i32* @RAND_MAX
%58 = sdiv i32 %57, 2
%59 = icmp slt i32 %56, %58
br i1 %59, label %60, label %63
60: ; preds = %35
%61 = load double, double* %10
%62 = fadd double %61, 1.000000e+00
store double %62, double* %10
br label %63
63: ; preds = %60, %35
%64 = load double, double* %11
%65 = fadd double %64, 1.000000e+00
store double %65, double* %11
br label %66
66: ; preds = %63
%67 = load i64, i64* %6
%68 = add nsw i64 %67, 1
store i64 %68, i64* %6
br label %31
69: ; preds = %31
%70 = load double, double* %10
%71 = load double, double* %11
%72 = fdiv double %70, %71
store double %72, double* %12
%73 = load double, double* %12
%74 = fmul double %73, 4.000000e+00
store double %74, double* %13
%75 = load i32, i32* @stdout
%76 = load double, double* %13
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %76)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Bad iterations value\0A\00"
@EXIT_FAILURE = global i32 0
@RAND_MAX = global i32 0
@stdout = global i32 0
@.str.1 = [9 x i8] c"pi = %f\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @atol(i8* %6)
%8 = icmp slt i64 %7, 1
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4, %2
%15 = phi i64 [ %7, %4 ], [ 1000000, %2 ]
br label %16
16: ; preds = %14, %16
%17 = phi double [ %40, %16 ], [ 0.000000e+00, %14 ]
%18 = phi double [ %39, %16 ], [ 0.000000e+00, %14 ]
%19 = phi i64 [ %41, %16 ], [ 0, %14 ]
%20 = tail call i32 (...) @random()
%21 = load i32, i32* @RAND_MAX
%22 = sdiv i32 %21, 2
%23 = shl nsw i32 %22, 1
%24 = srem i32 %20, %23
%25 = sub nsw i32 %24, %22
%26 = sitofp i32 %25 to double
%27 = tail call i32 (...) @random()
%28 = load i32, i32* @RAND_MAX
%29 = sdiv i32 %28, 2
%30 = shl nsw i32 %29, 1
%31 = srem i32 %27, %30
%32 = sub nsw i32 %31, %29
%33 = sitofp i32 %32 to double
%34 = tail call i32 @zeroDist(double %26, double %33)
%35 = load i32, i32* @RAND_MAX
%36 = sdiv i32 %35, 2
%37 = icmp slt i32 %34, %36
%38 = fadd double %18, 1.000000e+00
%39 = select i1 %37, double %38, double %18
%40 = fadd double %17, 1.000000e+00
%41 = add nuw nsw i64 %19, 1
%42 = icmp eq i64 %41, %15
br i1 %42, label %43, label %16
43: ; preds = %16
%44 = fdiv double %39, %40
%45 = fmul double %44, 4.000000e+00
%46 = load i32, i32* @stdout
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %45)
ret i32 0
}