#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 */
int __ffs(unsigned long);
__attribute__((always_)) int find_first_bit(const unsigned long *addr,
                                            unsigned size) {
  int x = 0;
  while (x < size) {
    unsigned long val = *addr++;
    if (val)
      return __ffs(val) + x;
    x += (sizeof(*addr) << 3);
  }
  return x;
}


x86 assembly (clang, -O0):
<find_first_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1177 <find_first_bit+0x67>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1167 <find_first_bit+0x57>
mov    -0x20(%rbp),%rdi
call   1030 <__ffs@plt>
add    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117d <find_first_bit+0x6d>
movslq -0x18(%rbp),%rax
add    $0x40,%rax
mov    %eax,-0x18(%rbp)
jmp    1126 <find_first_bit+0x16>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_bit>:
push   %rbx
xor    %ebx,%ebx
test   %esi,%esi
je     113f <find_first_bit+0x2f>
nopw   0x0(%rax,%rax,1)
mov    (%rdi),%rax
test   %rax,%rax
jne    1135 <find_first_bit+0x25>
add    $0x8,%rdi
add    $0x40,%ebx
cmp    %esi,%ebx
jb     1120 <find_first_bit+0x10>
jmp    113f <find_first_bit+0x2f>
mov    %rax,%rdi
call   1030 <__ffs@plt>
add    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @find_first_bit(i64* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i64* %0, i64** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %23, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i64*, i64** %4
%14 = getelementptr inbounds i64, i64* %13, i32 1
store i64* %14, i64** %4
%15 = load i64, i64* %13
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i64, i64* %7
%20 = call i32 @__ffs(i64 %19)
%21 = load i32, i32* %6
%22 = add nsw i32 %20, %21
store i32 %22, i32* %3
br label %30
23: ; preds = %12
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = add i64 %25, 64
%27 = trunc i64 %26 to i32
store i32 %27, i32* %6
br label %8
28: ; preds = %8
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %18
%31 = load i32, i32* %3
ret i32 %31
}

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