#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 */
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
scalar_t__ counter_entry(unsigned long *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int fake_test_and_change_bit(int nr, unsigned long *addr) {
  unsigned long mask = (1UL << ((nr) % 32));
  unsigned long *p = ((unsigned long *)counter_entry(addr)) + ((nr) / 32);
  unsigned long old;
  pthread_mutex_t *lock = (pthread_mutex_t *)addr;
  pthread_mutex_lock(lock);
  old = *p;
  *p = old ^ mask;
  pthread_mutex_unlock(lock);
  return (old & mask) != 0;
}


x86 assembly (clang, -O0):
<fake_test_and_change_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <counter_entry@plt>
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <pthread_mutex_lock@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <pthread_mutex_unlock@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):
<fake_test_and_change_bit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x1f(%rbx),%ebp
test   %edi,%edi
cmovns %edi,%ebp
mov    %ebp,%eax
and    $0xffffffe0,%eax
sub    %eax,%ebx
mov    %rsi,%rdi
call   1040 <counter_entry@plt>
mov    %rax,%r15
sar    $0x5,%ebp
movslq %ebp,%rbp
mov    %r14,%rdi
call   1050 <pthread_mutex_lock@plt>
mov    (%r15,%rbp,8),%r12
mov    %r12,%rax
btc    %rbx,%rax
mov    %rax,(%r15,%rbp,8)
mov    %r14,%rdi
call   1030 <pthread_mutex_unlock@plt>
xor    %eax,%eax
bt     %rbx,%r12
setb   %al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fake_test_and_change_bit(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32*
store i32 %0, i32* %3
store i64* %1, i64** %4
%9 = load i32, i32* %3
%10 = srem i32 %9, 32
%11 = zext i32 %10 to i64
%12 = shl i64 1, %11
store i64 %12, i64* %5
%13 = load i64*, i64** %4
%14 = call i64 @counter_entry(i64* %13)
%15 = inttoptr i64 %14 to i64*
%16 = load i32, i32* %3
%17 = sdiv i32 %16, 32
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i64, i64* %15, i64 %18
store i64* %19, i64** %6
%20 = load i64*, i64** %4
%21 = bitcast i64* %20 to i32*
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = call i32 @pthread_mutex_lock(i32* %22)
%24 = load i64*, i64** %6
%25 = load i64, i64* %24
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = load i64, i64* %5
%28 = xor i64 %26, %27
%29 = load i64*, i64** %6
store i64 %28, i64* %29
%30 = load i32*, i32** %8
%31 = call i32 @pthread_mutex_unlock(i32* %30)
%32 = load i64, i64* %7
%33 = load i64, i64* %5
%34 = and i64 %32, %33
%35 = icmp ne i64 %34, 0
%36 = zext i1 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
define i32 @fake_test_and_change_bit(i32 %0, i64* %1) {
%3 = srem i32 %0, 32
%4 = zext i32 %3 to i64
%5 = shl nuw i64 1, %4
%6 = tail call i64 @counter_entry(i64* %1)
%7 = inttoptr i64 %6 to i64*
%8 = sdiv i32 %0, 32
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i64, i64* %7, i64 %9
%11 = bitcast i64* %1 to i32*
%12 = tail call i32 @pthread_mutex_lock(i32* %11)
%13 = load i64, i64* %10
%14 = xor i64 %13, %5
store i64 %14, i64* %10
%15 = tail call i32 @pthread_mutex_unlock(i32* %11)
%16 = and i64 %13, %5
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
ret i32 %18
}