#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int omp_get_num_threads();
int omp_get_thread_num();
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  int threadCount = 20;
  omp_set_num_threads(threadCount);
  int i = 6;
  int *v = (int *)calloc(threadCount, sizeof(int));
  int veryBad = 0;
#pragma omp parallel
  {
    int rank = omp_get_thread_num();
    int size = omp_get_num_threads();
#pragma omp critical
    {
      v[rank] = rank;
      veryBad += rank;
      printf("veryBad = %d (from rank %d)\n", veryBad, rank);
    }
  }
  printf("veryBad = %d\n", veryBad);
  exit(0);
  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)
movl   $0x14,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <omp_set_num_threads@plt>
movl   $0x6,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <omp_get_num_threads@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x14,%edi
call   1070 <omp_set_num_threads@plt>
mov    $0x14,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <omp_get_num_threads@plt>
movslq %ebp,%rbp
mov    %ebp,(%rbx,%rbp,4)
lea    0xe64(%rip),%rdi
mov    %ebp,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"veryBad = %d (from rank %d)\0A\00"
@.str.1 = [14 x i8] c"veryBad = %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
store i32 20, i32* %6
%12 = load i32, i32* %6
%13 = call i32 @omp_set_num_threads(i32 %12)
store i32 6, i32* %7
%14 = load i32, i32* %6
%15 = call i64 @calloc(i32 %14, i32 4)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %8
store i32 0, i32* %9
%17 = call i32 (...) @omp_get_thread_num()
store i32 %17, i32* %10
%18 = call i32 (...) @omp_get_num_threads()
store i32 %18, i32* %11
%19 = load i32, i32* %10
%20 = load i32*, i32** %8
%21 = load i32, i32* %10
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 %19, i32* %23
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = add nsw i32 %25, %24
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* %10
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28)
%30 = load i32, i32* %9
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"veryBad = %d (from rank %d)\0A\00"
@.str.1 = [14 x i8] c"veryBad = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @omp_set_num_threads(i32 20)
%4 = tail call i64 @calloc(i32 20, i32 4)
%5 = inttoptr i64 %4 to i32*
%6 = tail call i32 (...) @omp_get_thread_num()
%7 = tail call i32 (...) @omp_get_num_threads()
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i32, i32* %5, i64 %8
store i32 %6, i32* %9
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %6)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%12 = tail call i32 @exit(i32 0)
unreachable
}