#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 */
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
double func(double);
int getopt(int, char **, char *);
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int recs = 100000000;
  int num_threads = 1;
  const char *name = argv[0];
  int c;
  while ((c = getopt(argc, argv, "n:t:")) != -1) {
    switch (c) {
    case 'n':
      recs = atoi(optarg);
      break;
    case 't':
      num_threads = atoi(optarg);
      break;
    case '?':
    default:
      fprintf(stderr,
              "Usage: %s -n [NUMBER_OF_RECTANGLES] -t [OMP_NUM_THREADS]\n",
              name);
      return -1;
    }
  }
  argc - +optind;
  argv += optind;
  double width;
  width = 1.0 / recs;
  int first = 0, last = recs;
  double sum = 0.0;
  int i = 0;
  omp_set_num_threads(num_threads);
#pragma omp parallel for reduction(+:sum) shared(first,last,width) private(i)
  for (i = first; i < last; i++) {
    sum += func(width * i) * width * 4.0;
  }
  printf(" --- %s --- \n", name);
  printf("Number of processes: %d\n", 1);
  printf("Threads per process: %d\n", num_threads);
  printf("Rectangles         : %d\n", recs);
  printf("pi is approximately: %f\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x5f5e100,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe73(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     123f <main+0xdf>
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
sub    $0x3f,%eax
je     1210 <main+0xb0>
jmp    11c2 <main+0x62>
mov    -0x48(%rbp),%eax
sub    $0x6e,%eax
je     11e4 <main+0x84>
jmp    11d3 <main+0x73>
mov    -0x48(%rbp),%eax
sub    $0x74,%eax
je     11fa <main+0x9a>
jmp    1215 <main+0xb5>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    123a <main+0xda>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    123a <main+0xda>
jmp    1215 <main+0xb5>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdec(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <main+0x1e7>
jmp    118f <main+0x2f>
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  0xd99(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <omp_set_num_threads@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12e6 <main+0x186>
movsd  -0x30(%rbp),%xmm0
cvtsi2sdl -0x44(%rbp),%xmm1
mulsd  %xmm1,%xmm0
call   1050 <func@plt>
mulsd  -0x30(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
movsd  0xd3d(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    129a <main+0x13a>
mov    -0x20(%rbp),%rsi
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd5e(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsd  -0x40(%rbp),%xmm0
lea    0xd6f(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
mov    (%rsi),%rax
mov    %rax,0x10(%rsp)
mov    $0x5f5e100,%r12d
mov    $0x1,%r15d
lea    0xe82(%rip),%r13
mov    0x2e63(%rip),%r14
jmp    11ab <main+0x4b>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
call   1080 <atoi@plt>
mov    %eax,%r12d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1070 <getopt@plt>
cmp    $0x6e,%eax
je     11a0 <main+0x40>
cmp    $0xffffffff,%eax
je     11d8 <main+0x78>
cmp    $0x74,%eax
jne    1268 <main+0x108>
mov    (%r14),%edi
call   1080 <atoi@plt>
mov    %eax,%r15d
jmp    11ab <main+0x4b>
mov    %r15d,%ebp
mov    %r15d,%edi
call   1060 <omp_set_num_threads@plt>
test   %r12d,%r12d
jle    1289 <main+0x129>
cvtsi2sd %r12d,%xmm0
movsd  0xe07(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
xorpd  %xmm1,%xmm1
mov    %r12d,%ebx
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    0x10(%rsp),%r15
nopl   0x0(%rax)
movsd  %xmm1,0x20(%rsp)
movsd  0x18(%rsp),%xmm0
mulsd  %xmm1,%xmm0
call   1050 <func@plt>
mulsd  0x18(%rsp),%xmm0
mulsd  0xdc5(%rip),%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x20(%rsp),%xmm1
addsd  0xd9f(%rip),%xmm1
add    $0xffffffff,%ebx
jne    1220 <main+0xc0>
jmp    1298 <main+0x138>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    0x10(%rsp),%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12f3 <main+0x193>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    0x10(%rsp),%r15
lea    0xdb0(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdab(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdb1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdba(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xdc2(%rip),%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"n:t:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [58 x i8] c"Usage: %s -n [NUMBER_OF_RECTANGLES] -t [OMP_NUM_THREADS]\0A\00"
@optind = global i32 0
@.str.2 = [14 x i8] c" --- %s --- \0A\00"
@.str.3 = [25 x i8] c"Number of processes: %d\0A\00"
@.str.4 = [25 x i8] c"Threads per process: %d\0A\00"
@.str.5 = [25 x i8] c"Rectangles : %d\0A\00"
@.str.6 = [25 x i8] c"pi is approximately: %f\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 i8*
%9 = alloca i32
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
%13 = alloca double
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 100000000, i32* %6
store i32 1, i32* %7
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
store i8* %17, i8** %8
br label %18
18: ; preds = %36, %2
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @getopt(i32 %19, i8** %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %21, i32* %9
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %37
23: ; preds = %18
%24 = load i32, i32* %9
switch i32 %24, label %32 [
i32 110, label %25
i32 116, label %28
i32 63, label %31
]
25: ; preds = %23
%26 = load i32, i32* @optarg
%27 = call i32 @atoi(i32 %26)
store i32 %27, i32* %6
br label %36
28: ; preds = %23
%29 = load i32, i32* @optarg
%30 = call i32 @atoi(i32 %29)
store i32 %30, i32* %7
br label %36
31: ; preds = %23
br label %32
32: ; preds = %23, %31
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %8
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 -1, i32* %3
br label %79
36: ; preds = %28, %25
br label %18
37: ; preds = %18
%38 = load i32, i32* %4
%39 = load i32, i32* @optind
%40 = sub nsw i32 %38, %39
%41 = load i32, i32* @optind
%42 = load i8**, i8*** %5
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8*, i8** %42, i64 %43
store i8** %44, i8*** %5
%45 = load i32, i32* %6
%46 = sitofp i32 %45 to double
%47 = fdiv double 1.000000e+00, %46
store double %47, double* %10
store i32 0, i32* %11
%48 = load i32, i32* %6
store i32 %48, i32* %12
store double 0.000000e+00, double* %13
store i32 0, i32* %14
%49 = load i32, i32* %7
%50 = call i32 @omp_set_num_threads(i32 %49)
%51 = load i32, i32* %11
store i32 %51, i32* %14
br label %52
52: ; preds = %66, %37
%53 = load i32, i32* %14
%54 = load i32, i32* %12
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %69
56: ; preds = %52
%57 = load double, double* %10
%58 = load i32, i32* %14
%59 = sitofp i32 %58 to double
%60 = fmul double %57, %59
%61 = call double @func(double %60)
%62 = load double, double* %10
%63 = fmul double %61, %62
%64 = load double, double* %13
%65 = call double @llvm.fmuladd.f64(double %63, double 4.000000e+00, double %64)
store double %65, double* %13
br label %66
66: ; preds = %56
%67 = load i32, i32* %14
%68 = add nsw i32 %67, 1
store i32 %68, i32* %14
br label %52
69: ; preds = %52
%70 = load i8*, i8** %8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 1)
%73 = load i32, i32* %7
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %73)
%75 = load i32, i32* %6
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %75)
%77 = load double, double* %13
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), double %77)
store i32 0, i32* %3
br label %79
79: ; preds = %69, %32
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [5 x i8] c"n:t:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [58 x i8] c"Usage: %s -n [NUMBER_OF_RECTANGLES] -t [OMP_NUM_THREADS]\0A\00"
@optind = global i32 0
@.str.2 = [14 x i8] c" --- %s --- \0A\00"
@.str.3 = [25 x i8] c"Number of processes: %d\0A\00"
@.str.4 = [25 x i8] c"Threads per process: %d\0A\00"
@.str.5 = [25 x i8] c"Rectangles : %d\0A\00"
@.str.6 = [25 x i8] c"pi is approximately: %f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i8*, i8** %1
br label %4
4: ; preds = %13, %2
%5 = phi i32 [ %15, %13 ], [ 1, %2 ]
%6 = phi i32 [ %8, %13 ], [ 100000000, %2 ]
br label %7
7: ; preds = %4, %10
%8 = phi i32 [ %12, %10 ], [ %6, %4 ]
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %16 [
i32 -1, label %19
i32 110, label %10
i32 116, label %13
]
10: ; preds = %7
%11 = load i32, i32* @optarg
%12 = tail call i32 @atoi(i32 %11)
br label %7
13: ; preds = %7
%14 = load i32, i32* @optarg
%15 = tail call i32 @atoi(i32 %14)
br label %4
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %41
19: ; preds = %7
%20 = sitofp i32 %8 to double
%21 = fdiv double 1.000000e+00, %20
%22 = tail call i32 @omp_set_num_threads(i32 %5)
%23 = icmp sgt i32 %8, 0
br i1 %23, label %24, label %34
24: ; preds = %19, %24
%25 = phi i32 [ %32, %24 ], [ 0, %19 ]
%26 = phi double [ %31, %24 ], [ 0.000000e+00, %19 ]
%27 = sitofp i32 %25 to double
%28 = fmul double %21, %27
%29 = tail call double @func(double %28)
%30 = fmul double %21, %29
%31 = tail call double @llvm.fmuladd.f64(double %30, double 4.000000e+00, double %26)
%32 = add nuw nsw i32 %25, 1
%33 = icmp eq i32 %32, %8
br i1 %33, label %34, label %24
34: ; preds = %24, %19
%35 = phi double [ 0.000000e+00, %19 ], [ %31, %24 ]
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 1)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %5)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %8)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), double %35)
br label %41
41: ; preds = %34, %16
%42 = phi i32 [ -1, %16 ], [ 0, %34 ]
ret i32 %42
}