#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*/ assert(int *);
int /*<<< orphan*/ atomic_cas(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ atomic_dec(int *);
int /*<<< orphan*/ atomic_inc(int *);
unsigned long long current_time_ns();
scalar_t__ malloc(int);
int omp_get_num_threads();
int /*<<< orphan*/ printf(char *, double);
int main(int argc, char **argv) {
  int nthreads;
#pragma omp parallel
#pragma omp single
  { nthreads = omp_get_num_threads(); }
  int *shared = (int *)malloc(sizeof(int));
  assert(shared);
  *shared = 0;
  unsigned long long start, end, elapsed;
#pragma omp parallel
  {
    int i;
    for (i = 0; i < (100000 / 10) + 100000; i++) {
      if (i == (100000 / 10)) {
#pragma omp barrier
#pragma omp master
        start = current_time_ns();
      }
      atomic_inc(shared);
    }
#pragma omp barrier
#pragma omp master
    end = current_time_ns();
  }
  printf("INC : %f ops/ns\n",
         (double)(100000 * nthreads) / (double)(end - start));
#pragma omp parallel
  {
    int i;
    for (i = 0; i < (100000 / 10) + 100000; i++) {
      if (i == (100000 / 10)) {
#pragma omp barrier
#pragma omp master
        start = current_time_ns();
      }
      atomic_dec(shared);
    }
#pragma omp barrier
#pragma omp master
    end = current_time_ns();
  }
  printf("DEC : %f ops/ns\n",
         (double)(100000 * nthreads) / (double)(end - start));
#pragma omp parallel
  {
    int i;
    for (i = 0; i < (100000 / 10) + 100000; i++) {
      if (i == (100000 / 10)) {
#pragma omp barrier
#pragma omp master
        start = current_time_ns();
      }
      atomic_cas(shared, 0, 1);
    }
#pragma omp barrier
#pragma omp master
    end = current_time_ns();
  }
  printf("CAS : %f ops/ns\n",
         (double)(100000 * nthreads) / (double)(end - start));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <omp_get_num_threads@plt>
mov    %eax,-0x14(%rbp)
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x1adb0,-0x3c(%rbp)
jge    1204 <main+0x84>
cmpl   $0x2710,-0x3c(%rbp)
jne    11ed <main+0x6d>
mov    $0x0,%al
call   10a0 <current_time_ns@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <atomic_inc@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11c8 <main+0x48>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
imul   $0x186a0,%eax,%eax
cvtsi2sd %eax,%xmm0
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm1
movaps 0xdcd(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xdd1(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x40(%rbp)
cmpl   $0x1adb0,-0x40(%rbp)
jge    12a1 <main+0x121>
cmpl   $0x2710,-0x40(%rbp)
jne    128a <main+0x10a>
mov    $0x0,%al
call   10a0 <current_time_ns@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <atomic_dec@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1265 <main+0xe5>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
imul   $0x186a0,%eax,%eax
cvtsi2sd %eax,%xmm0
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm1
movaps 0xd30(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xd34(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
lea    0xd3b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0x1adb0,-0x44(%rbp)
jge    1345 <main+0x1c5>
cmpl   $0x2710,-0x44(%rbp)
jne    1327 <main+0x1a7>
mov    $0x0,%al
call   10a0 <current_time_ns@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <atomic_cas@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1302 <main+0x182>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
imul   $0x186a0,%eax,%eax
cvtsi2sd %eax,%xmm0
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm1
movaps 0xc8c(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xc90(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
lea    0xca8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <omp_get_num_threads@plt>
mov    %eax,%r15d
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <assert@plt>
movl   $0x0,(%rbx)
mov    $0x1adb0,%ebp
jmp    11cd <main+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1090 <atomic_inc@plt>
add    $0xffffffff,%ebp
je     11e1 <main+0x61>
cmp    $0x186a0,%ebp
jne    11c0 <main+0x40>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,%r14
jmp    11c0 <main+0x40>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
imul   $0x186a0,%r15d,%ecx
cvtsi2sd %ecx,%xmm2
sub    %r14,%rax
movq   %rax,%xmm0
punpckldq 0xdfd(%rip),%xmm0
subpd  0xe05(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm2,(%rsp)
movapd %xmm2,%xmm0
divsd  %xmm1,%xmm0
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1adb0,%ebp
jmp    124d <main+0xcd>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <atomic_dec@plt>
add    $0xffffffff,%ebp
je     1261 <main+0xe1>
cmp    $0x186a0,%ebp
jne    1240 <main+0xc0>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,%r14
jmp    1240 <main+0xc0>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
sub    %r14,%rax
movq   %rax,%xmm0
punpckldq 0xd88(%rip),%xmm0
subpd  0xd90(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
divsd  %xmm1,%xmm0
lea    0xd95(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1adb0,%ebp
jmp    12c4 <main+0x144>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <atomic_cas@plt>
add    $0xffffffff,%ebp
je     12d8 <main+0x158>
cmp    $0x186a0,%ebp
jne    12b0 <main+0x130>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
mov    %rax,%r14
jmp    12b0 <main+0x130>
xor    %eax,%eax
call   10a0 <current_time_ns@plt>
sub    %r14,%rax
movq   %rax,%xmm0
punpckldq 0xd11(%rip),%xmm0
subpd  0xd19(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
divsd  %xmm1,%xmm0
lea    0xd2f(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"INC : %f ops/ns\0A\00"
@.str.1 = [17 x i8] c"DEC : %f ops/ns\0A\00"
@.str.2 = [17 x i8] c"CAS : %f ops/ns\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 i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 (...) @omp_get_num_threads()
store i32 %14, i32* %6
%15 = call i64 @malloc(i32 4)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = call i32 @assert(i32* %17)
%19 = load i32*, i32** %7
store i32 0, i32* %19
store i32 0, i32* %11
br label %20
20: ; preds = %31, %2
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 110000
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = load i32, i32* %11
%25 = icmp eq i32 %24, 10000
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i64 (...) @current_time_ns()
store i64 %27, i64* %8
br label %28
28: ; preds = %26, %23
%29 = load i32*, i32** %7
%30 = call i32 @atomic_inc(i32* %29)
br label %31
31: ; preds = %28
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 1
store i32 %33, i32* %11
br label %20
34: ; preds = %20
%35 = call i64 (...) @current_time_ns()
store i64 %35, i64* %9
%36 = load i32, i32* %6
%37 = mul nsw i32 100000, %36
%38 = sitofp i32 %37 to double
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = sub i64 %39, %40
%42 = uitofp i64 %41 to double
%43 = fdiv double %38, %42
%44 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), double %43)
store i32 0, i32* %12
br label %45
45: ; preds = %56, %34
%46 = load i32, i32* %12
%47 = icmp slt i32 %46, 110000
br i1 %47, label %48, label %59
48: ; preds = %45
%49 = load i32, i32* %12
%50 = icmp eq i32 %49, 10000
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i64 (...) @current_time_ns()
store i64 %52, i64* %8
br label %53
53: ; preds = %51, %48
%54 = load i32*, i32** %7
%55 = call i32 @atomic_dec(i32* %54)
br label %56
56: ; preds = %53
%57 = load i32, i32* %12
%58 = add nsw i32 %57, 1
store i32 %58, i32* %12
br label %45
59: ; preds = %45
%60 = call i64 (...) @current_time_ns()
store i64 %60, i64* %9
%61 = load i32, i32* %6
%62 = mul nsw i32 100000, %61
%63 = sitofp i32 %62 to double
%64 = load i64, i64* %9
%65 = load i64, i64* %8
%66 = sub i64 %64, %65
%67 = uitofp i64 %66 to double
%68 = fdiv double %63, %67
%69 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %68)
store i32 0, i32* %13
br label %70
70: ; preds = %81, %59
%71 = load i32, i32* %13
%72 = icmp slt i32 %71, 110000
br i1 %72, label %73, label %84
73: ; preds = %70
%74 = load i32, i32* %13
%75 = icmp eq i32 %74, 10000
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i64 (...) @current_time_ns()
store i64 %77, i64* %8
br label %78
78: ; preds = %76, %73
%79 = load i32*, i32** %7
%80 = call i32 @atomic_cas(i32* %79, i32 0, i32 1)
br label %81
81: ; preds = %78
%82 = load i32, i32* %13
%83 = add nsw i32 %82, 1
store i32 %83, i32* %13
br label %70
84: ; preds = %70
%85 = call i64 (...) @current_time_ns()
store i64 %85, i64* %9
%86 = load i32, i32* %6
%87 = mul nsw i32 100000, %86
%88 = sitofp i32 %87 to double
%89 = load i64, i64* %9
%90 = load i64, i64* %8
%91 = sub i64 %89, %90
%92 = uitofp i64 %91 to double
%93 = fdiv double %88, %92
%94 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %93)
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"INC : %f ops/ns\0A\00"
@.str.1 = [17 x i8] c"DEC : %f ops/ns\0A\00"
@.str.2 = [17 x i8] c"CAS : %f ops/ns\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @omp_get_num_threads()
%4 = tail call i64 @malloc(i32 4)
%5 = inttoptr i64 %4 to i32*
%6 = tail call i32 @assert(i32* %5)
store i32 0, i32* %5
br label %7
7: ; preds = %2, %13
%8 = phi i32 [ 0, %2 ], [ %16, %13 ]
%9 = phi i64 [ undef, %2 ], [ %14, %13 ]
%10 = icmp eq i32 %8, 10000
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i64 (...) @current_time_ns()
br label %13
13: ; preds = %11, %7
%14 = phi i64 [ %12, %11 ], [ %9, %7 ]
%15 = tail call i32 @atomic_inc(i32* %5)
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, 110000
br i1 %17, label %18, label %7
18: ; preds = %13
%19 = tail call i64 (...) @current_time_ns()
%20 = mul nsw i32 %3, 100000
%21 = sitofp i32 %20 to double
%22 = sub i64 %19, %14
%23 = uitofp i64 %22 to double
%24 = fdiv double %21, %23
%25 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), double %24)
br label %26
26: ; preds = %18, %32
%27 = phi i32 [ 0, %18 ], [ %35, %32 ]
%28 = phi i64 [ %14, %18 ], [ %33, %32 ]
%29 = icmp eq i32 %27, 10000
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i64 (...) @current_time_ns()
br label %32
32: ; preds = %30, %26
%33 = phi i64 [ %31, %30 ], [ %28, %26 ]
%34 = tail call i32 @atomic_dec(i32* %5)
%35 = add nuw nsw i32 %27, 1
%36 = icmp eq i32 %35, 110000
br i1 %36, label %37, label %26
37: ; preds = %32
%38 = tail call i64 (...) @current_time_ns()
%39 = sub i64 %38, %33
%40 = uitofp i64 %39 to double
%41 = fdiv double %21, %40
%42 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %41)
br label %43
43: ; preds = %37, %49
%44 = phi i32 [ 0, %37 ], [ %52, %49 ]
%45 = phi i64 [ %33, %37 ], [ %50, %49 ]
%46 = icmp eq i32 %44, 10000
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = tail call i64 (...) @current_time_ns()
br label %49
49: ; preds = %47, %43
%50 = phi i64 [ %48, %47 ], [ %45, %43 ]
%51 = tail call i32 @atomic_cas(i32* %5, i32 0, i32 1)
%52 = add nuw nsw i32 %44, 1
%53 = icmp eq i32 %52, 110000
br i1 %53, label %54, label %43
54: ; preds = %49
%55 = tail call i64 (...) @current_time_ns()
%56 = sub i64 %55, %50
%57 = uitofp i64 %56 to double
%58 = fdiv double %21, %57
%59 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %58)
ret i32 0
}