#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();
int /*<<< orphan*/ printf(char *, int, ...);
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;
  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];
    }
#pragma omp critical(totalSum)
    {
      totalSum += localSum;
      printf("Thread %d has local sum %d and adds to total sum %d.\n", thread,
             localSum, totalSum);
    }
  }
  printf("Total sum at end is %d.\n", totalSum);
  free(a);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x64,%edi
call   1060 <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)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11b9 <main+0x79>
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    118e <main+0x4e>
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    11fc <main+0xbc>
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    11d1 <main+0x91>
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    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movaps 0xea9(%rip),%xmm0
movups %xmm0,(%rax)
movaps 0xeaf(%rip),%xmm0
movups %xmm0,0x10(%rax)
movaps 0xeb4(%rip),%xmm0
movups %xmm0,0x20(%rax)
movaps 0xeb9(%rip),%xmm0
movups %xmm0,0x30(%rax)
movaps 0xebe(%rip),%xmm0
movups %xmm0,0x40(%rax)
movaps 0xec3(%rip),%xmm0
movups %xmm0,0x50(%rax)
movl   $0x19,0x60(%rax)
xor    %eax,%eax
call   1040 <omp_get_thread_num@plt>
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,%ecx
pshufd $0xee,%xmm3,%xmm0
paddd  %xmm3,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%ebp
add    %ecx,%ebp
add    (%rbx),%ebp
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
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 = [25 x i8] c"Total sum at end is %d.\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32* @malloc(i32 100)
store i32* %12, 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 i32 0, i32* %7
br label %13
13: ; preds = %24, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %27
17: ; preds = %13
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = load i32*, i32** %6
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 %19, i32* %23
br label %24
24: ; preds = %17
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %13
27: ; preds = %13
%28 = call i32 (...) @omp_get_thread_num()
store i32 %28, i32* %11
store i32 0, i32* %9
store i32 0, i32* %7
br label %29
29: ; preds = %41, %27
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = load i32*, i32** %6
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = load i32, i32* %9
%40 = add nsw i32 %39, %38
store i32 %40, i32* %9
br label %41
41: ; preds = %33
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %29
44: ; preds = %29
%45 = load i32, i32* %9
%46 = load i32, i32* %10
%47 = add nsw i32 %46, %45
store i32 %47, i32* %10
%48 = load i32, i32* %11
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %48, i32 %49, i32 %50)
%52 = load i32, i32* %10
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i32*, i32** %6
%55 = call i32 @free(i32* %54)
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.1 = [25 x i8] c"Total sum at end is %d.\0A\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 i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %27, i32 %27)
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%30 = tail call i32 @free(i32* %3)
ret i32 0
}