#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 /*<<< orphan*/ printf(char *, int, ...);
int main() {
  int i, n = 25;
  int sum, TID, a[n];
  int ref = 0 + (n - 1) * n / 2;
  int sumLocal;
  for (i = 0; i < n; i++)
    a[i] = i;
#pragma omp parallel
  {
#pragma omp single
    printf("Number of threads is %d\n", 1);
  }
  sum = 0;
  printf("Value of sum prior to parallel region: %d\n", sum);
#pragma omp parallel default(none) shared(n, a, sum) private(TID, sumLocal)
  {
    TID = 0;
    sumLocal = 0;
#pragma omp for
    for (i = 0; i < n; i++)
      sumLocal += a[i];
#pragma omp critical(update_sum)
    {
      sum += sumLocal;
      printf("TID=%d: sumLocal = %d sum = %d\n", TID, sumLocal, sum);
    }
  }
  printf("Value of sum after parallel region: %d\n", sum);
  printf("Check results: sum = %d (should be %d)\n", sum, ref);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x19,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
imul   -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x0,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1194 <main+0x84>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%edx
movslq -0x8(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116c <main+0x5c>
lea    0xe65(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ff <main+0xef>
mov    -0x38(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11d4 <main+0xc4>
mov    -0x30(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee8(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeee(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf09(%rip),%rdi
xor    %esi,%esi
mov    $0x12c,%edx
mov    $0x12c,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf0f(%rip),%rdi
mov    $0x12c,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf24(%rip),%rdi
mov    $0x12c,%esi
mov    $0x12c,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Number of threads is %d\0A\00"
@.str.1 = [43 x i8] c"Value of sum prior to parallel region: %d\0A\00"
@.str.2 = [32 x i8] c"TID=%d: sumLocal = %d sum = %d\0A\00"
@.str.3 = [40 x i8] c"Value of sum after parallel region: %d\0A\00"
@.str.4 = [40 x i8] c"Check results: sum = %d (should be %d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
store i32 25, i32* %3
%10 = load i32, i32* %3
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i32, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* %3
%15 = sub nsw i32 %14, 1
%16 = load i32, i32* %3
%17 = mul nsw i32 %15, %16
%18 = sdiv i32 %17, 2
%19 = add nsw i32 0, %18
store i32 %19, i32* %8
store i32 0, i32* %2
br label %20
20: ; preds = %29, %0
%21 = load i32, i32* %2
%22 = load i32, i32* %3
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %2
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %13, i64 %27
store i32 %25, i32* %28
br label %29
29: ; preds = %24
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
br label %20
32: ; preds = %20
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 0, i32* %4
%34 = load i32, i32* %4
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 0, i32* %5
store i32 0, i32* %9
store i32 0, i32* %2
br label %36
36: ; preds = %47, %32
%37 = load i32, i32* %2
%38 = load i32, i32* %3
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %50
40: ; preds = %36
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %13, i64 %42
%44 = load i32, i32* %43
%45 = load i32, i32* %9
%46 = add nsw i32 %45, %44
store i32 %46, i32* %9
br label %47
47: ; preds = %40
%48 = load i32, i32* %2
%49 = add nsw i32 %48, 1
store i32 %49, i32* %2
br label %36
50: ; preds = %36
%51 = load i32, i32* %9
%52 = load i32, i32* %4
%53 = add nsw i32 %52, %51
store i32 %53, i32* %4
%54 = load i32, i32* %5
%55 = load i32, i32* %9
%56 = load i32, i32* %4
%57 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 %55, i32 %56)
%58 = load i32, i32* %4
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %4
%61 = load i32, i32* %8
%62 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %60, i32 %61)
store i32 0, i32* %1
%63 = load i8*, i8** %6
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
@.str = [25 x i8] c"Number of threads is %d\0A\00"
@.str.1 = [43 x i8] c"Value of sum prior to parallel region: %d\0A\00"
@.str.2 = [32 x i8] c"TID=%d: sumLocal = %d sum = %d\0A\00"
@.str.3 = [40 x i8] c"Value of sum after parallel region: %d\0A\00"
@.str.4 = [40 x i8] c"Check results: sum = %d (should be %d)\0A\00"
define i32 @main() {
%1 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 1)
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 0)
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 300, i32 300)
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 300)
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 300, i32 300)
ret i32 0
}