#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 */
typedef int /*<<< orphan*/ omp_lock_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ generate_samples(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ omp_destroy_lock(int /*<<< orphan*/ *);
int omp_get_num_threads();
int omp_get_thread_num();
int /*<<< orphan*/ omp_init_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ omp_set_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ omp_unset_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  omp_lock_t hist_locks[100000];
  int hist[100000];
  int *samples;
  int i, ival;
  samples = (int *)malloc(sizeof(int) * 10000000);
  assert(samples != NULL);
  generate_samples(samples);
#pragma omp parallel
  {
    printf("[%d] Total threads: %d\n", omp_get_thread_num(),
           omp_get_num_threads());
#pragma omp for
    for (i = 0; i < 100000; ++i) {
      omp_init_lock(&hist_locks[i]);
      hist[i] = 0;
    }
  }
#pragma omp parallel for private(ival)
  for (i = 0; i < 10000000; ++i) {
    ival = samples[i];
    omp_set_lock(&hist_locks[ival]);
    hist[ival]++;
    omp_unset_lock(&hist_locks[ival]);
  }
#pragma omp parallel for
  for (i = 0; i < 100000; ++i) {
    omp_destroy_lock(&hist_locks[i]);
  }
  if (0) {
    printf("Histogram:\n");
    for (i = 0; i < 100000; ++i) {
      printf("%d ---> %d\n", i, hist[i]);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc3530,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x2625a00,%edi
call   1090 <malloc@plt>
mov    %rax,-0xc3518(%rbp)
cmpq   $0x0,-0xc3518(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0xc3518(%rbp),%rdi
call   10a0 <generate_samples@plt>
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0xc3524(%rbp)
mov    $0x0,%al
call   1060 <omp_get_num_threads@plt>
mov    -0xc3524(%rbp),%esi
mov    %eax,%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0xc351c(%rbp)
cmpl   $0x186a0,-0xc351c(%rbp)
jge    1268 <main+0xc8>
movslq -0xc351c(%rbp),%rax
lea    -0x61a90(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   10b0 <omp_init_lock@plt>
movslq -0xc351c(%rbp),%rax
movl   $0x0,-0xc3510(%rbp,%rax,4)
mov    -0xc351c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc351c(%rbp)
jmp    1218 <main+0x78>
movl   $0x0,-0xc351c(%rbp)
cmpl   $0x989680,-0xc351c(%rbp)
jge    12f9 <main+0x159>
mov    -0xc3518(%rbp),%rax
movslq -0xc351c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xc3520(%rbp)
movslq -0xc3520(%rbp),%rax
lea    -0x61a90(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1080 <omp_set_lock@plt>
movslq -0xc3520(%rbp),%rax
mov    -0xc3510(%rbp,%rax,4),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc3510(%rbp,%rax,4)
movslq -0xc3520(%rbp),%rax
lea    -0x61a90(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1040 <omp_unset_lock@plt>
mov    -0xc351c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc351c(%rbp)
jmp    1272 <main+0xd2>
movl   $0x0,-0xc351c(%rbp)
cmpl   $0x186a0,-0xc351c(%rbp)
jge    1341 <main+0x1a1>
movslq -0xc351c(%rbp),%rax
lea    -0x61a90(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   10c0 <omp_destroy_lock@plt>
mov    -0xc351c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc351c(%rbp)
jmp    1303 <main+0x163>
mov    -0x4(%rbp),%eax
add    $0xc3530,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc3508,%rsp
mov    $0x2625a00,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   10a0 <generate_samples@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1060 <omp_get_num_threads@plt>
lea    0xe16(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
lea    (%rsp,%rbp,1),%rdi
call   10b0 <omp_init_lock@plt>
movl   $0x0,0x61a80(%rsp,%rbp,1)
add    $0x4,%rbp
cmp    $0x61a80,%rbp
jne    1200 <main+0x60>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq (%r15,%rbx,4),%rbp
lea    (%rsp,%rbp,4),%r14
mov    %r14,%rdi
call   1080 <omp_set_lock@plt>
addl   $0x1,0x61a80(%rsp,%rbp,4)
01
mov    %r14,%rdi
call   1040 <omp_unset_lock@plt>
add    $0x1,%rbx
cmp    $0x989680,%rbx
jne    1230 <main+0x90>
xor    %ebx,%ebx
nop
lea    (%rsp,%rbx,1),%rdi
call   10c0 <omp_destroy_lock@plt>
add    $0x4,%rbx
cmp    $0x61a80,%rbx
jne    1260 <main+0xc0>
xor    %eax,%eax
add    $0xc3508,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"[%d] Total threads: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100000 x i32]
%3 = alloca [100000 x i32]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i64 @malloc(i32 40000000)
%8 = inttoptr i64 %7 to i32*
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32*, i32** %4
%14 = call i32 @generate_samples(i32* %13)
%15 = call i32 (...) @omp_get_thread_num()
%16 = call i32 (...) @omp_get_num_threads()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
store i32 0, i32* %5
br label %18
18: ; preds = %29, %0
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 100000
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %23
%25 = call i32 @omp_init_lock(i32* %24)
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 %27
store i32 0, i32* %28
br label %29
29: ; preds = %21
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %18
32: ; preds = %18
store i32 0, i32* %5
br label %33
33: ; preds = %55, %32
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 10000000
br i1 %35, label %36, label %58
36: ; preds = %33
%37 = load i32*, i32** %4
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %43
%45 = call i32 @omp_set_lock(i32* %44)
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [100000 x i32], [100000 x i32]* %3, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
store i32 %50, i32* %48
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %52
%54 = call i32 @omp_unset_lock(i32* %53)
br label %55
55: ; preds = %36
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %33
58: ; preds = %33
store i32 0, i32* %5
br label %59
59: ; preds = %67, %58
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 100000
br i1 %61, label %62, label %70
62: ; preds = %59
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %64
%66 = call i32 @omp_destroy_lock(i32* %65)
br label %67
67: ; preds = %62
%68 = load i32, i32* %5
%69 = add nsw i32 %68, 1
store i32 %69, i32* %5
br label %59
70: ; preds = %59
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@.str = [24 x i8] c"[%d] Total threads: %d\0A\00"
define i32 @main() {
%1 = alloca [100000 x i32]
%2 = alloca [100000 x i32]
%3 = bitcast [100000 x i32]* %1 to i8*
%4 = bitcast [100000 x i32]* %2 to i8*
%5 = tail call i64 @malloc(i32 40000000)
%6 = inttoptr i64 %5 to i32*
%7 = icmp ne i64 %5, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @generate_samples(i32* %6)
%11 = tail call i32 (...) @omp_get_thread_num()
%12 = tail call i32 (...) @omp_get_num_threads()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
br label %14
14: ; preds = %0, %14
%15 = phi i64 [ 0, %0 ], [ %19, %14 ]
%16 = getelementptr inbounds [100000 x i32], [100000 x i32]* %1, i64 0, i64 %15
%17 = call i32 @omp_init_lock(i32* %16)
%18 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %15
store i32 0, i32* %18
%19 = add nuw nsw i64 %15, 1
%20 = icmp eq i64 %19, 100000
br i1 %20, label %21, label %14
21: ; preds = %14, %21
%22 = phi i64 [ %32, %21 ], [ 0, %14 ]
%23 = getelementptr inbounds i32, i32* %6, i64 %22
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [100000 x i32], [100000 x i32]* %1, i64 0, i64 %25
%27 = call i32 @omp_set_lock(i32* %26)
%28 = getelementptr inbounds [100000 x i32], [100000 x i32]* %2, i64 0, i64 %25
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
store i32 %30, i32* %28
%31 = call i32 @omp_unset_lock(i32* %26)
%32 = add nuw nsw i64 %22, 1
%33 = icmp eq i64 %32, 10000000
br i1 %33, label %34, label %21
34: ; preds = %21, %34
%35 = phi i64 [ %38, %34 ], [ 0, %21 ]
%36 = getelementptr inbounds [100000 x i32], [100000 x i32]* %1, i64 0, i64 %35
%37 = call i32 @omp_destroy_lock(i32* %36)
%38 = add nuw nsw i64 %35, 1
%39 = icmp eq i64 %38, 100000
br i1 %39, label %40, label %34
40: ; preds = %34
ret i32 0
}