#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 bit_first_in_long(unsigned long);
int /*<<< orphan*/ memcpy(unsigned long *, void const *, size_t);
int bit_first(const void *pData, size_t uLen) {
  int iRet = 0;
  while (uLen >= sizeof(long)) {
    unsigned long lCur = *(const unsigned long *)pData;
    if (lCur)
      return iRet + bit_first_in_long(lCur);
    iRet += sizeof(long) * 8;
    uLen -= sizeof(long);
    pData = (const char *)pData + sizeof(long);
  }
  if (uLen > 0) {
    unsigned long lCur = 0;
    memcpy(&lCur, pData, uLen);
    if (lCur)
      return iRet + bit_first_in_long(lCur);
  }
  return -1;
}


x86 assembly (clang, -O0):
<bit_first>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x8,-0x18(%rbp)
jb     119e <bit_first+0x7e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1176 <bit_first+0x56>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <bit_first_in_long@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <bit_first+0xd7>
movslq -0x1c(%rbp),%rax
add    $0x40,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    1137 <bit_first+0x17>
cmpq   $0x0,-0x18(%rbp)
jbe    11f0 <bit_first+0xd0>
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1030 <memcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     11eb <bit_first+0xcb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <bit_first_in_long@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <bit_first+0xd7>
jmp    11f0 <bit_first+0xd0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_first>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rdx
mov    %rdi,%rsi
xor    %r14d,%r14d
cmp    $0x8,%rdx
jb     1169 <bit_first+0x49>
lea    0x0(,%rdx,8),%r14d
and    $0xffffffc0,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsi),%rdi
test   %rdi,%rdi
jne    119c <bit_first+0x7c>
add    $0xfffffffffffffff8,%rdx
add    $0x8,%rsi
add    $0xffffffc0,%ebp
cmp    $0x7,%rdx
ja     1150 <bit_first+0x30>
mov    $0xffffffff,%ebx
test   %rdx,%rdx
je     11a5 <bit_first+0x85>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1030 <memcpy@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11a5 <bit_first+0x85>
call   1040 <bit_first_in_long@plt>
mov    %eax,%ebx
add    %r14d,%ebx
jmp    11a5 <bit_first+0x85>
call   1040 <bit_first_in_long@plt>
mov    %eax,%ebx
sub    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bit_first(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
br label %9
9: ; preds = %23, %2
%10 = load i64, i64* %5
%11 = icmp uge i64 %10, 8
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = bitcast i8* %13 to i64*
%15 = load i64, i64* %14
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 i32, i32* %6
%20 = load i64, i64* %7
%21 = call i32 @bit_first_in_long(i64 %20)
%22 = add nsw i32 %19, %21
store i32 %22, i32* %3
br label %48
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
%28 = load i64, i64* %5
%29 = sub i64 %28, 8
store i64 %29, i64* %5
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 8
store i8* %31, i8** %4
br label %9
32: ; preds = %9
%33 = load i64, i64* %5
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %32
store i64 0, i64* %8
%36 = load i8*, i8** %4
%37 = load i64, i64* %5
%38 = call i32 @memcpy(i64* %8, i8* %36, i64 %37)
%39 = load i64, i64* %8
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* %6
%43 = load i64, i64* %8
%44 = call i32 @bit_first_in_long(i64 %43)
%45 = add nsw i32 %42, %44
store i32 %45, i32* %3
br label %48
46: ; preds = %35
br label %47
47: ; preds = %46, %32
store i32 -1, i32* %3
br label %48
48: ; preds = %47, %41, %18
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @bit_first(i8* %0, i64 %1) {
%3 = alloca i64
%4 = icmp ugt i64 %1, 7
br i1 %4, label %5, label %24
5: ; preds = %2
%6 = trunc i64 %1 to i32
%7 = shl i32 %6, 3
%8 = and i32 %7, -64
br label %9
9: ; preds = %5, %19
%10 = phi i32 [ %20, %19 ], [ 0, %5 ]
%11 = phi i64 [ %21, %19 ], [ %1, %5 ]
%12 = phi i8* [ %22, %19 ], [ %0, %5 ]
%13 = bitcast i8* %12 to i64*
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = tail call i32 @bit_first_in_long(i64 %14)
%18 = add nsw i32 %17, %10
br label %38
19: ; preds = %9
%20 = add i32 %10, 64
%21 = add i64 %11, -8
%22 = getelementptr inbounds i8, i8* %12, i64 8
%23 = icmp ugt i64 %21, 7
br i1 %23, label %9, label %24
24: ; preds = %19, %2
%25 = phi i8* [ %0, %2 ], [ %22, %19 ]
%26 = phi i64 [ %1, %2 ], [ %21, %19 ]
%27 = phi i32 [ 0, %2 ], [ %8, %19 ]
%28 = icmp eq i64 %26, 0
br i1 %28, label %38, label %29
29: ; preds = %24
%30 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%31 = call i32 @memcpy(i64* %3, i8* %25, i64 %26)
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %38
35: ; preds = %29
%36 = call i32 @bit_first_in_long(i64 %32)
%37 = add nsw i32 %36, %27
br label %38
38: ; preds = %24, %34, %35, %16
%39 = phi i32 [ %37, %35 ], [ %18, %16 ], [ -1, %34 ], [ -1, %24 ]
ret i32 %39
}