#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_set_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_set_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
or     -0x18(%rbp),%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_set_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    %rbx,%rax
or     %r15,%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_set_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 = or i64 %22, %23
%25 = load i64*, i64** %6
store i64 %24, i64* %25
br label %26
26: ; preds = %19
%27 = load i64, i64* %8
%28 = call i32 @local_irq_restore(i64 %27)
br label %29
29: ; preds = %26
%30 = load i64, i64* %7
%31 = load i64, i64* %5
%32 = and i64 %30, %31
%33 = icmp ne i64 %32, 0
%34 = zext i1 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
define i32 @test_and_set_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 = or i64 %8, %3
store i64 %9, i64* %6
%10 = tail call i32 @local_irq_restore(i64 undef)
%11 = and i64 %8, %3
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
ret i32 %13
}