int test_and_set_bit(int nr, volatile void *addr) {
  int mask, retval;
  volatile unsigned int *a = addr;
  unsigned long flags;
  a += nr >> 5;
  mask = 1 << (nr & 0x1f);
  local_irq_save(flags);
  retval = (mask & *a) != 0;
  *a |= mask;
  local_irq_restore(flags);
  return retval;
}


x86 assembly (clang, -O0):
<test_and_set_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%ecx
sar    $0x5,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%ecx
and    $0x1f,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1030 <local_irq_save@plt>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
or     %edx,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1040 <local_irq_restore@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_and_set_bit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edi,%eax
sar    $0x5,%eax
movslq %eax,%r15
mov    $0x1,%r12d
mov    %ebp,%ecx
shl    %cl,%r12d
xor    %eax,%eax
call   1030 <local_irq_save@plt>
mov    (%r14,%r15,4),%ebx
mov    %ebp,%ecx
shr    %cl,%ebx
and    $0x1,%ebx
or     %r12d,(%r14,%r15,4)
xor    %eax,%eax
call   1040 <local_irq_restore@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @test_and_set_bit(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %7
%11 = load i32, i32* %3
%12 = ashr i32 %11, 5
%13 = load i32*, i32** %7
%14 = sext i32 %12 to i64
%15 = getelementptr inbounds i32, i32* %13, i64 %14
store i32* %15, i32** %7
%16 = load i32, i32* %3
%17 = and i32 %16, 31
%18 = shl i32 1, %17
store i32 %18, i32* %5
%19 = load i64, i64* %8
%20 = call i32 (i64, ...) bitcast (i32 (...)* @local_irq_save to i32 (i64, ...)*)(i64 %19)
%21 = load i32, i32* %5
%22 = load i32*, i32** %7
%23 = load volatile i32, i32* %22
%24 = and i32 %21, %23
%25 = icmp ne i32 %24, 0
%26 = zext i1 %25 to i32
store i32 %26, i32* %6
%27 = load i32, i32* %5
%28 = load i32*, i32** %7
%29 = load volatile i32, i32* %28
%30 = or i32 %29, %27
store volatile i32 %30, i32* %28
%31 = load i64, i64* %8
%32 = call i32 (i64, ...) bitcast (i32 (...)* @local_irq_restore to i32 (i64, ...)*)(i64 %31)
%33 = load i32, i32* %6
ret i32 %33
}

LLVM IR (-O2):
define i32 @test_and_set_bit(i32 %0, i8* %1) {
%3 = bitcast i8* %1 to i32*
%4 = ashr i32 %0, 5
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i32, i32* %3, i64 %5
%7 = and i32 %0, 31
%8 = shl nuw i32 1, %7
%9 = tail call i32 (i64, ...) bitcast (i32 (...)* @local_irq_save to i32 (i64, ...)*)(i64 undef)
%10 = load volatile i32, i32* %6
%11 = lshr i32 %10, %7
%12 = and i32 %11, 1
%13 = load volatile i32, i32* %6
%14 = or i32 %13, %8
store volatile i32 %14, i32* %6
%15 = tail call i32 (i64, ...) bitcast (i32 (...)* @local_irq_restore to i32 (i64, ...)*)(i64 undef)
ret i32 %12
}