unsigned long find_first_zero_bit(const unsigned long *addr,
                                  unsigned long size) {
  if (small_const_nbits(size)) {
    unsigned long val = *addr | ~GENMASK(size - 1, 0);
    return val == ~0UL ? size : ffz(val);
  }
  return _find_first_zero_bit(addr, size);
}


x86 assembly (clang, -O0):
<find_first_zero_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1050 <small_const_nbits@plt>
cmp    $0x0,%eax
je     11c9 <find_first_zero_bit+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
sub    $0x1,%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1040 <GENMASK@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
xor    $0xffffffff,%ecx
movslq %ecx,%rcx
or     %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11ab <find_first_zero_bit+0x6b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11bc <find_first_zero_bit+0x7c>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <ffz@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11de <find_first_zero_bit+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1030 <_find_first_zero_bit@plt>
cltq
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_zero_bit>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
xor    %eax,%eax
call   1050 <small_const_nbits@plt>
test   %eax,%eax
je     117f <find_first_zero_bit+0x3f>
mov    (%r14),%r14
lea    -0x1(%rbx),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <GENMASK@plt>
not    %eax
movslq %eax,%rdi
or     %r14,%rdi
cmp    $0xffffffffffffffff,%rdi
je     118f <find_first_zero_bit+0x4f>
xor    %eax,%eax
call   1060 <ffz@plt>
jmp    118c <find_first_zero_bit+0x4c>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <_find_first_zero_bit@plt>
movslq %eax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @find_first_zero_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 i32 (i64, ...) bitcast (i32 (...)* @small_const_nbits to i32 (i64, ...)*)(i64 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %29
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 i32 (i64, i32, ...) bitcast (i32 (...)* @GENMASK to i32 (i64, i32, ...)*)(i64 %14, i32 0)
%16 = xor i32 %15, -1
%17 = sext i32 %16 to i64
%18 = or i64 %12, %17
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = load i64, i64* %5
br label %27
23: ; preds = %10
%24 = load i64, i64* %6
%25 = call i32 (i64, ...) bitcast (i32 (...)* @ffz to i32 (i64, ...)*)(i64 %24)
%26 = sext i32 %25 to i64
br label %27
27: ; preds = %23, %21
%28 = phi i64 [ %22, %21 ], [ %26, %23 ]
store i64 %28, i64* %3
br label %34
29: ; preds = %2
%30 = load i64*, i64** %4
%31 = load i64, i64* %5
%32 = call i32 (i64*, i64, ...) bitcast (i32 (...)* @_find_first_zero_bit to i32 (i64*, i64, ...)*)(i64* %30, i64 %31)
%33 = sext i32 %32 to i64
store i64 %33, i64* %3
br label %34
34: ; preds = %29, %27
%35 = load i64, i64* %3
ret i64 %35
}

LLVM IR (-O2):
define i64 @find_first_zero_bit(i64* %0, i64 %1) {
%3 = tail call i32 (i64, ...) bitcast (i32 (...)* @small_const_nbits to i32 (i64, ...)*)(i64 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = load i64, i64* %0
%7 = add i64 %1, -1
%8 = tail call i32 (i64, i32, ...) bitcast (i32 (...)* @GENMASK to i32 (i64, i32, ...)*)(i64 %7, i32 0)
%9 = xor i32 %8, -1
%10 = sext i32 %9 to i64
%11 = or i64 %6, %10
%12 = icmp eq i64 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %5
%14 = tail call i32 (i64, ...) bitcast (i32 (...)* @ffz to i32 (i64, ...)*)(i64 %11)
%15 = sext i32 %14 to i64
br label %19
16: ; preds = %2
%17 = tail call i32 (i64*, i64, ...) bitcast (i32 (...)* @_find_first_zero_bit to i32 (i64*, i64, ...)*)(i64* %0, i64 %1)
%18 = sext i32 %17 to i64
br label %19
19: ; preds = %13, %5, %16
%20 = phi i64 [ %18, %16 ], [ %15, %13 ], [ %1, %5 ]
ret i64 %20
}