#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 const GENMASK(unsigned long, int /*<<< orphan*/);
unsigned long __ffs(unsigned long);
unsigned long _find_first_bit(unsigned long const *, unsigned long);
scalar_t__ small_const_nbits(unsigned long);
unsigned long find_first_bit(const unsigned long *addr, unsigned long size) {
  if (small_const_nbits(size)) {
    unsigned long val = *addr & GENMASK(size - 1, 0);
    return val ? __ffs(val) : size;
  }
  return _find_first_bit(addr, size);
}


x86 assembly (clang, -O0):
<find_first_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <small_const_nbits@plt>
cmp    $0x0,%rax
je     11bd <find_first_bit+0x7d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
sub    $0x1,%rdi
xor    %esi,%esi
call   1050 <GENMASK@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a8 <find_first_bit+0x68>
mov    -0x20(%rbp),%rdi
call   1040 <__ffs@plt>
mov    %rax,-0x30(%rbp)
jmp    11b0 <find_first_bit+0x70>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <find_first_bit+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <_find_first_bit@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_bit>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <small_const_nbits@plt>
test   %rax,%rax
je     1179 <find_first_bit+0x39>
mov    (%r14),%r14
lea    -0x1(%rbx),%rdi
xor    %esi,%esi
call   1050 <GENMASK@plt>
and    %r14,%rax
je     118b <find_first_bit+0x4b>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <__ffs@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <_find_first_bit@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @find_first_bit(i64* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64
store i64* %0, i64** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = call i64 @small_const_nbits(i64 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %2
%11 = load i64*, i64** %4
%12 = load i64, i64* %11
%13 = load i64, i64* %5
%14 = sub i64 %13, 1
%15 = call i64 @GENMASK(i64 %14, i32 0)
%16 = and i64 %12, %15
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = load i64, i64* %6
%21 = call i64 @__ffs(i64 %20)
br label %24
22: ; preds = %10
%23 = load i64, i64* %5
br label %24
24: ; preds = %22, %19
%25 = phi i64 [ %21, %19 ], [ %23, %22 ]
store i64 %25, i64* %3
br label %30
26: ; preds = %2
%27 = load i64*, i64** %4
%28 = load i64, i64* %5
%29 = call i64 @_find_first_bit(i64* %27, i64 %28)
store i64 %29, i64* %3
br label %30
30: ; preds = %26, %24
%31 = load i64, i64* %3
ret i64 %31
}

LLVM IR (-O2):
define i64 @find_first_bit(i64* %0, i64 %1) {
%3 = tail call i64 @small_const_nbits(i64 %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = load i64, i64* %0
%7 = add i64 %1, -1
%8 = tail call i64 @GENMASK(i64 %7, i32 0)
%9 = and i64 %8, %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %5
%12 = tail call i64 @__ffs(i64 %9)
br label %15
13: ; preds = %2
%14 = tail call i64 @_find_first_bit(i64* %0, i64 %1)
br label %15
15: ; preds = %11, %5, %13
%16 = phi i64 [ %14, %13 ], [ %12, %11 ], [ %1, %5 ]
ret i64 %16
}