#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 */
scalar_t__ RAND_MAX;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
double rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int i;
  double random_x = 0;
  double random_y = 0;
  double pi = 0;
  if (argc != 2) {
    printf("ERROR: Needs exactly 1 argument.\n");
    exit(1);
  }
  int len = atoi(argv[1]);
  srand(time(NULL));
#pragma omp parallel for
  for (int i = 0; i < len; i++) {
    double random_x = rand() / (double)RAND_MAX;
    double random_y = rand() / (double)RAND_MAX;
    int add = random_x * random_x + random_y * random_y <= 1;
#pragma omp critical
    { pi += add; }
  }
  pi *= 4.0 / len;
  printf("Pi is: %f\n", pi);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b0 <main+0x50>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    126b <main+0x10b>
mov    $0x0,%al
call   1080 <rand@plt>
mov    0x2df7(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
mov    0x2ddb(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdc9(%rip),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4c(%rbp)
cvtsi2sdl -0x4c(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11d7 <main+0x77>
cvtsi2sdl -0x34(%rbp),%xmm1
movsd  0xd88(%rip),%xmm0
divsd  %xmm1,%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xda0(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    1245 <main+0xd5>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
xorpd  %xmm2,%xmm2
test   %r14d,%r14d
jle    1214 <main+0xa4>
mov    0x2e3d(%rip),%rbx
mov    %r14d,%ebp
xchg   %ax,%ax
movsd  %xmm2,0x8(%rsp)
xor    %eax,%eax
call   1090 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sdq (%rbx),%xmm1
divsd  %xmm1,%xmm0
movapd %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1090 <rand@plt>
movsd  0x8(%rsp),%xmm2
xorps  %xmm1,%xmm1
cvtsi2sdq (%rbx),%xmm1
divsd  %xmm1,%xmm0
mulsd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdfe(%rip),%xmm0
cmplesd %xmm0,%xmm1
andpd  %xmm0,%xmm1
addsd  %xmm1,%xmm2
add    $0xffffffff,%ebp
jne    11b0 <main+0x40>
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
movsd  0xde4(%rip),%xmm0
divsd  %xmm1,%xmm0
mulsd  %xmm2,%xmm0
lea    0xddd(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"ERROR: Needs exactly 1 argument.\0A\00"
@RAND_MAX = global i64 0
@.str.1 = [11 x i8] c"Pi is: %f\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 double
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca double
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 0.000000e+00, double* %7
store double 0.000000e+00, double* %8
store double 0.000000e+00, double* %9
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %10
%25 = call i32 @time(i32* null)
%26 = call i32 @srand(i32 %25)
store i32 0, i32* %11
br label %27
27: ; preds = %52, %20
%28 = load i32, i32* %11
%29 = load i32, i32* %10
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %55
31: ; preds = %27
%32 = call double (...) @rand()
%33 = load i64, i64* @RAND_MAX
%34 = sitofp i64 %33 to double
%35 = fdiv double %32, %34
store double %35, double* %12
%36 = call double (...) @rand()
%37 = load i64, i64* @RAND_MAX
%38 = sitofp i64 %37 to double
%39 = fdiv double %36, %38
store double %39, double* %13
%40 = load double, double* %12
%41 = load double, double* %12
%42 = load double, double* %13
%43 = load double, double* %13
%44 = fmul double %42, %43
%45 = call double @llvm.fmuladd.f64(double %40, double %41, double %44)
%46 = fcmp ole double %45, 1.000000e+00
%47 = zext i1 %46 to i32
store i32 %47, i32* %14
%48 = load i32, i32* %14
%49 = sitofp i32 %48 to double
%50 = load double, double* %9
%51 = fadd double %50, %49
store double %51, double* %9
br label %52
52: ; preds = %31
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %27
55: ; preds = %27
%56 = load i32, i32* %10
%57 = sitofp i32 %56 to double
%58 = fdiv double 4.000000e+00, %57
%59 = load double, double* %9
%60 = fmul double %59, %58
store double %60, double* %9
%61 = load double, double* %9
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double %61)
ret i32 0
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
@.str.1 = [11 x i8] c"Pi is: %f\0A\00"
@str = [33 x i8] c"ERROR: Needs exactly 1 argument.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i32 @time(i32* null)
%12 = tail call i32 @srand(i32 %11)
%13 = icmp sgt i32 %10, 0
br i1 %13, label %20, label %14
14: ; preds = %20, %7
%15 = phi double [ 0.000000e+00, %7 ], [ %35, %20 ]
%16 = sitofp i32 %10 to double
%17 = fdiv double 4.000000e+00, %16
%18 = fmul double %17, %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double %18)
ret i32 0
20: ; preds = %7, %20
%21 = phi double [ %35, %20 ], [ 0.000000e+00, %7 ]
%22 = phi i32 [ %36, %20 ], [ 0, %7 ]
%23 = tail call double (...) @rand()
%24 = load i64, i64* @RAND_MAX
%25 = sitofp i64 %24 to double
%26 = fdiv double %23, %25
%27 = tail call double (...) @rand()
%28 = load i64, i64* @RAND_MAX
%29 = sitofp i64 %28 to double
%30 = fdiv double %27, %29
%31 = fmul double %30, %30
%32 = tail call double @llvm.fmuladd.f64(double %26, double %26, double %31)
%33 = fcmp ole double %32, 1.000000e+00
%34 = uitofp i1 %33 to double
%35 = fadd double %21, %34
%36 = add nuw nsw i32 %22, 1
%37 = icmp eq i32 %36, %10
br i1 %37, label %14, label %20
}