#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 omp_get_thread_num();
int /*<<< orphan*/ printf(char *, int, unsigned int);
scalar_t__ rand_r(unsigned int *);
float piEst(int n) {
  float sum = 0.0;
#pragma omp parallel
  {
    int id = omp_get_thread_num();
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    unsigned int seed = t.tv_nsec;
    printf("thread %d, seed=%u\n", id, seed);
#pragma omp for reduction(+ : sum)
    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    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe8e(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    124d <piEst+0x10d>
lea    -0x14(%rbp),%rdi
call   1050 <rand_r@plt>
cvtsi2ss %rax,%xmm0
mov    0x2e31(%rip),%rax
divss  (%rax),%xmm0
addss  %xmm0,%xmm0
movss  0xe49(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <rand_r@plt>
cvtsi2ss %rax,%xmm0
mov    0x2e03(%rip),%rax
divss  (%rax),%xmm0
addss  %xmm0,%xmm0
movss  0xe1b(%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  0xde2(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     123a <piEst+0xfa>
movss  0xddc(%rip),%xmm0
addss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    123f <piEst+0xff>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118e <piEst+0x4e>
movss  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<piEst>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%ebp
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x8(%rsp),%edx
mov    %edx,0xc(%rsp)
lea    0xe93(%rip),%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
test   %ebx,%ebx
jle    1237 <piEst+0xf7>
xorps  %xmm1,%xmm1
lea    0xc(%rsp),%r14
mov    0x2e4d(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movaps %xmm1,0x20(%rsp)
mov    %r14,%rdi
call   1050 <rand_r@plt>
xorps  %xmm1,%xmm1
cvtsi2ss %rax,%xmm1
divss  0x0(%rbp),%xmm1
addss  %xmm1,%xmm1
movss  0xe3a(%rip),%xmm0
addss  %xmm0,%xmm1
movaps %xmm1,0x10(%rsp)
mov    %r14,%rdi
call   1050 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
divss  0x0(%rbp),%xmm0
addss  %xmm0,%xmm0
addss  0xe10(%rip),%xmm0
mulss  %xmm0,%xmm0
movaps 0x10(%rsp),%xmm3
mulss  %xmm3,%xmm3
addss  %xmm0,%xmm3
movss  0xdfb(%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    11a0 <piEst+0x60>
jmp    123a <piEst+0xfa>
xorps  %xmm0,%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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