#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;
scalar_t__ rand_r(unsigned int *);
int time(int /*<<< orphan*/ *);
unsigned long long pi_montecarlo(int threadnum, unsigned long long n) {
  unsigned long long i;
  double x, y;
  unsigned int seed = time(NULL) + threadnum;
  unsigned long long accum = 0;
  for (i = 0; i < n; i++) {
    x = (double)rand_r(&seed) / RAND_MAX;
    y = (double)rand_r(&seed) / RAND_MAX;
    if (x * x + y * y < 1) {
      accum++;
    }
  }
  return accum;
}


x86 assembly (clang, -O0):
<pi_montecarlo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
add    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11e8 <pi_montecarlo+0xc8>
lea    -0x2c(%rbp),%rdi
call   1030 <rand_r@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e77(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <rand_r@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e59(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe44(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11d2 <pi_montecarlo+0xb2>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11d7 <pi_montecarlo+0xb7>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114e <pi_montecarlo+0x2e>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pi_montecarlo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <time@plt>
add    %ebp,%eax
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     11bc <pi_montecarlo+0x9c>
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
mov    0x2e97(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0x0(%rbp),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r15,%rdi
call   1030 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0x0(%rbp),%xmm0
mulsd  %xmm0,%xmm0
movsd  0x10(%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
xor    %eax,%eax
movsd  0xe54(%rip),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
add    %rax,%rbx
add    $0xffffffffffffffff,%r14
jne    1160 <pi_montecarlo+0x40>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
define i64 @pi_montecarlo(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
%10 = call i32 @time(i32* null)
%11 = load i32, i32* %3
%12 = add nsw i32 %10, %11
store i32 %12, i32* %8
store i64 0, i64* %9
store i64 0, i64* %5
br label %13
13: ; preds = %37, %2
%14 = load i64, i64* %5
%15 = load i64, i64* %4
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %40
17: ; preds = %13
%18 = call i64 @rand_r(i32* %8)
%19 = sitofp i64 %18 to double
%20 = load double, double* @RAND_MAX
%21 = fdiv double %19, %20
store double %21, double* %6
%22 = call i64 @rand_r(i32* %8)
%23 = sitofp i64 %22 to double
%24 = load double, double* @RAND_MAX
%25 = fdiv double %23, %24
store double %25, double* %7
%26 = load double, double* %6
%27 = load double, double* %6
%28 = load double, double* %7
%29 = load double, double* %7
%30 = fmul double %28, %29
%31 = call double @llvm.fmuladd.f64(double %26, double %27, double %30)
%32 = fcmp olt double %31, 1.000000e+00
br i1 %32, label %33, label %36
33: ; preds = %17
%34 = load i64, i64* %9
%35 = add i64 %34, 1
store i64 %35, i64* %9
br label %36
36: ; preds = %33, %17
br label %37
37: ; preds = %36
%38 = load i64, i64* %5
%39 = add i64 %38, 1
store i64 %39, i64* %5
br label %13
40: ; preds = %13
%41 = load i64, i64* %9
ret i64 %41
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define i64 @pi_montecarlo(i32 %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @time(i32* null)
%6 = add nsw i32 %5, %0
store i32 %6, i32* %3
%7 = icmp eq i64 %1, 0
br i1 %7, label %26, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %23, %8 ], [ 0, %2 ]
%10 = phi i64 [ %24, %8 ], [ 0, %2 ]
%11 = call i64 @rand_r(i32* %3)
%12 = sitofp i64 %11 to double
%13 = load double, double* @RAND_MAX
%14 = fdiv double %12, %13
%15 = call i64 @rand_r(i32* %3)
%16 = sitofp i64 %15 to double
%17 = load double, double* @RAND_MAX
%18 = fdiv double %16, %17
%19 = fmul double %18, %18
%20 = call double @llvm.fmuladd.f64(double %14, double %14, double %19)
%21 = fcmp olt double %20, 1.000000e+00
%22 = zext i1 %21 to i64
%23 = add i64 %9, %22
%24 = add nuw i64 %10, 1
%25 = icmp eq i64 %24, %1
br i1 %25, label %26, label %8
26: ; preds = %8, %2
%27 = phi i64 [ 0, %2 ], [ %23, %8 ]
ret i64 %27
}