void LOCK(int *q) {
  int i;
  while (__sync_lock_test_and_set(q, 1)) {
    for (i = 0; i < 32; i++) {
      __asm__ __volatile__("pause");
    }
    sched_yield();
  }
}


x86 assembly (clang, -O0):
<LOCK>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rcx
mov    $0x1,%eax
xchg   %eax,(%rcx)
cmp    $0x0,%eax
je     115d <LOCK+0x4d>
movl   $0x0,-0xc(%rbp)
cmpl   $0x20,-0xc(%rbp)
jge    1151 <LOCK+0x41>
pause
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1137 <LOCK+0x27>
mov    $0x0,%al
call   1030 <sched_yield@plt>
jmp    111c <LOCK+0xc>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LOCK>:
push   %rbx
mov    $0x1,%eax
xchg   %eax,(%rdi)
test   %eax,%eax
je     1172 <LOCK+0x62>
mov    %rdi,%rbx
nop
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
pause
xor    %eax,%eax
call   1030 <sched_yield@plt>
mov    $0x1,%eax
xchg   %eax,(%rbx)
test   %eax,%eax
jne    1120 <LOCK+0x10>
pop    %rbx
ret

LLVM IR (-O0):
define void @LOCK(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
store i32* %0, i32** %2
br label %4
4: ; preds = %16, %1
%5 = load i32*, i32** %2
%6 = atomicrmw xchg i32* %5, i32 1 seq_cst
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %4
store i32 0, i32* %3
br label %9
9: ; preds = %13, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 32
br i1 %11, label %12, label %16
12: ; preds = %9
br label %13
13: ; preds = %12
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
store i32 %15, i32* %3
br label %9
16: ; preds = %9
%17 = call i32 (...) @sched_yield()
br label %4
18: ; preds = %4
ret void
}

LLVM IR (-O2):
define void @LOCK(i32* nocapture %0) {
%2 = atomicrmw xchg i32* %0, i32 1 seq_cst
%3 = icmp eq i32 %2, 0
br i1 %3, label %8, label %4
4: ; preds = %1, %4
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
tail call void asm sideeffect "pause", "~{dirflag},~{fpsr},~{flags}"()
%5 = tail call i32 (...) @sched_yield()
%6 = atomicrmw xchg i32* %0, i32 1 seq_cst
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %4
8: ; preds = %4, %1
ret void
}