#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 */
unsigned long BIT_MASK(int);
int BIT_WORD(int);
int /*<<< orphan*/ local_irq_restore(unsigned long);
int /*<<< orphan*/ local_irq_save(unsigned long);
int test_and_clear_bit(int nr, volatile unsigned long *addr) {
  unsigned long mask = BIT_MASK(nr);
  unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  unsigned long old;
  unsigned long flags;
  do {
    local_irq_save(flags);
  } while (0);
  old = *p;
  *p = old & ~mask;
  do {
    local_irq_restore(flags);
  } while (0);
  return (old & mask) != 0;
}


x86 assembly (clang, -O0):
<test_and_clear_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <BIT_MASK@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <BIT_WORD@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <local_irq_save@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
xor    $0xffffffffffffffff,%rax
and    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <local_irq_restore@plt>
mov    -0x28(%rbp),%rax
and    -0x18(%rbp),%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_and_clear_bit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <BIT_MASK@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1060 <BIT_WORD@plt>
movslq %eax,%rbp
call   1040 <local_irq_save@plt>
mov    (%r14,%rbp,8),%rbx
mov    %r15,%rax
not    %rax
and    %rbx,%rax
mov    %rax,(%r14,%rbp,8)
call   1050 <local_irq_restore@plt>
xor    %eax,%eax
test   %r15,%rbx
setne  %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @test_and_clear_bit(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %3
store i64* %1, i64** %4
%9 = load i32, i32* %3
%10 = call i64 @BIT_MASK(i32 %9)
store i64 %10, i64* %5
%11 = load i64*, i64** %4
%12 = load i32, i32* %3
%13 = call i32 @BIT_WORD(i32 %12)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i64, i64* %11, i64 %14
store i64* %15, i64** %6
br label %16
16: ; preds = %2
%17 = load i64, i64* %8
%18 = call i32 @local_irq_save(i64 %17)
br label %19
19: ; preds = %16
%20 = load i64*, i64** %6
%21 = load i64, i64* %20
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = load i64, i64* %5
%24 = xor i64 %23, -1
%25 = and i64 %22, %24
%26 = load i64*, i64** %6
store i64 %25, i64* %26
br label %27
27: ; preds = %19
%28 = load i64, i64* %8
%29 = call i32 @local_irq_restore(i64 %28)
br label %30
30: ; preds = %27
%31 = load i64, i64* %7
%32 = load i64, i64* %5
%33 = and i64 %31, %32
%34 = icmp ne i64 %33, 0
%35 = zext i1 %34 to i32
ret i32 %35
}

LLVM IR (-O2):
define i32 @test_and_clear_bit(i32 %0, i64* nocapture %1) {
%3 = tail call i64 @BIT_MASK(i32 %0)
%4 = tail call i32 @BIT_WORD(i32 %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i64, i64* %1, i64 %5
%7 = tail call i32 @local_irq_save(i64 undef)
%8 = load i64, i64* %6
%9 = xor i64 %3, -1
%10 = and i64 %8, %9
store i64 %10, i64* %6
%11 = tail call i32 @local_irq_restore(i64 undef)
%12 = and i64 %8, %3
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
ret i32 %14
}