#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*/ printf(char *);
int /*<<< orphan*/ spin_lock(int *);
int /*<<< orphan*/ spin_unlock(int *);
void barrier(int *bar) {
  int incr = 0;
  while (1) {
    spin_lock((bar + 3));
    if (incr == 0 && *(bar + 2) > 0) {
      spin_unlock((bar + 3));
      continue;
    }
    if (incr == 0) {
      *(bar + 1) = *(bar + 1) + 1;
      incr = 1;
    }
    if (*(bar + 1) < *(bar + 0) && (*(bar + 2) == 0)) {
      spin_unlock((bar + 3));
      continue;
    } else {
      if (*(bar + 2) == 0) {
        *(bar + 2) = *(bar + 0) - 1;
        *(bar + 1) = 0;
        spin_unlock((bar + 3));
        return;
      } else {
        *(bar + 2) = *(bar + 2) - 1;
        spin_unlock((bar + 3));
        return;
      }
    }
    spin_unlock((bar + 3));
    printf("\nError in barrier");
    return;
  }
}


x86 assembly (clang, -O0):
<barrier>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
add    $0xc,%rdi
call   1030 <spin_lock@plt>
cmpl   $0x0,-0xc(%rbp)
jne    116a <barrier+0x4a>
mov    -0x8(%rbp),%rax
cmpl   $0x0,0x8(%rax)
jle    116a <barrier+0x4a>
mov    -0x8(%rbp),%rdi
add    $0xc,%rdi
call   1040 <spin_unlock@plt>
jmp    1133 <barrier+0x13>
cmpl   $0x0,-0xc(%rbp)
jne    118c <barrier+0x6c>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    11bf <barrier+0x9f>
mov    -0x8(%rbp),%rax
cmpl   $0x0,0x8(%rax)
jne    11bf <barrier+0x9f>
mov    -0x8(%rbp),%rdi
add    $0xc,%rdi
call   1040 <spin_unlock@plt>
jmp    1133 <barrier+0x13>
mov    -0x8(%rbp),%rax
cmpl   $0x0,0x8(%rax)
jne    11fa <barrier+0xda>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0x8(%rbp),%rdi
add    $0xc,%rdi
call   1040 <spin_unlock@plt>
jmp    1218 <barrier+0xf8>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
sub    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rdi
add    $0xc,%rdi
call   1040 <spin_unlock@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<barrier>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xc(%rdi),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <spin_lock@plt>
cmpl   $0x0,0x8(%r14)
jle    1149 <barrier+0x29>
mov    %rbx,%rdi
call   1040 <spin_unlock@plt>
jmp    1130 <barrier+0x10>
mov    (%r14),%eax
mov    0x4(%r14),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%r14)
cmp    %eax,%ecx
jge    1181 <barrier+0x61>
nopl   0x0(%rax,%rax,1)
mov    0x8(%r14),%ecx
test   %ecx,%ecx
jne    1189 <barrier+0x69>
mov    %rbx,%rdi
call   1040 <spin_unlock@plt>
mov    %rbx,%rdi
call   1030 <spin_lock@plt>
mov    (%r14),%eax
cmp    %eax,0x4(%r14)
jl     1160 <barrier+0x40>
mov    0x8(%r14),%ecx
test   %ecx,%ecx
je     1192 <barrier+0x72>
add    $0xffffffff,%ecx
mov    %ecx,0x8(%r14)
jmp    11a1 <barrier+0x81>
add    $0xffffffff,%eax
mov    %eax,0x8(%r14)
movl   $0x0,0x4(%r14)
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <spin_unlock@plt>

LLVM IR (-O0):
define void @barrier(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
br label %4
4: ; preds = %1, %15, %42
%5 = load i32*, i32** %2
%6 = getelementptr inbounds i32, i32* %5, i64 3
%7 = call i32 @spin_lock(i32* %6)
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %4
%11 = load i32*, i32** %2
%12 = getelementptr inbounds i32, i32* %11, i64 2
%13 = load i32, i32* %12
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32*, i32** %2
%17 = getelementptr inbounds i32, i32* %16, i64 3
%18 = call i32 @spin_unlock(i32* %17)
br label %4
19: ; preds = %10, %4
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32*, i32** %2
%24 = getelementptr inbounds i32, i32* %23, i64 1
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
%27 = load i32*, i32** %2
%28 = getelementptr inbounds i32, i32* %27, i64 1
store i32 %26, i32* %28
store i32 1, i32* %3
br label %29
29: ; preds = %22, %19
%30 = load i32*, i32** %2
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = load i32, i32* %31
%33 = load i32*, i32** %2
%34 = getelementptr inbounds i32, i32* %33, i64 0
%35 = load i32, i32* %34
%36 = icmp slt i32 %32, %35
br i1 %36, label %37, label %46
37: ; preds = %29
%38 = load i32*, i32** %2
%39 = getelementptr inbounds i32, i32* %38, i64 2
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32*, i32** %2
%44 = getelementptr inbounds i32, i32* %43, i64 3
%45 = call i32 @spin_unlock(i32* %44)
br label %4
46: ; preds = %37, %29
%47 = load i32*, i32** %2
%48 = getelementptr inbounds i32, i32* %47, i64 2
%49 = load i32, i32* %48
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %63
51: ; preds = %46
%52 = load i32*, i32** %2
%53 = getelementptr inbounds i32, i32* %52, i64 0
%54 = load i32, i32* %53
%55 = sub nsw i32 %54, 1
%56 = load i32*, i32** %2
%57 = getelementptr inbounds i32, i32* %56, i64 2
store i32 %55, i32* %57
%58 = load i32*, i32** %2
%59 = getelementptr inbounds i32, i32* %58, i64 1
store i32 0, i32* %59
%60 = load i32*, i32** %2
%61 = getelementptr inbounds i32, i32* %60, i64 3
%62 = call i32 @spin_unlock(i32* %61)
br label %73
63: ; preds = %46
%64 = load i32*, i32** %2
%65 = getelementptr inbounds i32, i32* %64, i64 2
%66 = load i32, i32* %65
%67 = sub nsw i32 %66, 1
%68 = load i32*, i32** %2
%69 = getelementptr inbounds i32, i32* %68, i64 2
store i32 %67, i32* %69
%70 = load i32*, i32** %2
%71 = getelementptr inbounds i32, i32* %70, i64 3
%72 = call i32 @spin_unlock(i32* %71)
br label %73
73: ; preds = %63, %51
ret void
}

LLVM IR (-O2):
define void @barrier(i32* %0) {
%2 = getelementptr inbounds i32, i32* %0, i64 3
%3 = getelementptr inbounds i32, i32* %0, i64 2
%4 = getelementptr inbounds i32, i32* %0, i64 1
%5 = tail call i32 @spin_lock(i32* %2)
%6 = load i32, i32* %3
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1, %8
%9 = tail call i32 @spin_unlock(i32* %2)
%10 = tail call i32 @spin_lock(i32* %2)
%11 = load i32, i32* %3
%12 = icmp sgt i32 %11, 0
br i1 %12, label %8, label %13
13: ; preds = %8, %1
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = load i32, i32* %0
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %13, %21
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = tail call i32 @spin_unlock(i32* %2)
%23 = tail call i32 @spin_lock(i32* %2)
%24 = load i32, i32* %4
%25 = load i32, i32* %0
%26 = icmp slt i32 %24, %25
br i1 %26, label %18, label %27
27: ; preds = %21, %13
%28 = phi i32 [ %16, %13 ], [ %25, %21 ]
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = add nsw i32 %28, -1
store i32 %32, i32* %3
store i32 0, i32* %4
br label %36
33: ; preds = %18, %27
%34 = phi i32 [ %29, %27 ], [ %19, %18 ]
%35 = add nsw i32 %34, -1
store i32 %35, i32* %3
br label %36
36: ; preds = %33, %31
%37 = tail call i32 @spin_unlock(i32* %2)
ret void
}