#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;
double RAND_MAX;
int atoi(char *);
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand_r(unsigned int *);
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int i;
  int hits;
  int trials;
  unsigned int seed;
  double x, y;
  clock_t start, end;
  if (argc > 1) {
    trials = atoi(argv[1]);
  } else {
    printf("Usage ./pi_seq <number of trials>\n");
    return -1;
  }
  printf("Trials : %7d\n", trials);
  start = clock();
  hits = 0;
  seed = (unsigned int)time(NULL);
  srand(seed);
  for (i = 0; i < trials; i++) {
    x = (double)rand_r(&seed) / RAND_MAX;
    y = (double)rand_r(&seed) / RAND_MAX;
    if (x * x + y * y <= 1.0) {
      hits++;
    }
  }
  end = clock();
  printf("Time   : %7.2fs\n\n", (double)(end - start) / (double)CLOCKS_PER_SEC);
  printf("Estimate of pi: %7.5f\n", 4.0 * hits / trials);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1195 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    11af <main+0x4f>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <main+0x184>
mov    -0x1c(%rbp),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <srand@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    127f <main+0x11f>
lea    -0x20(%rbp),%rdi
call   1040 <rand_r@plt>
cvtsi2sd %rax,%xmm0
mov    0x2dd2(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <rand_r@plt>
cvtsi2sd %rax,%xmm0
mov    0x2db4(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdaf(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     126c <main+0x10c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1271 <main+0x111>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ed <main+0x8d>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d4a(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
lea    0xd93(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  0xd3e(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xd7d(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     125e <main+0xee>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
lea    0xe76(%rip),%rdi
xor    %r15d,%r15d
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1070 <srand@plt>
test   %r14d,%r14d
jle    1272 <main+0x102>
mov    %rbx,0x10(%rsp)
xor    %ebp,%ebp
lea    0xc(%rsp),%r13
mov    0x2e02(%rip),%rbx
mov    %r14d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%rbx),%xmm0
movsd  %xmm0,(%rsp)
mov    %r13,%rdi
call   1050 <rand_r@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%rbx),%xmm0
mulsd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdca(%rip),%xmm0
ucomisd %xmm1,%xmm0
sbb    $0xffffffff,%ebp
add    $0xffffffff,%r12d
jne    11f0 <main+0x80>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  0xdb6(%rip),%xmm0
movsd  %xmm0,(%rsp)
mov    0x10(%rsp),%rbx
jmp    127b <main+0x10b>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
jmp    12cd <main+0x15d>
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1040 <clock@plt>
sub    %rbx,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    0x2d54(%rip),%rax
xorps  %xmm1,%xmm1
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
lea    0xd77(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
movsd  (%rsp),%xmm0
divsd  %xmm1,%xmm0
lea    0xd6a(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Usage ./pi_seq <number of trials>\0A\00"
@.str.1 = [14 x i8] c"Trials : %7d\0A\00"
@RAND_MAX = global double 0.000000e+00
@.str.2 = [18 x i8] c"Time : %7.2fs\0A\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [23 x i8] c"Estimate of pi: %7.5f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca i64
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %8
br label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %75
23: ; preds = %16
%24 = load i32, i32* %8
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i64 (...) @clock()
store i64 %26, i64* %12
store i32 0, i32* %7
%27 = call i64 @time(i32* null)
%28 = trunc i64 %27 to i32
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = call i32 @srand(i32 %29)
store i32 0, i32* %6
br label %31
31: ; preds = %55, %23
%32 = load i32, i32* %6
%33 = load i32, i32* %8
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %58
35: ; preds = %31
%36 = call i64 @rand_r(i32* %9)
%37 = sitofp i64 %36 to double
%38 = load double, double* @RAND_MAX
%39 = fdiv double %37, %38
store double %39, double* %10
%40 = call i64 @rand_r(i32* %9)
%41 = sitofp i64 %40 to double
%42 = load double, double* @RAND_MAX
%43 = fdiv double %41, %42
store double %43, double* %11
%44 = load double, double* %10
%45 = load double, double* %10
%46 = load double, double* %11
%47 = load double, double* %11
%48 = fmul double %46, %47
%49 = call double @llvm.fmuladd.f64(double %44, double %45, double %48)
%50 = fcmp ole double %49, 1.000000e+00
br i1 %50, label %51, label %54
51: ; preds = %35
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %54
54: ; preds = %51, %35
br label %55
55: ; preds = %54
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %31
58: ; preds = %31
%59 = call i64 (...) @clock()
store i64 %59, i64* %13
%60 = load i64, i64* %13
%61 = load i64, i64* %12
%62 = sub nsw i64 %60, %61
%63 = sitofp i64 %62 to double
%64 = load i64, i64* @CLOCKS_PER_SEC
%65 = sitofp i64 %64 to double
%66 = fdiv double %63, %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %66)
%68 = load i32, i32* %7
%69 = sitofp i32 %68 to double
%70 = fmul double 4.000000e+00, %69
%71 = load i32, i32* %8
%72 = sitofp i32 %71 to double
%73 = fdiv double %70, %72
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %73)
store i32 0, i32* %3
br label %75
75: ; preds = %58, %21
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"Trials : %7d\0A\00"
@RAND_MAX = global double 0.000000e+00
@.str.2 = [18 x i8] c"Time : %7.2fs\0A\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [23 x i8] c"Estimate of pi: %7.5f\0A\00"
@str = [34 x i8] c"Usage ./pi_seq <number of trials>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i64 (...) @clock()
%12 = tail call i64 @time(i32* null)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %3
%14 = tail call i32 @srand(i32 %13)
%15 = icmp sgt i32 %9, 0
br i1 %15, label %18, label %39
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %51
18: ; preds = %6, %18
%19 = phi i32 [ %34, %18 ], [ 0, %6 ]
%20 = phi i32 [ %33, %18 ], [ 0, %6 ]
%21 = call i64 @rand_r(i32* %3)
%22 = sitofp i64 %21 to double
%23 = load double, double* @RAND_MAX
%24 = fdiv double %22, %23
%25 = call i64 @rand_r(i32* %3)
%26 = sitofp i64 %25 to double
%27 = load double, double* @RAND_MAX
%28 = fdiv double %26, %27
%29 = fmul double %28, %28
%30 = call double @llvm.fmuladd.f64(double %24, double %24, double %29)
%31 = fcmp ole double %30, 1.000000e+00
%32 = zext i1 %31 to i32
%33 = add nuw nsw i32 %20, %32
%34 = add nuw nsw i32 %19, 1
%35 = icmp eq i32 %34, %9
br i1 %35, label %36, label %18
36: ; preds = %18
%37 = sitofp i32 %33 to double
%38 = fmul double %37, 4.000000e+00
br label %39
39: ; preds = %36, %6
%40 = phi double [ 0.000000e+00, %6 ], [ %38, %36 ]
%41 = call i64 (...) @clock()
%42 = sub nsw i64 %41, %11
%43 = sitofp i64 %42 to double
%44 = load i64, i64* @CLOCKS_PER_SEC
%45 = sitofp i64 %44 to double
%46 = fdiv double %43, %45
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %46)
%48 = sitofp i32 %9 to double
%49 = fdiv double %40, %48
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %49)
br label %51
51: ; preds = %39, %16
%52 = phi i32 [ 0, %39 ], [ -1, %16 ]
ret i32 %52
}