#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 scalar_t__ clock_t;

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, ...);
long long rand();
int /*<<< orphan*/ scanf(char *, long long *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  long long count = 0, max = 0, samples = 0, i = 0, x = 0, y = 0;
  long double pi = 0.0;
  clock_t start, stop;
  printf("Please enter the sample size. Doubling the sample size quadruples "
         "the computation time.\n");
  scanf("%lld", &samples);
  max = samples * samples;
  start = clock();
  for (i = 0; i < max; i++) {
    x = rand() % samples + 1;
    y = rand() % samples + 1;
    if (x * x + y * y <= max)
      count++;
  }
  pi = (4 * count) / (long double)max;
  stop = clock();
  printf("Sample size is %lld.\n", samples);
  printf("Approximate value of Pi is %7.6Lf.\n", pi);
  printf("Time taken is %f seconds.\n",
         ((float)stop - (float)start) / (float)CLOCKS_PER_SEC);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
fldz
fstpt  -0x50(%rbp)
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8d(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x20(%rbp),%rax
imul   -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    126e <main+0x10e>
mov    $0x0,%al
call   1080 <rand@plt>
cqto
idivq  -0x20(%rbp)
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
cqto
idivq  -0x20(%rbp)
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    -0x30(%rbp),%rax
imul   -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
imul   -0x38(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jg     1258 <main+0xf8>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    125d <main+0xfd>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11f5 <main+0x95>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x68(%rbp)
fildll -0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
fildll -0x70(%rbp)
fdivrp %st,%st(1)
fstpt  -0x50(%rbp)
xor    %eax,%eax
mov    %al,-0x71(%rbp)
call   1030 <clock@plt>
mov    %rax,%rcx
mov    -0x71(%rbp),%al
mov    %rcx,-0x60(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
fldt   -0x50(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cvtsi2ssq -0x60(%rbp),%xmm0
cvtsi2ssq -0x58(%rbp),%xmm1
subss  %xmm1,%xmm0
mov    0x2d0a(%rip),%rax
cvtsi2ssq (%rax),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xda6(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
xor    %r15d,%r15d
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
movq   $0x0,0x18(%rsp)
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
lea    0xe56(%rip),%rdi
lea    0x18(%rsp),%rsi
call   1080 <scanf@plt>
mov    0x18(%rsp),%r12
imul   %r12,%r12
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%r14
fldz
test   %r12,%r12
je     1265 <main+0xf5>
fstp   %st(0)
mov    %r12,%r13
jmp    1201 <main+0x91>
nopl   0x0(%rax)
cqto
idiv   %rcx
add    $0x1,%rdx
imul   %rbx,%rbx
imul   %rdx,%rdx
add    %rbx,%rdx
cmp    %rdx,%r12
sbb    $0xffffffffffffffff,%r15
add    $0xffffffffffffffff,%r13
je     1258 <main+0xe8>
xor    %eax,%eax
call   1090 <rand@plt>
mov    0x18(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1230 <main+0xc0>
cqto
idiv   %rcx
mov    %rdx,%rbx
jmp    1236 <main+0xc6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edx,%edx
div    %ecx
mov    %edx,%ebx
add    $0x1,%rbx
xor    %eax,%eax
call   1090 <rand@plt>
mov    0x18(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    11e0 <main+0x70>
xor    %edx,%edx
div    %ecx
jmp    11e5 <main+0x75>
shl    $0x2,%r15
mov    %r15,0x28(%rsp)
fildll 0x28(%rsp)
mov    %r12,0x20(%rsp)
fildll 0x20(%rsp)
fdivrp %st,%st(1)
fstpt  0x34(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
mov    0x18(%rsp),%rsi
lea    0xd7b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
fldt   0x34(%rsp)
fstpt  (%rsp)
lea    0xd7c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cvtsi2ss %rbx,%xmm0
cvtsi2ss %r14,%xmm1
mov    0x2d31(%rip),%rax
cvtsi2ssq (%rax),%xmm2
subss  %xmm1,%xmm0
divss  %xmm2,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd70(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [89 x i8] c"Please enter the sample size. Doubling the sample size quadruples the computation time.\0A\00"
@.str.1 = [5 x i8] c"%lld\00"
@.str.2 = [22 x i8] c"Sample size is %lld.\0A\00"
@.str.3 = [36 x i8] c"Approximate value of Pi is %7.6Lf.\0A\00"
@.str.4 = [27 x i8] c"Time taken is %f seconds.\0A\00"
@CLOCKS_PER_SEC = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca x86_fp80
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %1
%11 = call i32 @time(i32* null)
%12 = call i32 @srand(i32 %11)
store i64 0, i64* %2
store i64 0, i64* %3
store i64 0, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
store x86_fp80 0xK00000000000000000000, x86_fp80* %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %4)
%15 = load i64, i64* %4
%16 = load i64, i64* %4
%17 = mul nsw i64 %15, %16
store i64 %17, i64* %3
%18 = call i64 (...) @clock()
store i64 %18, i64* %9
store i64 0, i64* %5
br label %19
19: ; preds = %45, %0
%20 = load i64, i64* %5
%21 = load i64, i64* %3
%22 = icmp slt i64 %20, %21
br i1 %22, label %23, label %48
23: ; preds = %19
%24 = call i64 (...) @rand()
%25 = load i64, i64* %4
%26 = srem i64 %24, %25
%27 = add nsw i64 %26, 1
store i64 %27, i64* %6
%28 = call i64 (...) @rand()
%29 = load i64, i64* %4
%30 = srem i64 %28, %29
%31 = add nsw i64 %30, 1
store i64 %31, i64* %7
%32 = load i64, i64* %6
%33 = load i64, i64* %6
%34 = mul nsw i64 %32, %33
%35 = load i64, i64* %7
%36 = load i64, i64* %7
%37 = mul nsw i64 %35, %36
%38 = add nsw i64 %34, %37
%39 = load i64, i64* %3
%40 = icmp sle i64 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %23
%42 = load i64, i64* %2
%43 = add nsw i64 %42, 1
store i64 %43, i64* %2
br label %44
44: ; preds = %41, %23
br label %45
45: ; preds = %44
%46 = load i64, i64* %5
%47 = add nsw i64 %46, 1
store i64 %47, i64* %5
br label %19
48: ; preds = %19
%49 = load i64, i64* %2
%50 = mul nsw i64 4, %49
%51 = sitofp i64 %50 to x86_fp80
%52 = load i64, i64* %3
%53 = sitofp i64 %52 to x86_fp80
%54 = fdiv x86_fp80 %51, %53
store x86_fp80 %54, x86_fp80* %8
%55 = call i64 (...) @clock()
store i64 %55, i64* %10
%56 = load i64, i64* %4
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %56)
%58 = load x86_fp80, x86_fp80* %8
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), x86_fp80 %58)
%60 = load i64, i64* %10
%61 = sitofp i64 %60 to float
%62 = load i64, i64* %9
%63 = sitofp i64 %62 to float
%64 = fsub float %61, %63
%65 = load i64, i64* @CLOCKS_PER_SEC
%66 = sitofp i64 %65 to float
%67 = fdiv float %64, %66
%68 = fpext float %67 to double
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %68)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%lld\00"
@.str.2 = [22 x i8] c"Sample size is %lld.\0A\00"
@.str.3 = [36 x i8] c"Approximate value of Pi is %7.6Lf.\0A\00"
@.str.4 = [27 x i8] c"Time taken is %f seconds.\0A\00"
@CLOCKS_PER_SEC = global i64 0
@str = [88 x i8] c"Please enter the sample size. Doubling the sample size quadruples the computation time.\00"
define i32 @main() {
%1 = alloca i64
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([88 x i8], [88 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%7 = load i64, i64* %1
%8 = mul i64 %7, %7
%9 = call i64 (...) @clock()
%10 = icmp eq i64 %8, 0
br i1 %10, label %33, label %11
11: ; preds = %0, %11
%12 = phi i64 [ %27, %11 ], [ 0, %0 ]
%13 = phi i64 [ %28, %11 ], [ 0, %0 ]
%14 = call i64 (...) @rand()
%15 = load i64, i64* %1
%16 = srem i64 %14, %15
%17 = add nsw i64 %16, 1
%18 = call i64 (...) @rand()
%19 = load i64, i64* %1
%20 = srem i64 %18, %19
%21 = add nsw i64 %20, 1
%22 = mul nsw i64 %17, %17
%23 = mul nsw i64 %21, %21
%24 = add nuw nsw i64 %23, %22
%25 = icmp ule i64 %24, %8
%26 = zext i1 %25 to i64
%27 = add nuw nsw i64 %12, %26
%28 = add nuw i64 %13, 1
%29 = icmp eq i64 %28, %8
br i1 %29, label %30, label %11
30: ; preds = %11
%31 = shl i64 %27, 2
%32 = sitofp i64 %31 to x86_fp80
br label %33
33: ; preds = %30, %0
%34 = phi x86_fp80 [ 0xK00000000000000000000, %0 ], [ %32, %30 ]
%35 = sitofp i64 %8 to x86_fp80
%36 = fdiv x86_fp80 %34, %35
%37 = call i64 (...) @clock()
%38 = load i64, i64* %1
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), x86_fp80 %36)
%41 = sitofp i64 %37 to float
%42 = sitofp i64 %9 to float
%43 = fsub float %41, %42
%44 = load i64, i64* @CLOCKS_PER_SEC
%45 = sitofp i64 %44 to float
%46 = fdiv float %43, %45
%47 = fpext float %46 to double
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %47)
ret i32 0
}