int find_first_zero_bit(const unsigned long *addr, unsigned size) {
  int d0, d1, d2;
  int res;
  if (!size)
    return 0;
  __asm__ __volatile__("movl $-1,%%eax\n\t"
                       "xorl %%edx,%%edx\n\t"
                       "repe; scasl\n\t"
                       "je 1f\n\t"
                       "xorl -4(%%edi),%%eax\n\t"
                       "subl $4,%%edi\n\t"
                       "bsfl %%eax,%%edx\n"
                       "1:\tsubl %%ebx,%%edi\n\t"
                       "shll $3,%%edi\n\t"
                       "addl %%edi,%%edx"
                       : "=d"(res), "=&c"(d0), "=&D"(d1), "=&a"(d2)
                       : "1"((size + 31) >> 5), "2"(addr), "b"(addr)
                       : "memory");
  return res;
}


x86 assembly (clang, -O0):
<find_first_zero_bit>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1122 <find_first_zero_bit+0x22>
movl   $0x0,-0xc(%rbp)
jmp    1167 <find_first_zero_bit+0x67>
mov    -0x1c(%rbp),%ecx
add    $0x1f,%ecx
shr    $0x5,%ecx
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rbx
mov    $0xffffffff,%eax
xor    %edx,%edx
repz scas %es:(%rdi),%eax
je     114c <find_first_zero_bit+0x4c>
xor    -0x4(%edi),%eax
sub    $0x4,%edi
bsf    %eax,%edx
sub    %ebx,%edi
shl    $0x3,%edi
add    %edi,%edx
mov    %edx,-0x2c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %edi,%ecx
mov    %ecx,-0x24(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_zero_bit>:
test   %esi,%esi
je     1130 <find_first_zero_bit+0x30>
push   %rbx
add    $0x1f,%esi
shr    $0x5,%esi
mov    %rdi,%rbx
mov    %esi,%ecx
mov    $0xffffffff,%eax
xor    %edx,%edx
repz scas %es:(%rdi),%eax
je     1125 <find_first_zero_bit+0x25>
xor    -0x4(%edi),%eax
sub    $0x4,%edi
bsf    %eax,%edx
sub    %ebx,%edi
shl    $0x3,%edi
add    %edi,%edx
mov    %edx,%eax
pop    %rbx
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @find_first_zero_bit(i64* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64* %0, i64** %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 0, i32* %3
br label %27
13: ; preds = %2
%14 = load i32, i32* %5
%15 = add i32 %14, 31
%16 = lshr i32 %15, 5
%17 = load i64*, i64** %4
%18 = load i64*, i64** %4
%19 = call { i32, i32, i64*, i32 } asm sideeffect "movl $$-1,%eax\0A\09xorl %edx,%edx\0A\09repe; scasl\0A\09je 1f\0A\09xorl -4(%edi),%eax\0A\09subl $$4,%edi\0A\09bsfl %eax,%edx\0A1:\09subl %ebx,%edi\0A\09shll $$3,%edi\0A\09addl %edi,%edx", "={dx},=&{cx},=&{di},=&{ax},1,2,{bx},~{memory},~{dirflag},~{fpsr},~{flags}"(i32 %16, i64* %17, i64* %18)
%20 = extractvalue { i32, i32, i64*, i32 } %19, 0
%21 = extractvalue { i32, i32, i64*, i32 } %19, 1
%22 = extractvalue { i32, i32, i64*, i32 } %19, 2
%23 = extractvalue { i32, i32, i64*, i32 } %19, 3
store i32 %20, i32* %9
store i32 %21, i32* %6
%24 = ptrtoint i64* %22 to i64
%25 = trunc i64 %24 to i32
store i32 %25, i32* %7
store i32 %23, i32* %8
%26 = load i32, i32* %9
store i32 %26, i32* %3
br label %27
27: ; preds = %13, %12
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @find_first_zero_bit(i64* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = add i32 %1, 31
%6 = lshr i32 %5, 5
%7 = tail call { i32, i32, i64*, i32 } asm sideeffect "movl $$-1,%eax\0A\09xorl %edx,%edx\0A\09repe; scasl\0A\09je 1f\0A\09xorl -4(%edi),%eax\0A\09subl $$4,%edi\0A\09bsfl %eax,%edx\0A1:\09subl %ebx,%edi\0A\09shll $$3,%edi\0A\09addl %edi,%edx", "={dx},=&{cx},=&{di},=&{ax},1,2,{bx},~{memory},~{dirflag},~{fpsr},~{flags}"(i32 %6, i64* %0, i64* %0)
%8 = extractvalue { i32, i32, i64*, i32 } %7, 0
br label %9
9: ; preds = %2, %4
%10 = phi i32 [ %8, %4 ], [ 0, %2 ]
ret i32 %10
}