#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, float *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand();
int /*<<< orphan*/ scanf(char *, int *);
int sinf(float);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  float closestLineDistance;
  float crossingAngle;
  float verticalLineSpacing;
  int linesCrossed = 0;
  int needlesDropped = 0;
  float approximatePiValue;
  float crossChecker;
  int needleDropSample = 0;
  FILE *filepointer;
  srand(time(NULL));
  verticalLineSpacing = (2 * 1);
  printf("\n\nEnter the number of needles you wish to drop: ");
  scanf("%d", &needleDropSample);
  filepointer = fopen("rn_GNUC.txt", "r");
  while (needlesDropped < needleDropSample) {
    fscanf(filepointer, "%e", &closestLineDistance);
    needlesDropped++;
    crossingAngle = (3.14159265 / 2.0) * ((float)rand() / (float)RAND_MAX);
    crossChecker = (1 / 2.0) * sinf(crossingAngle);
    if (closestLineDistance <= crossChecker) {
      linesCrossed++;
    }
  }
  fclose(filepointer);
  approximatePiValue =
      (2 * 1 * needlesDropped) / (verticalLineSpacing * linesCrossed);
  printf("\n\nThe number of needles dropped were: %d", needlesDropped);
  printf("\n\nThe number of lines crossed were: %d", linesCrossed);
  printf("\n\nThe approximate value of Pi based on the above data is: %f\n\n",
         approximatePiValue);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movss  0xe34(%rip),%xmm0
movss  %xmm0,-0x10(%rbp)
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe9a(%rip),%rdi
lea    -0x24(%rbp),%rsi
call   1090 <scanf@plt>
lea    0xe53(%rip),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12a8 <main+0x118>
mov    -0x30(%rbp),%rdi
lea    0xe3a(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
cvtsi2ss %rax,%xmm0
mov    0x2da6(%rip),%rax
cvtsi2ssq (%rax),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xdb9(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
call   1030 <sinf@plt>
cvtsi2sd %eax,%xmm1
movsd  0xd8e(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x8(%rbp),%xmm1
movss  -0x20(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jb     12a3 <main+0x113>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1206 <main+0x76>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
cvtsi2ss %eax,%xmm0
movss  -0x10(%rbp),%xmm1
cvtsi2ssl -0x14(%rbp),%xmm2
mulss  %xmm2,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xda6(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0xc(%rsp)
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xead(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1090 <scanf@plt>
lea    0xe65(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
cmpl   $0x0,0xc(%rsp)
mov    $0x0,%ebp
jle    1273 <main+0xe3>
lea    0xe51(%rip),%r15
lea    0x14(%rsp),%r12
mov    0x2dde(%rip),%r13
xor    %ebp,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <fscanf@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   10b0 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
xorps  %xmm1,%xmm1
cvtsi2ssq 0x0(%r13),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xdb7(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <sinf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xda7(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
ucomiss 0x14(%rsp),%xmm0
sbb    $0xffffffff,%ebp
cmp    0xc(%rsp),%ebx
jl     1210 <main+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    (%rbx,%rbx,1),%eax
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
addss  %xmm0,%xmm0
divss  %xmm0,%xmm1
movss  %xmm1,0x10(%rsp)
lea    0xdb1(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdca(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movss  0x10(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdd7(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"\0A\0AEnter the number of needles you wish to drop: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [12 x i8] c"rn_GNUC.txt\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%e\00"
@RAND_MAX = global i64 0
@.str.5 = [41 x i8] c"\0A\0AThe number of needles dropped were: %d\00"
@.str.6 = [39 x i8] c"\0A\0AThe number of lines crossed were: %d\00"
@.str.7 = [63 x i8] c"\0A\0AThe approximate value of Pi based on the above data is: %f\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca float
%3 = alloca float
%4 = alloca float
%5 = alloca i32
%6 = alloca i32
%7 = alloca float
%8 = alloca float
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %9
%11 = call i32 @time(i32* null)
%12 = call i32 @srand(i32 %11)
store float 2.000000e+00, float* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%15 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %10
br label %16
16: ; preds = %44, %0
%17 = load i32, i32* %6
%18 = load i32, i32* %9
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %45
20: ; preds = %16
%21 = load i32*, i32** %10
%22 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), float* %2)
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = call i64 (...) @rand()
%26 = sitofp i64 %25 to float
%27 = load i64, i64* @RAND_MAX
%28 = sitofp i64 %27 to float
%29 = fdiv float %26, %28
%30 = fpext float %29 to double
%31 = fmul double 0x3FF921FB53C8D4F1, %30
%32 = fptrunc double %31 to float
store float %32, float* %3
%33 = load float, float* %3
%34 = call i32 @sinf(float %33)
%35 = sitofp i32 %34 to double
%36 = fmul double 5.000000e-01, %35
%37 = fptrunc double %36 to float
store float %37, float* %8
%38 = load float, float* %2
%39 = load float, float* %8
%40 = fcmp ole float %38, %39
br i1 %40, label %41, label %44
41: ; preds = %20
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %44
44: ; preds = %41, %20
br label %16
45: ; preds = %16
%46 = load i32*, i32** %10
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %6
%49 = mul nsw i32 2, %48
%50 = sitofp i32 %49 to float
%51 = load float, float* %4
%52 = load i32, i32* %5
%53 = sitofp i32 %52 to float
%54 = fmul float %51, %53
%55 = fdiv float %50, %54
store float %55, float* %7
%56 = load i32, i32* %6
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %5
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %58)
%60 = load float, float* %7
%61 = fpext float %60 to double
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.7, i64 0, i64 0), double %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [49 x i8] c"\0A\0AEnter the number of needles you wish to drop: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [12 x i8] c"rn_GNUC.txt\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%e\00"
@RAND_MAX = global i64 0
@.str.5 = [41 x i8] c"\0A\0AThe number of needles dropped were: %d\00"
@.str.6 = [39 x i8] c"\0A\0AThe number of lines crossed were: %d\00"
@.str.7 = [63 x i8] c"\0A\0AThe approximate value of Pi based on the above data is: %f\0A\0A\00"
define i32 @main() {
%1 = alloca float
%2 = alloca i32
%3 = bitcast float* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = tail call i32 @time(i32* null)
%6 = tail call i32 @srand(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %0, %12
%13 = phi i32 [ %32, %12 ], [ 0, %0 ]
%14 = phi i32 [ %16, %12 ], [ 0, %0 ]
%15 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), float* %1)
%16 = add nuw nsw i32 %14, 1
%17 = call i64 (...) @rand()
%18 = sitofp i64 %17 to float
%19 = load i64, i64* @RAND_MAX
%20 = sitofp i64 %19 to float
%21 = fdiv float %18, %20
%22 = fpext float %21 to double
%23 = fmul double %22, 0x3FF921FB53C8D4F1
%24 = fptrunc double %23 to float
%25 = call i32 @sinf(float %24)
%26 = sitofp i32 %25 to double
%27 = fmul double %26, 5.000000e-01
%28 = fptrunc double %27 to float
%29 = load float, float* %1
%30 = fcmp ole float %29, %28
%31 = zext i1 %30 to i32
%32 = add nuw nsw i32 %13, %31
%33 = load i32, i32* %2
%34 = icmp slt i32 %16, %33
br i1 %34, label %12, label %35
35: ; preds = %12, %0
%36 = phi i32 [ 0, %0 ], [ %16, %12 ]
%37 = phi i32 [ 0, %0 ], [ %32, %12 ]
%38 = call i32 @fclose(i32* %9)
%39 = shl nuw nsw i32 %36, 1
%40 = sitofp i32 %39 to float
%41 = sitofp i32 %37 to float
%42 = fmul float %41, 2.000000e+00
%43 = fdiv float %40, %42
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%46 = fpext float %43 to double
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.7, i64 0, i64 0), double %46)
ret i32 0
}