#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 rand();
long long unsigned int monte_carlo_pi(unsigned int n) {
  long long unsigned int in = 0, i;
  double x, y, d;
  for (i = 0; i < n; i++) {
    x = ((rand() % 1000000) / 500000.0) - 1;
    y = ((rand() % 1000000) / 500000.0) - 1;
    d = ((x * x) + (y * y));
    if (d <= 1)
      in += 1;
  }
  return in;
}


x86 assembly (clang, -O0):
<monte_carlo_pi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
cmp    %rcx,%rax
jae    11f1 <monte_carlo_pi+0xe1>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm0
movsd  0xeb2(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe9e(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm0
movsd  0xe82(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe6e(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe3c(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jb     11db <monte_carlo_pi+0xcb>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11e0 <monte_carlo_pi+0xd0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    112b <monte_carlo_pi+0x1b>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monte_carlo_pi>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     11e2 <monte_carlo_pi+0xd2>
mov    %edi,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
cltq
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  0xe9c(%rip),%xmm0
divsd  %xmm0,%xmm1
movsd  0xe98(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xor    %eax,%eax
call   1030 <rand@plt>
cltq
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0xe53(%rip),%xmm0
addsd  0xe53(%rip),%xmm0
mulsd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe42(%rip),%xmm0
ucomisd %xmm1,%xmm0
sbb    $0xffffffffffffffff,%rbx
add    $0xffffffffffffffff,%r14
jne    1130 <monte_carlo_pi+0x20>
jmp    11e4 <monte_carlo_pi+0xd4>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @monte_carlo_pi(i32 %0) {
%2 = zext i32 %0 to i64
%3 = icmp eq i32 %0, 0
br i1 %3, label %24, label %4
4: ; preds = %1, %4
%5 = phi i64 [ %21, %4 ], [ 0, %1 ]
%6 = phi i64 [ %22, %4 ], [ 0, %1 ]
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 1000000
%9 = sitofp i32 %8 to double
%10 = fdiv double %9, 5.000000e+05
%11 = fadd double %10, -1.000000e+00
%12 = tail call i32 (...) @rand()
%13 = srem i32 %12, 1000000
%14 = sitofp i32 %13 to double
%15 = fdiv double %14, 5.000000e+05
%16 = fadd double %15, -1.000000e+00
%17 = fmul double %16, %16
%18 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %17)
%19 = fcmp ole double %18, 1.000000e+00
%20 = zext i1 %19 to i64
%21 = add i64 %5, %20
%22 = add nuw nsw i64 %6, 1
%23 = icmp eq i64 %22, %2
br i1 %23, label %24, label %4
24: ; preds = %4, %1
%25 = phi i64 [ 0, %1 ], [ %21, %4 ]
ret i64 %25
}