#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 */
typedef int uint64_t;

/* Variables and functions */
int UINT64_MAX;
int ffsll(int const);
uint64_t find_first_bit_heap(const uint64_t *ptr, const uint64_t *const end,
                             uint64_t start) {
  if (start > 63) {
    const uint64_t mask = (1ull << 6) - 1;
    const uint64_t remainder = (start & mask);
    ptr += (start >> 6);
    start >>= remainder;
  }
  for (; ptr != end; ptr++) {
    const uint64_t loc = ffsll(*ptr);
    if (loc != 0) {
      return (loc - 1);
    }
  }
  return UINT64_MAX;
}


x86 assembly (clang, -O0):
<find_first_bit_heap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x3f,-0x1c(%rbp)
jle    1160 <find_first_bit_heap+0x50>
movl   $0x3f,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x3f,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%ecx
sar    $0x6,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x1c(%rbp)
jmp    1165 <find_first_bit_heap+0x55>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11af <find_first_bit_heap+0x9f>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ffsll@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1199 <find_first_bit_heap+0x89>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <find_first_bit_heap+0xab>
jmp    119e <find_first_bit_heap+0x8e>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
jmp    1165 <find_first_bit_heap+0x55>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_bit_heap>:
mov    %edx,%eax
sar    $0x6,%eax
xor    %ecx,%ecx
cmp    $0x40,%edx
cmovge %eax,%ecx
lea    (%rdi,%rcx,4),%rax
cmp    %rsi,%rax
je     112f <find_first_bit_heap+0x2f>
cs nopw 0x0(%rax,%rax,1)
mov    (%rax),%ecx
test   %ecx,%ecx
jne    1139 <find_first_bit_heap+0x39>
add    $0x4,%rax
cmp    %rsi,%rax
jne    1120 <find_first_bit_heap+0x20>
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
ret
bsf    %ecx,%eax
ret

LLVM IR (-O0):
@UINT64_MAX = global i32 0
define i32 @find_first_bit_heap(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 63
br i1 %12, label %13, label %24
13: ; preds = %3
store i32 63, i32* %8
%14 = load i32, i32* %7
%15 = and i32 %14, 63
store i32 %15, i32* %9
%16 = load i32, i32* %7
%17 = ashr i32 %16, 6
%18 = load i32*, i32** %5
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32* %20, i32** %5
%21 = load i32, i32* %9
%22 = load i32, i32* %7
%23 = ashr i32 %22, %21
store i32 %23, i32* %7
br label %24
24: ; preds = %13, %3
br label %25
25: ; preds = %39, %24
%26 = load i32*, i32** %5
%27 = load i32*, i32** %6
%28 = icmp ne i32* %26, %27
br i1 %28, label %29, label %42
29: ; preds = %25
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = call i32 @ffsll(i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* %10
%37 = sub nsw i32 %36, 1
store i32 %37, i32* %4
br label %44
38: ; preds = %29
br label %39
39: ; preds = %38
%40 = load i32*, i32** %5
%41 = getelementptr inbounds i32, i32* %40, i32 1
store i32* %41, i32** %5
br label %25
42: ; preds = %25
%43 = load i32, i32* @UINT64_MAX
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %35
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@UINT64_MAX = global i32 0
define i32 @find_first_bit_heap(i32* %0, i32* readnone %1, i32 %2) {
%4 = icmp sgt i32 %2, 63
%5 = ashr i32 %2, 6
%6 = select i1 %4, i32 %5, i32 0
%7 = zext i32 %6 to i64
%8 = getelementptr i32, i32* %0, i64 %7
%9 = icmp eq i32* %8, %1
br i1 %9, label %17, label %12
10: ; preds = %12
%11 = icmp eq i32* %16, %1
br i1 %11, label %17, label %12
12: ; preds = %3, %10
%13 = phi i32* [ %16, %10 ], [ %8, %3 ]
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
%16 = getelementptr inbounds i32, i32* %13, i64 1
br i1 %15, label %10, label %19
17: ; preds = %10, %3
%18 = load i32, i32* @UINT64_MAX
br label %21
19: ; preds = %12
%20 = tail call i32 @llvm.cttz.i32(i32 %14, i1 true)
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
ret i32 %22
}