#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 omp_get_thread_num();
int /*<<< orphan*/ printf(char *, int, ...);
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  double factor = 1.0;
  double sum = 0.0;
  double result;
  int n = 1000000;
  int x = 5;
  int thread_count = 1;
  if (argc == 2) {
    thread_count = strtol(argv[1], NULL, 10);
  }
#pragma omp parallel for num_threads(thread_count) reduction(+: sum) private(factor)
  for (int k = 0; k < n; k++) {
    factor = (k % 2 == 0) ? 1.0 : -1.0;
    sum += factor / (2 * k + 1);
  }
  result = 4.0 * sum;
  printf("sum = %f\n", result);
#pragma omp parallel num_threads(thread_count) default(none) private(x)
  {
    int my_rank = omp_get_thread_num();
    x = 1;
    printf("Thread %d > before initialization, x = %d\n", my_rank, x);
    x = my_rank;
    printf("Thread %d > after initialization, x = %d\n", my_rank, x);
  }
  printf("After parallel block, x = %d\n", x);
  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)
movsd  0xeb2(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0xf4240,-0x2c(%rbp)
movl   $0x5,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1193 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1216 <main+0xe6>
mov    -0x38(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
movsd  0xe47(%rip),%xmm0
movsd  0xe4f(%rip),%xmm1
movsd  %xmm1,-0x50(%rbp)
cmp    $0x0,%edx
movsd  %xmm0,-0x48(%rbp)
je     11de <main+0xae>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    -0x38(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    119a <main+0x6a>
movsd  0xdea(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
cvttsd2si -0x28(%rbp),%esi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%esi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    1146 <main+0x16>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
xorpd  %xmm0,%xmm0
mov    $0x3,%eax
movsd  0xea9(%rip),%xmm1
movsd  0xea9(%rip),%xmm2
nop
lea    -0x2(%rax),%ecx
xorps  %xmm3,%xmm3
cvtsi2sd %ecx,%xmm3
movapd %xmm1,%xmm4
divsd  %xmm3,%xmm4
addsd  %xmm0,%xmm4
xorps  %xmm3,%xmm3
cvtsi2sd %eax,%xmm3
movapd %xmm2,%xmm0
divsd  %xmm3,%xmm0
addsd  %xmm4,%xmm0
add    $0x4,%eax
cmp    $0x1e8483,%eax
jne    1160 <main+0x30>
mulsd  0xe75(%rip),%xmm0
cvttsd2si %xmm0,%esi
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%ebx
lea    0xe65(%rip),%rdi
mov    %eax,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe7b(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe93(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"sum = %f\0A\00"
@.str.1 = [43 x i8] c"Thread %d > before initialization, x = %d\0A\00"
@.str.2 = [42 x i8] c"Thread %d > after initialization, x = %d\0A\00"
@.str.3 = [30 x i8] c"After parallel block, x = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 1.000000e+00, double* %6
store double 0.000000e+00, double* %7
store i32 1000000, i32* %9
store i32 5, i32* %10
store i32 1, i32* %11
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 2
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 @strtol(i8* %19, i32* null, i32 10)
store i32 %20, i32* %11
br label %21
21: ; preds = %16, %2
store i32 0, i32* %12
br label %22
22: ; preds = %40, %21
%23 = load i32, i32* %12
%24 = load i32, i32* %9
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %43
26: ; preds = %22
%27 = load i32, i32* %12
%28 = srem i32 %27, 2
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, double 1.000000e+00, double -1.000000e+00
store double %31, double* %6
%32 = load double, double* %6
%33 = load i32, i32* %12
%34 = mul nsw i32 2, %33
%35 = add nsw i32 %34, 1
%36 = sitofp i32 %35 to double
%37 = fdiv double %32, %36
%38 = load double, double* %7
%39 = fadd double %38, %37
store double %39, double* %7
br label %40
40: ; preds = %26
%41 = load i32, i32* %12
%42 = add nsw i32 %41, 1
store i32 %42, i32* %12
br label %22
43: ; preds = %22
%44 = load double, double* %7
%45 = fmul double 4.000000e+00, %44
store double %45, double* %8
%46 = load double, double* %8
%47 = fptosi double %46 to i32
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %47)
%49 = call i32 (...) @omp_get_thread_num()
store i32 %49, i32* %13
store i32 1, i32* %10
%50 = load i32, i32* %13
%51 = load i32, i32* %10
%52 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 %51)
%53 = load i32, i32* %13
store i32 %53, i32* %10
%54 = load i32, i32* %13
%55 = load i32, i32* %10
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 %55)
%57 = load i32, i32* %10
%58 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"sum = %f\0A\00"
@.str.1 = [43 x i8] c"Thread %d > before initialization, x = %d\0A\00"
@.str.2 = [42 x i8] c"Thread %d > after initialization, x = %d\0A\00"
@.str.3 = [30 x i8] c"After parallel block, x = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %5, label %4
4: ; preds = %5, %2
br label %17
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strtol(i8* nocapture %7, i32* null, i32 10)
br label %4
9: ; preds = %17
%10 = fmul double %29, 4.000000e+00
%11 = fptosi double %10 to i32
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @omp_get_thread_num()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 1)
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %13)
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %13)
ret i32 0
17: ; preds = %17, %4
%18 = phi i32 [ 0, %4 ], [ %30, %17 ]
%19 = phi double [ 0.000000e+00, %4 ], [ %29, %17 ]
%20 = shl nuw nsw i32 %18, 1
%21 = or i32 %20, 1
%22 = sitofp i32 %21 to double
%23 = fdiv double 1.000000e+00, %22
%24 = fadd double %19, %23
%25 = shl nuw i32 %18, 1
%26 = or i32 %25, 3
%27 = sitofp i32 %26 to double
%28 = fdiv double -1.000000e+00, %27
%29 = fadd double %24, %28
%30 = add nuw nsw i32 %18, 2
%31 = icmp eq i32 %30, 1000000
br i1 %31, label %9, label %17
}