#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 */
struct timespec {
  unsigned int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
float RAND_MAX;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, unsigned int);
scalar_t__ rand_r(unsigned int *);
float piEst(int n) {
  float sum = 0.0;
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  unsigned int seed = t.tv_nsec;
  printf("seed=%u\n", seed);
  for (int i = 0; i < n; i++) {
    float x = (float)rand_r(&seed) / RAND_MAX * 2 - 1;
    float y = (float)rand_r(&seed) / RAND_MAX * 2 - 1;
    if (x * x + y * y <= 1.0)
      sum++;
  }
  return sum;
}


x86 assembly (clang, -O0):
<piEst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x8(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xeab(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1230 <piEst+0x100>
lea    -0x14(%rbp),%rdi
call   1040 <rand_r@plt>
cvtsi2ss %rax,%xmm0
mov    0x2e4e(%rip),%rax
divss  (%rax),%xmm0
addss  %xmm0,%xmm0
movss  0xe66(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <rand_r@plt>
cvtsi2ss %rax,%xmm0
mov    0x2e20(%rip),%rax
divss  (%rax),%xmm0
addss  %xmm0,%xmm0
movss  0xe38(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  -0x1c(%rbp),%xmm2
movss  -0x20(%rbp),%xmm1
mulss  -0x20(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xdff(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     121d <piEst+0xed>
movss  0xdf9(%rip),%xmm0
addss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    1222 <piEst+0xf2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1171 <piEst+0x41>
movss  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<piEst>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%esi
mov    %esi,0xc(%rsp)
lea    0xeab(%rip),%rdi
call   1050 <printf@plt>
test   %ebx,%ebx
jle    1217 <piEst+0xe7>
xorps  %xmm1,%xmm1
lea    0xc(%rsp),%r14
mov    0x2e67(%rip),%r15
nopl   0x0(%rax)
movaps %xmm1,0x20(%rsp)
mov    %r14,%rdi
call   1040 <rand_r@plt>
xorps  %xmm1,%xmm1
cvtsi2ss %rax,%xmm1
divss  (%r15),%xmm1
addss  %xmm1,%xmm1
movss  0xe5a(%rip),%xmm0
addss  %xmm0,%xmm1
movaps %xmm1,0x10(%rsp)
mov    %r14,%rdi
call   1040 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
divss  (%r15),%xmm0
addss  %xmm0,%xmm0
addss  0xe30(%rip),%xmm0
mulss  %xmm0,%xmm0
movaps 0x10(%rsp),%xmm3
mulss  %xmm3,%xmm3
addss  %xmm0,%xmm3
movss  0xe1b(%rip),%xmm1
cmpnless %xmm1,%xmm3
movaps %xmm3,%xmm2
movaps 0x20(%rsp),%xmm0
andps  %xmm0,%xmm2
addss  %xmm1,%xmm0
movaps %xmm0,%xmm1
movaps %xmm3,%xmm0
andnps %xmm1,%xmm0
orps   %xmm2,%xmm0
movaps %xmm0,%xmm1
add    $0xffffffff,%ebx
jne    1180 <piEst+0x50>
jmp    121a <piEst+0xea>
xorps  %xmm0,%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [9 x i8] c"seed=%u\0A\00"
@RAND_MAX = global float 0.000000e+00
define float @piEst(i32 %0) {
%2 = alloca i32
%3 = alloca float
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = alloca float
%8 = alloca float
store i32 %0, i32* %2
store float 0.000000e+00, float* %3
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %4)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%12 = load i32, i32* %11
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 0, i32* %6
br label %15
15: ; preds = %42, %1
%16 = load i32, i32* %6
%17 = load i32, i32* %2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %45
19: ; preds = %15
%20 = call i64 @rand_r(i32* %5)
%21 = sitofp i64 %20 to float
%22 = load float, float* @RAND_MAX
%23 = fdiv float %21, %22
%24 = call float @llvm.fmuladd.f32(float %23, float 2.000000e+00, float -1.000000e+00)
store float %24, float* %7
%25 = call i64 @rand_r(i32* %5)
%26 = sitofp i64 %25 to float
%27 = load float, float* @RAND_MAX
%28 = fdiv float %26, %27
%29 = call float @llvm.fmuladd.f32(float %28, float 2.000000e+00, float -1.000000e+00)
store float %29, float* %8
%30 = load float, float* %7
%31 = load float, float* %7
%32 = load float, float* %8
%33 = load float, float* %8
%34 = fmul float %32, %33
%35 = call float @llvm.fmuladd.f32(float %30, float %31, float %34)
%36 = fpext float %35 to double
%37 = fcmp ole double %36, 1.000000e+00
br i1 %37, label %38, label %41
38: ; preds = %19
%39 = load float, float* %3
%40 = fadd float %39, 1.000000e+00
store float %40, float* %3
br label %41
41: ; preds = %38, %19
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %15
45: ; preds = %15
%46 = load float, float* %3
ret float %46
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [9 x i8] c"seed=%u\0A\00"
@RAND_MAX = global float 0.000000e+00
define float @piEst(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = bitcast %struct.timespec* %2 to i8*
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %2)
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%9 = load i32, i32* %8
store i32 %9, i32* %3
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %0, 0
br i1 %11, label %14, label %12
12: ; preds = %14, %1
%13 = phi float [ 0.000000e+00, %1 ], [ %31, %14 ]
ret float %13
14: ; preds = %1, %14
%15 = phi float [ %31, %14 ], [ 0.000000e+00, %1 ]
%16 = phi i32 [ %32, %14 ], [ 0, %1 ]
%17 = call i64 @rand_r(i32* %3)
%18 = sitofp i64 %17 to float
%19 = load float, float* @RAND_MAX
%20 = fdiv float %18, %19
%21 = call float @llvm.fmuladd.f32(float %20, float 2.000000e+00, float -1.000000e+00)
%22 = call i64 @rand_r(i32* %3)
%23 = sitofp i64 %22 to float
%24 = load float, float* @RAND_MAX
%25 = fdiv float %23, %24
%26 = call float @llvm.fmuladd.f32(float %25, float 2.000000e+00, float -1.000000e+00)
%27 = fmul float %26, %26
%28 = call float @llvm.fmuladd.f32(float %21, float %21, float %27)
%29 = fcmp ugt float %28, 1.000000e+00
%30 = fadd float %15, 1.000000e+00
%31 = select i1 %29, float %15, float %30
%32 = add nuw nsw i32 %16, 1
%33 = icmp eq i32 %32, %0
br i1 %33, label %12, label %14
}