int __acpi_acquire_global_lock(unsigned int *lock) {
  unsigned int old, new, val;
  do {
    old = *lock;
    new = (((old & ~0x3) + 2) + ((old >> 1) & 0x1));
    val = ((__typeof__(*(lock)))__cmpxchg(
        (lock), (unsigned long)(old), (unsigned long)(new), sizeof(*(lock))));
  } while (__builtin_expect((val != old), 0));
  return (new < 3) ? -1 : 0;
}


x86 assembly (clang, -O0):
<__acpi_acquire_global_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xfffffffc,%eax
add    $0x2,%eax
mov    -0xc(%rbp),%ecx
shr    $0x1,%ecx
and    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    %eax,%esi
mov    -0x10(%rbp),%eax
mov    %eax,%edx
mov    $0x4,%ecx
mov    $0x0,%al
call   1030 <__cmpxchg@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
cmp    $0x0,%rax
jne    111c <__acpi_acquire_global_lock+0xc>
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x3,%edx
cmovb  %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__acpi_acquire_global_lock>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%r14),%r15d
mov    %r15d,%eax
and    $0xfffffffc,%eax
mov    %r15d,%ecx
shr    %ecx
and    $0x1,%ecx
lea    (%rax,%rcx,1),%rbx
add    $0x2,%rbx
mov    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <__cmpxchg@plt>
cmp    %r15d,%eax
jne    1118 <__acpi_acquire_global_lock+0x8>
xor    %eax,%eax
cmp    $0x2,%ebx
sete   %al
neg    %eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @__acpi_acquire_global_lock(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
br label %6
6: ; preds = %22, %1
%7 = load i32*, i32** %2
%8 = load i32, i32* %7
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = and i32 %9, -4
%11 = add i32 %10, 2
%12 = load i32, i32* %3
%13 = lshr i32 %12, 1
%14 = and i32 %13, 1
%15 = add i32 %11, %14
store i32 %15, i32* %4
%16 = load i32*, i32** %2
%17 = load i32, i32* %3
%18 = zext i32 %17 to i64
%19 = load i32, i32* %4
%20 = zext i32 %19 to i64
%21 = call i32 (i32*, i64, i64, i64, ...) bitcast (i32 (...)* @__cmpxchg to i32 (i32*, i64, i64, i64, ...)*)(i32* %16, i64 %18, i64 %20, i64 4)
store i32 %21, i32* %5
br label %22
22: ; preds = %6
%23 = load i32, i32* %5
%24 = load i32, i32* %3
%25 = icmp ne i32 %23, %24
%26 = zext i1 %25 to i32
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 0
br i1 %28, label %6, label %29
29: ; preds = %22
%30 = load i32, i32* %4
%31 = icmp ult i32 %30, 3
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 -1, i32 0
ret i32 %33
}

LLVM IR (-O2):
define i32 @__acpi_acquire_global_lock(i32* %0) {
br label %2
2: ; preds = %2, %1
%3 = load i32, i32* %0
%4 = and i32 %3, -4
%5 = lshr i32 %3, 1
%6 = and i32 %5, 1
%7 = or i32 %4, %6
%8 = or i32 %7, 2
%9 = zext i32 %3 to i64
%10 = zext i32 %8 to i64
%11 = tail call i32 (i32*, i64, i64, i64, ...) bitcast (i32 (...)* @__cmpxchg to i32 (i32*, i64, i64, i64, ...)*)(i32* %0, i64 %9, i64 %10, i64 4)
%12 = icmp eq i32 %11, %3
br i1 %12, label %13, label %2
13: ; preds = %2
%14 = icmp eq i32 %8, 2
%15 = sext i1 %14 to i32
ret i32 %15
}