int find_first_zero_bit(const unsigned long *addr, unsigned size) {
  int d0, d1, d2;
  int res;
  if (!size)
    return 0;
  asm volatile("  repe; scasq\n"
               "  je 1f\n"
               "  xorq -8(%%rdi),%%rax\n"
               "  subq $8,%%rdi\n"
               "  bsfq %%rax,%%rdx\n"
               "1:  subq %[addr],%%rdi\n"
               "  shlq $3,%%rdi\n"
               "  addq %%rdi,%%rdx"
               : "=d"(res), "=&c"(d0), "=&D"(d1), "=&a"(d2)
               : "0"(0ULL), "1"((size + 63) >> 6), "2"(addr),
                 "3"(-1ULL), [addr] "r"(addr)
               : "memory");
  return res;
}


x86 assembly (clang, -O0):
<find_first_zero_bit>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1121 <find_first_zero_bit+0x21>
movl   $0x0,-0x4(%rbp)
jmp    1170 <find_first_zero_bit+0x70>
mov    -0x14(%rbp),%ecx
add    $0x3f,%ecx
shr    $0x6,%ecx
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0xffffffffffffffff,%rax
repz scas %es:(%rdi),%rax
je     1152 <find_first_zero_bit+0x52>
xor    -0x8(%rdi),%rax
sub    $0x8,%rdi
bsf    %rax,%rdx
sub    %rsi,%rdi
shl    $0x3,%rdi
add    %rdi,%rdx
mov    %edx,-0x24(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %edi,%ecx
mov    %ecx,-0x1c(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_zero_bit>:
mov    %esi,%ecx
test   %esi,%esi
je     1137 <find_first_zero_bit+0x37>
mov    %rdi,%rsi
add    $0x3f,%ecx
shr    $0x6,%ecx
mov    $0xffffffffffffffff,%rax
xor    %edx,%edx
repz scas %es:(%rdi),%rax
je     1129 <find_first_zero_bit+0x29>
xor    -0x8(%rdi),%rax
sub    $0x8,%rdi
bsf    %rax,%rdx
sub    %rsi,%rdi
shl    $0x3,%rdi
add    %rdi,%rdx
mov    %rdx,%rax
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 %29
13: ; preds = %2
%14 = load i32, i32* %5
%15 = add i32 %14, 63
%16 = lshr i32 %15, 6
%17 = load i64*, i64** %4
%18 = load i64*, i64** %4
%19 = call { i64, i32, i64*, i64 } asm sideeffect " repe; scasq\0A je 1f\0A xorq -8(%rdi),%rax\0A subq $$8,%rdi\0A bsfq %rax,%rdx\0A1: subq $8,%rdi\0A shlq $$3,%rdi\0A addq %rdi,%rdx", "={dx},=&{cx},=&{di},=&{ax},0,1,2,3,r,~{memory},~{dirflag},~{fpsr},~{flags}"(i64 0, i32 %16, i64* %17, i64 -1, i64* %18)
%20 = extractvalue { i64, i32, i64*, i64 } %19, 0
%21 = extractvalue { i64, i32, i64*, i64 } %19, 1
%22 = extractvalue { i64, i32, i64*, i64 } %19, 2
%23 = extractvalue { i64, i32, i64*, i64 } %19, 3
%24 = trunc i64 %20 to i32
store i32 %24, i32* %9
store i32 %21, i32* %6
%25 = ptrtoint i64* %22 to i64
%26 = trunc i64 %25 to i32
store i32 %26, i32* %7
%27 = trunc i64 %23 to i32
store i32 %27, i32* %8
%28 = load i32, i32* %9
store i32 %28, i32* %3
br label %29
29: ; preds = %13, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @find_first_zero_bit(i64* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = add i32 %1, 63
%6 = lshr i32 %5, 6
%7 = tail call { i64, i32, i64*, i64 } asm sideeffect " repe; scasq\0A je 1f\0A xorq -8(%rdi),%rax\0A subq $$8,%rdi\0A bsfq %rax,%rdx\0A1: subq $8,%rdi\0A shlq $$3,%rdi\0A addq %rdi,%rdx", "={dx},=&{cx},=&{di},=&{ax},0,1,2,3,r,~{memory},~{dirflag},~{fpsr},~{flags}"(i64 0, i32 %6, i64* %0, i64 -1, i64* %0)
%8 = extractvalue { i64, i32, i64*, i64 } %7, 0
%9 = trunc i64 %8 to i32
br label %10
10: ; preds = %2, %4
%11 = phi i32 [ %9, %4 ], [ 0, %2 ]
ret i32 %11
}