#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*/ free(int *);
int *malloc(int);
int omp_get_thread_num();
float omp_get_wtime();
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int *a = malloc(25 * sizeof(int));
  int i = 0;
  int n = 25;
  int localSum = 0;
  int totalSum = 0;
  int thread = 0;
  float start = 0.0;
  float end = 0.0;
  float time = 0.0;
  for (i = 0; i < n; i++) {
    a[i] = i + 1;
  }
#pragma omp parallel shared(n, a, totalSum) private(thread, localSum)
  {
    thread = omp_get_thread_num();
    localSum = 0;
#pragma omp for
    for (i = 0; i < n; i++) {
      localSum += a[i];
    }
    start = omp_get_wtime();
#pragma omp critical(totalSum)
    {
      totalSum += localSum;
      printf("Thread %d has local sum %d and adds to total sum %d.\n", thread,
             localSum, totalSum);
    }
#pragma omp critical(print)
    { printf("I'm an unnamed critical region.\n"); }
    end = omp_get_wtime();
  }
  time = end - start;
  printf("Total sum at end is %d.\nTime:  %0.9lf\n", totalSum, time);
  free(a);
  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)
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x19,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11e1 <main+0x91>
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b6 <main+0x66>
mov    $0x0,%al
call   1040 <omp_get_thread_num@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1224 <main+0xd4>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f9 <main+0xa9>
mov    $0x0,%al
call   1050 <omp_get_wtime@plt>
movss  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <omp_get_wtime@plt>
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
subss  -0x30(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
mov    -0x28(%rbp),%esi
movss  -0x38(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdcb(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movaps 0xe84(%rip),%xmm0
movups %xmm0,(%rax)
movaps 0xe8a(%rip),%xmm0
movups %xmm0,0x10(%rax)
movaps 0xe8f(%rip),%xmm0
movups %xmm0,0x20(%rax)
movaps 0xe94(%rip),%xmm0
movups %xmm0,0x30(%rax)
movaps 0xe99(%rip),%xmm0
movups %xmm0,0x40(%rax)
movaps 0xe9e(%rip),%xmm0
movups %xmm0,0x50(%rax)
movl   $0x19,0x60(%rax)
xor    %eax,%eax
call   1050 <omp_get_thread_num@plt>
mov    %eax,%r14d
movdqu 0x44(%rbx),%xmm0
movdqu 0x54(%rbx),%xmm1
movdqu 0x4(%rbx),%xmm2
movdqu 0x14(%rbx),%xmm3
paddd  %xmm2,%xmm3
movdqu 0x24(%rbx),%xmm2
paddd  %xmm0,%xmm2
movdqu 0x34(%rbx),%xmm0
paddd  %xmm1,%xmm0
paddd  %xmm2,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%eax
pshufd $0xee,%xmm3,%xmm0
paddd  %xmm3,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%ebp
add    %eax,%ebp
add    (%rbx),%ebp
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
movd   %xmm0,0xc(%rsp)
lea    0xe27(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
subss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe2b(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [54 x i8] c"Thread %d has local sum %d and adds to total sum %d.\0A\00"
@.str.1 = [33 x i8] c"I'm an unnamed critical region.\0A\00"
@.str.2 = [39 x i8] c"Total sum at end is %d.\0ATime: %0.9lf\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 i32
%12 = alloca float
%13 = alloca float
%14 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32* @malloc(i32 100)
store i32* %15, i32** %6
store i32 0, i32* %7
store i32 25, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store float 0.000000e+00, float* %12
store float 0.000000e+00, float* %13
store float 0.000000e+00, float* %14
store i32 0, i32* %7
br label %16
16: ; preds = %27, %2
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
store i32 %22, i32* %26
br label %27
27: ; preds = %20
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %16
30: ; preds = %16
%31 = call i32 (...) @omp_get_thread_num()
store i32 %31, i32* %11
store i32 0, i32* %9
store i32 0, i32* %7
br label %32
32: ; preds = %44, %30
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
br label %44
44: ; preds = %36
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %32
47: ; preds = %32
%48 = call float (...) @omp_get_wtime()
store float %48, float* %12
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = add nsw i32 %50, %49
store i32 %51, i32* %10
%52 = load i32, i32* %11
%53 = load i32, i32* %9
%54 = load i32, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%57 = call float (...) @omp_get_wtime()
store float %57, float* %13
%58 = load float, float* %13
%59 = load float, float* %12
%60 = fsub float %58, %59
store float %60, float* %14
%61 = load i32, i32* %10
%62 = load float, float* %14
%63 = fpext float %62 to double
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %61, double %63)
%65 = load i32*, i32** %6
%66 = call i32 @free(i32* %65)
ret i32 0
}

LLVM IR (-O2):
@.str = [54 x i8] c"Thread %d has local sum %d and adds to total sum %d.\0A\00"
@.str.2 = [39 x i8] c"Total sum at end is %d.\0ATime: %0.9lf\0A\00"
@str = [32 x i8] c"I'm an unnamed critical region.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @malloc(i32 100)
%4 = getelementptr inbounds i32, i32* %3, i64 1
%5 = bitcast i32* %3 to <4 x i32>*
store <4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32>* %5
%6 = getelementptr inbounds i32, i32* %3, i64 4
%7 = bitcast i32* %6 to <4 x i32>*
store <4 x i32> <i32 5, i32 6, i32 7, i32 8>, <4 x i32>* %7
%8 = getelementptr inbounds i32, i32* %3, i64 8
%9 = getelementptr inbounds i32, i32* %3, i64 9
%10 = bitcast i32* %8 to <4 x i32>*
store <4 x i32> <i32 9, i32 10, i32 11, i32 12>, <4 x i32>* %10
%11 = getelementptr inbounds i32, i32* %3, i64 12
%12 = bitcast i32* %11 to <4 x i32>*
store <4 x i32> <i32 13, i32 14, i32 15, i32 16>, <4 x i32>* %12
%13 = getelementptr inbounds i32, i32* %3, i64 16
%14 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> <i32 17, i32 18, i32 19, i32 20>, <4 x i32>* %14
%15 = getelementptr inbounds i32, i32* %3, i64 20
%16 = bitcast i32* %15 to <4 x i32>*
store <4 x i32> <i32 21, i32 22, i32 23, i32 24>, <4 x i32>* %16
%17 = getelementptr inbounds i32, i32* %3, i64 24
store i32 25, i32* %17
%18 = tail call i32 (...) @omp_get_thread_num()
%19 = load i32, i32* %3
%20 = bitcast i32* %4 to <8 x i32>*
%21 = load <8 x i32>, <8 x i32>* %20
%22 = bitcast i32* %9 to <16 x i32>*
%23 = load <16 x i32>, <16 x i32>* %22
%24 = call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> %23)
%25 = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %21)
%26 = add nsw i32 %24, %25
%27 = add nsw i32 %26, %19
%28 = tail call float (...) @omp_get_wtime()
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %27, i32 %27)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%31 = tail call float (...) @omp_get_wtime()
%32 = fsub float %31, %28
%33 = fpext float %32 to double
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %27, double %33)
%35 = tail call i32 @free(i32* %3)
ret i32 0
}