#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 */
struct drand48_data {
  int dummy;
};

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ drand48_r(struct drand48_data *, double *);
int omp_get_thread_num();
double omp_get_wtime();
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand48_r(int, struct drand48_data *);
int time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int i;
  int hits;
  int trials;
  int n_threads;
  int seed;
  double x, y;
  double start, end;
  struct drand48_data buffer;
  if (argc > 2) {
    trials = atoi(argv[1]);
    n_threads = atoi(argv[2]);
  } else {
    printf("Usage: ./pi_omp <trials> <threads>\n");
    return -1;
  }
  omp_set_num_threads(n_threads);
  printf("Trials  : %7d\n", trials);
  printf("Threads : %7d\n", n_threads);
  start = omp_get_wtime();
  hits = 0;
#pragma omp parallel private(i, x, y, seed, buffer) shared(trials)
  {
    seed = 1202107158 + omp_get_thread_num() * time(NULL);
    srand48_r(seed, &buffer);
#pragma omp for reduction(+ : hits)
    for (i = 0; i < trials; i++) {
      drand48_r(&buffer, &x);
      drand48_r(&buffer, &y);
      if (x * x + y * y <= 1.0) {
        hits++;
      }
    }
  }
  end = omp_get_wtime();
  printf("Time    : %7.2fs\n\n", end - start);
  printf("Estimate of pi: %7.5f\n", 4.0 * hits / trials);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11c5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
jmp    11df <main+0x5f>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <main+0x195>
mov    -0x20(%rbp),%edi
call   1070 <omp_set_num_threads@plt>
mov    -0x1c(%rbp),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x54(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
imul   %ecx,%eax
add    $0x47a6b316,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1080 <srand48_r@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c2 <main+0x142>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <drand48_r@plt>
lea    -0x50(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <drand48_r@plt>
movsd  -0x30(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm2
movsd  -0x38(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xd6c(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     12af <main+0x12f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12b4 <main+0x134>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1252 <main+0xd2>
mov    $0x0,%al
call   1040 <omp_get_wtime@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
lea    0xd73(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  0xd0d(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xd5e(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jl     129c <main+0x10c>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <omp_set_num_threads@plt>
lea    0xe41(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3d(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <omp_get_wtime@plt>
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1040 <omp_get_thread_num@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1070 <time@plt>
imul   %eax,%ebx
add    $0x47a6b316,%ebx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1090 <srand48_r@plt>
test   %r14d,%r14d
jle    12af <main+0x11f>
xor    %ebp,%ebp
mov    %rsp,%rbx
lea    0x20(%rsp),%r12
lea    0x18(%rsp),%r13
mov    %r14d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   10b0 <drand48_r@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10b0 <drand48_r@plt>
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd8a(%rip),%xmm1
ucomisd %xmm0,%xmm1
sbb    $0xffffffff,%ebp
add    $0xffffffff,%r15d
jne    1240 <main+0xb0>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  0xd76(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %ebp,%ebp
jmp    12b9 <main+0x129>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    12f4 <main+0x164>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1050 <omp_get_wtime@plt>
subsd  0x10(%rsp),%xmm0
lea    0xd61(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
movsd  0x8(%rsp),%xmm0
divsd  %xmm1,%xmm0
lea    0xd54(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.drand48_data = type { i32 }
@.str = [36 x i8] c"Usage: ./pi_omp <trials> <threads>\0A\00"
@.str.1 = [15 x i8] c"Trials : %7d\0A\00"
@.str.2 = [15 x i8] c"Threads : %7d\0A\00"
@.str.3 = [19 x i8] c"Time : %7.2fs\0A\0A\00"
@.str.4 = [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 i32
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca %struct.drand48_data
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %27
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %9
br label %29
27: ; preds = %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
29: ; preds = %18
%30 = load i32, i32* %9
%31 = call i32 @omp_set_num_threads(i32 %30)
%32 = load i32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = call double (...) @omp_get_wtime()
store double %36, double* %13
store i32 0, i32* %7
%37 = call i32 (...) @omp_get_thread_num()
%38 = call i32 @time(i32* null)
%39 = mul nsw i32 %37, %38
%40 = add nsw i32 1202107158, %39
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = call i32 @srand48_r(i32 %41, %struct.drand48_data* %15)
store i32 0, i32* %6
br label %43
43: ; preds = %61, %29
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %64
47: ; preds = %43
%48 = call i32 @drand48_r(%struct.drand48_data* %15, double* %11)
%49 = call i32 @drand48_r(%struct.drand48_data* %15, double* %12)
%50 = load double, double* %11
%51 = load double, double* %11
%52 = load double, double* %12
%53 = load double, double* %12
%54 = fmul double %52, %53
%55 = call double @llvm.fmuladd.f64(double %50, double %51, double %54)
%56 = fcmp ole double %55, 1.000000e+00
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %60
60: ; preds = %57, %47
br label %61
61: ; preds = %60
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
store i32 %63, i32* %6
br label %43
64: ; preds = %43
%65 = call double (...) @omp_get_wtime()
store double %65, double* %14
%66 = load double, double* %14
%67 = load double, double* %13
%68 = fsub double %66, %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), double %68)
%70 = load i32, i32* %7
%71 = sitofp i32 %70 to double
%72 = fmul double 4.000000e+00, %71
%73 = load i32, i32* %8
%74 = sitofp i32 %73 to double
%75 = fdiv double %72, %74
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), double %75)
store i32 0, i32* %3
br label %77
77: ; preds = %64, %27
%78 = load i32, i32* %3
ret i32 %78
}

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