#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 stackEM_getThreadID();
int /*<<< orphan*/ stackEmLock_acquire(unsigned int *);
int /*<<< orphan*/ stackEmLock_release(unsigned int *);
void stackEmBarrier_wait(unsigned int *barrierAddress,
                         unsigned int *lockOffset) {
  int last = 0;
  while (!stackEmLock_acquire(barrierAddress)) {
  }
  int value = *(barrierAddress + 1) - 1;
  *(barrierAddress + 1) = value;
  last = (value < 1);
  stackEmLock_release(barrierAddress);
  if (last) {
    *(barrierAddress + 1) = *(barrierAddress + 2);
    for (int i = 0; i < (110); i++) {
      unsigned int *addr = (i << 25) + lockOffset;
      stackEmLock_release(addr);
    }
  } else {
    unsigned int *addr = (stackEM_getThreadID() << 25) + lockOffset;
    while (!stackEmLock_acquire(addr)) {
    };
  }
}


x86 assembly (clang, -O0):
<stackEmBarrier_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <stackEmLock_acquire@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1165 <stackEmBarrier_wait+0x35>
jmp    116a <stackEmBarrier_wait+0x3a>
jmp    1147 <stackEmBarrier_wait+0x17>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x1,-0x18(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <stackEmLock_release@plt>
cmpl   $0x0,-0x14(%rbp)
je     11f6 <stackEmBarrier_wait+0xc6>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x6e,-0x1c(%rbp)
jge    11f1 <stackEmBarrier_wait+0xc1>
mov    -0x1c(%rbp),%ecx
shl    $0x19,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <stackEmLock_release@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b8 <stackEmBarrier_wait+0x88>
jmp    123c <stackEmBarrier_wait+0x10c>
mov    $0x0,%al
call   1030 <stackEM_getThreadID@plt>
mov    %eax,%ecx
shl    $0x19,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <stackEmLock_acquire@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1232 <stackEmBarrier_wait+0x102>
jmp    1237 <stackEmBarrier_wait+0x107>
jmp    1214 <stackEmBarrier_wait+0xe4>
jmp    123c <stackEmBarrier_wait+0x10c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stackEmBarrier_wait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <stackEmLock_acquire@plt>
test   %eax,%eax
je     1140 <stackEmBarrier_wait+0x10>
mov    0x4(%rbx),%ebp
add    $0xffffffff,%ebp
mov    %ebp,0x4(%rbx)
mov    %rbx,%rdi
call   1050 <stackEmLock_release@plt>
test   %ebp,%ebp
jle    118e <stackEmBarrier_wait+0x5e>
xor    %eax,%eax
call   1030 <stackEM_getThreadID@plt>
shl    $0x19,%eax
cltq
lea    (%r14,%rax,4),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <stackEmLock_acquire@plt>
test   %eax,%eax
je     1180 <stackEmBarrier_wait+0x50>
jmp    11b8 <stackEmBarrier_wait+0x88>
mov    0x8(%rbx),%eax
mov    %eax,0x4(%rbx)
xor    %ebx,%ebx
mov    $0x6e,%r15d
nopl   0x0(%rax)
movslq %ebx,%rbx
lea    (%r14,%rbx,4),%rdi
call   1050 <stackEmLock_release@plt>
add    $0x2000000,%ebx
add    $0xffffffffffffffff,%r15
jne    11a0 <stackEmBarrier_wait+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @stackEmBarrier_wait(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %10
10: ; preds = %15, %2
%11 = load i32*, i32** %3
%12 = call i32 @stackEmLock_acquire(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %16
15: ; preds = %10
br label %10
16: ; preds = %10
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i64 1
%19 = load i32, i32* %18
%20 = sub i32 %19, 1
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 1
store i32 %21, i32* %23
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 1
%26 = zext i1 %25 to i32
store i32 %26, i32* %5
%27 = load i32*, i32** %3
%28 = call i32 @stackEmLock_release(i32* %27)
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %52
31: ; preds = %16
%32 = load i32*, i32** %3
%33 = getelementptr inbounds i32, i32* %32, i64 2
%34 = load i32, i32* %33
%35 = load i32*, i32** %3
%36 = getelementptr inbounds i32, i32* %35, i64 1
store i32 %34, i32* %36
store i32 0, i32* %7
br label %37
37: ; preds = %48, %31
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 110
br i1 %39, label %40, label %51
40: ; preds = %37
%41 = load i32, i32* %7
%42 = shl i32 %41, 25
%43 = load i32*, i32** %4
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i32, i32* %43, i64 %44
store i32* %45, i32** %8
%46 = load i32*, i32** %8
%47 = call i32 @stackEmLock_release(i32* %46)
br label %48
48: ; preds = %40
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %37
51: ; preds = %37
br label %65
52: ; preds = %16
%53 = call i32 (...) @stackEM_getThreadID()
%54 = shl i32 %53, 25
%55 = load i32*, i32** %4
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i32, i32* %55, i64 %56
store i32* %57, i32** %9
br label %58
58: ; preds = %63, %52
%59 = load i32*, i32** %9
%60 = call i32 @stackEmLock_acquire(i32* %59)
%61 = icmp ne i32 %60, 0
%62 = xor i1 %61, true
br i1 %62, label %63, label %64
63: ; preds = %58
br label %58
64: ; preds = %58
br label %65
65: ; preds = %64, %51
ret void
}

LLVM IR (-O2):
define void @stackEmBarrier_wait(i32* %0, i32* %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32 @stackEmLock_acquire(i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %3, label %6
6: ; preds = %3
%7 = getelementptr inbounds i32, i32* %0, i64 1
%8 = load i32, i32* %7
%9 = add i32 %8, -1
store i32 %9, i32* %7
%10 = icmp slt i32 %9, 1
%11 = tail call i32 @stackEmLock_release(i32* %0)
br i1 %10, label %12, label %24
12: ; preds = %6
%13 = getelementptr inbounds i32, i32* %0, i64 2
%14 = load i32, i32* %13
store i32 %14, i32* %7
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %22, %15 ]
%17 = trunc i64 %16 to i32
%18 = shl i32 %17, 25
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %1, i64 %19
%21 = tail call i32 @stackEmLock_release(i32* %20)
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, 110
br i1 %23, label %32, label %15
24: ; preds = %6
%25 = tail call i32 (...) @stackEM_getThreadID()
%26 = shl i32 %25, 25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %1, i64 %27
br label %29
29: ; preds = %29, %24
%30 = tail call i32 @stackEmLock_acquire(i32* %28)
%31 = icmp eq i32 %30, 0
br i1 %31, label %29, label %32
32: ; preds = %29, %15
ret void
}