#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__ in_circle(double, double);
scalar_t__ rand();
double monte_carlo(unsigned int samples) {
  unsigned int count = 0;
  for (unsigned int i = 0; i < samples; ++i) {
    double x = (double)rand() / RAND_MAX;
    double y = (double)rand() / RAND_MAX;
    if (in_circle(x, y)) {
      count += 1;
    }
  }
  return 4.0 * count / samples;
}


x86 assembly (clang, -O0):
<monte_carlo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11b2 <monte_carlo+0x92>
mov    $0x0,%al
call   1040 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e90(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e74(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
call   1030 <in_circle@plt>
cmp    $0x0,%rax
je     119f <monte_carlo+0x7f>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a4 <monte_carlo+0x84>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1139 <monte_carlo+0x19>
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xe3e(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monte_carlo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
test   %edi,%edi
je     1197 <monte_carlo+0x77>
mov    0x2eb3(%rip),%r15
mov    %r14d,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r15),%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  (%r15),%xmm1
movsd  (%rsp),%xmm0
call   1030 <in_circle@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
add    $0xffffffff,%ebx
jne    1140 <monte_carlo+0x20>
mov    %ebp,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0xe6b(%rip),%xmm0
jmp    119b <monte_carlo+0x7b>
xorpd  %xmm0,%xmm0
mov    %r14d,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
define double @monte_carlo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %7
7: ; preds = %28, %1
%8 = load i32, i32* %4
%9 = load i32, i32* %2
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %31
11: ; preds = %7
%12 = call i64 (...) @rand()
%13 = sitofp i64 %12 to double
%14 = load double, double* @RAND_MAX
%15 = fdiv double %13, %14
store double %15, double* %5
%16 = call i64 (...) @rand()
%17 = sitofp i64 %16 to double
%18 = load double, double* @RAND_MAX
%19 = fdiv double %17, %18
store double %19, double* %6
%20 = load double, double* %5
%21 = load double, double* %6
%22 = call i64 @in_circle(double %20, double %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = load i32, i32* %3
%26 = add i32 %25, 1
store i32 %26, i32* %3
br label %27
27: ; preds = %24, %11
br label %28
28: ; preds = %27
%29 = load i32, i32* %4
%30 = add i32 %29, 1
store i32 %30, i32* %4
br label %7
31: ; preds = %7
%32 = load i32, i32* %3
%33 = uitofp i32 %32 to double
%34 = fmul double 4.000000e+00, %33
%35 = load i32, i32* %2
%36 = uitofp i32 %35 to double
%37 = fdiv double %34, %36
ret double %37
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define double @monte_carlo(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %6, label %10
3: ; preds = %10
%4 = uitofp i32 %24 to double
%5 = fmul double %4, 4.000000e+00
br label %6
6: ; preds = %3, %1
%7 = phi double [ 0.000000e+00, %1 ], [ %5, %3 ]
%8 = uitofp i32 %0 to double
%9 = fdiv double %7, %8
ret double %9
10: ; preds = %1, %10
%11 = phi i32 [ %24, %10 ], [ 0, %1 ]
%12 = phi i32 [ %25, %10 ], [ 0, %1 ]
%13 = tail call i64 (...) @rand()
%14 = sitofp i64 %13 to double
%15 = load double, double* @RAND_MAX
%16 = fdiv double %14, %15
%17 = tail call i64 (...) @rand()
%18 = sitofp i64 %17 to double
%19 = load double, double* @RAND_MAX
%20 = fdiv double %18, %19
%21 = tail call i64 @in_circle(double %16, double %20)
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i32
%24 = add i32 %11, %23
%25 = add nuw i32 %12, 1
%26 = icmp eq i32 %25, %0
br i1 %26, label %3, label %10
}