#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 internal_atomic_cmpxchg(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ internal_atomic_exchange(int *, int);
int internal_atomic_get(int *);
int /*<<< orphan*/ isleep(int);
void ithread_once(int *control, void (*run_once)(void)) {
  if (internal_atomic_get(control) != 2) {
    int last;
    last = internal_atomic_cmpxchg(control, 1, 0);
    if (last == 0) {
      if (run_once) {
        run_once();
      }
      internal_atomic_exchange(control, 2);
    } else {
      while (internal_atomic_get(control) != 2)
        isleep(1);
    }
  }
}


x86 assembly (clang, -O0):
<ithread_once>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <internal_atomic_get@plt>
cmp    $0x2,%eax
je     11d0 <ithread_once+0x90>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <internal_atomic_cmpxchg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a0 <ithread_once+0x60>
cmpq   $0x0,-0x10(%rbp)
je     118d <ithread_once+0x4d>
call   *-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1050 <internal_atomic_exchange@plt>
jmp    11cb <ithread_once+0x8b>
jmp    11a5 <ithread_once+0x65>
mov    -0x8(%rbp),%rdi
call   1030 <internal_atomic_get@plt>
cmp    $0x2,%eax
je     11c6 <ithread_once+0x86>
mov    $0x1,%edi
call   1060 <isleep@plt>
jmp    11a5 <ithread_once+0x65>
jmp    11cb <ithread_once+0x8b>
jmp    11d0 <ithread_once+0x90>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ithread_once>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <internal_atomic_get@plt>
cmp    $0x2,%eax
jne    115c <ithread_once+0x1c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <internal_atomic_cmpxchg@plt>
test   %eax,%eax
je     1189 <ithread_once+0x49>
nop
mov    %rbx,%rdi
call   1030 <internal_atomic_get@plt>
cmp    $0x2,%eax
je     1154 <ithread_once+0x14>
mov    $0x1,%edi
call   1060 <isleep@plt>
jmp    1170 <ithread_once+0x30>
test   %r14,%r14
je     1191 <ithread_once+0x51>
call   *%r14
mov    %rbx,%rdi
mov    $0x2,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <internal_atomic_exchange@plt>

LLVM IR (-O0):
define void @ithread_once(i32* %0, void ()* %1) {
%3 = alloca i32*
%4 = alloca void ()*
%5 = alloca i32
store i32* %0, i32** %3
store void ()* %1, void ()** %4
%6 = load i32*, i32** %3
%7 = call i32 @internal_atomic_get(i32* %6)
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %31
9: ; preds = %2
%10 = load i32*, i32** %3
%11 = call i32 @internal_atomic_cmpxchg(i32* %10, i32 1, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load void ()*, void ()** %4
%16 = icmp ne void ()* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load void ()*, void ()** %4
br label %19
19: ; preds = %17, %14
%20 = load i32*, i32** %3
%21 = call i32 @internal_atomic_exchange(i32* %20, i32 2)
br label %30
22: ; preds = %9
br label %23
23: ; preds = %27, %22
%24 = load i32*, i32** %3
%25 = call i32 @internal_atomic_get(i32* %24)
%26 = icmp ne i32 %25, 2
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @isleep(i32 1)
br label %23
29: ; preds = %23
br label %30
30: ; preds = %29, %19
br label %31
31: ; preds = %30, %2
ret void
}

LLVM IR (-O2):
define void @ithread_once(i32* %0, void ()* %1) {
%3 = tail call i32 @internal_atomic_get(i32* %0)
%4 = icmp eq i32 %3, 2
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = tail call i32 @internal_atomic_cmpxchg(i32* %0, i32 1, i32 0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @internal_atomic_get(i32* %0)
%10 = icmp eq i32 %9, 2
br i1 %10, label %20, label %16
11: ; preds = %5
%12 = icmp eq void ()* %1, null
br i1 %12, label %14, label %13
13: ; preds = %11
tail call void %1()
br label %14
14: ; preds = %13, %11
%15 = tail call i32 @internal_atomic_exchange(i32* %0, i32 2)
br label %20
16: ; preds = %8, %16
%17 = tail call i32 @isleep(i32 1)
%18 = tail call i32 @internal_atomic_get(i32* %0)
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %16
20: ; preds = %16, %8, %14, %2
ret void
}