#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 */
unsigned long clz64(unsigned long);
unsigned long find_last_bit(const unsigned long *addr, unsigned long size) {
  unsigned long words;
  unsigned long tmp;
  words = size / (sizeof(unsigned long) * 8);
  if (size & ((sizeof(unsigned long) * 8) - 1)) {
    tmp =
        (addr[words] & (~0UL >> ((sizeof(unsigned long) * 8) -
                                 (size & ((sizeof(unsigned long) * 8) - 1)))));
    if (tmp) {
      goto found;
    }
  }
  while (words) {
    tmp = addr[--words];
    if (tmp) {
    found:
      return words * (sizeof(unsigned long) * 8) + (sizeof(unsigned long) * 8) -
             1 - clz64(tmp);
    }
  }
  return size;
}


x86 assembly (clang, -O0):
<find_last_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x6,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    $0x3f,%rax
cmp    $0x0,%rax
je     1183 <find_last_bit+0x73>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x18(%rbp),%rdx
and    $0x3f,%rdx
mov    $0x40,%ecx
sub    %rdx,%rcx
mov    $0xffffffffffffffff,%rdx
shr    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     117e <find_last_bit+0x6e>
jmp    11bf <find_last_bit+0xaf>
jmp    1183 <find_last_bit+0x73>
jmp    1188 <find_last_bit+0x78>
cmpq   $0x0,-0x20(%rbp)
je     11f4 <find_last_bit+0xe4>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
mov    -0x8(%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ef <find_last_bit+0xdf>
jmp    11bf <find_last_bit+0xaf>
mov    -0x20(%rbp),%rax
shl    $0x6,%rax
add    $0x40,%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <clz64@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11fc <find_last_bit+0xec>
jmp    1188 <find_last_bit+0x78>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_last_bit>:
push   %rbx
mov    %rsi,%rax
mov    %rsi,%rbx
shr    $0x6,%rbx
mov    %rsi,%rcx
and    $0x3f,%rcx
je     114d <find_last_bit+0x3d>
mov    (%rdi,%rbx,8),%rdx
neg    %cl
shl    %cl,%rdx
shr    %cl,%rdx
test   %rdx,%rdx
je     114d <find_last_bit+0x3d>
shl    $0x6,%rbx
or     $0x3f,%rbx
mov    %rdx,%rdi
call   1030 <clz64@plt>
sub    %rax,%rbx
mov    %rbx,%rax
jmp    115e <find_last_bit+0x4e>
sub    $0x1,%rbx
jb     115e <find_last_bit+0x4e>
mov    (%rdi,%rbx,8),%rdx
test   %rdx,%rdx
je     114d <find_last_bit+0x3d>
jmp    1135 <find_last_bit+0x25>
pop    %rbx
ret

LLVM IR (-O0):
define i64 @find_last_bit(i64* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64* %0, i64** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = udiv i64 %8, 64
store i64 %9, i64* %6
%10 = load i64, i64* %5
%11 = and i64 %10, 63
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %2
%14 = load i64*, i64** %4
%15 = load i64, i64* %6
%16 = getelementptr inbounds i64, i64* %14, i64 %15
%17 = load i64, i64* %16
%18 = load i64, i64* %5
%19 = and i64 %18, 63
%20 = sub i64 64, %19
%21 = lshr i64 -1, %20
%22 = and i64 %17, %21
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %13
br label %40
26: ; preds = %13
br label %27
27: ; preds = %26, %2
br label %28
28: ; preds = %48, %27
%29 = load i64, i64* %6
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %28
%32 = load i64*, i64** %4
%33 = load i64, i64* %6
%34 = add i64 %33, -1
store i64 %34, i64* %6
%35 = getelementptr inbounds i64, i64* %32, i64 %34
%36 = load i64, i64* %35
store i64 %36, i64* %7
%37 = load i64, i64* %7
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %31
br label %40
40: ; preds = %39, %25
%41 = load i64, i64* %6
%42 = mul i64 %41, 64
%43 = add i64 %42, 64
%44 = sub i64 %43, 1
%45 = load i64, i64* %7
%46 = call i64 @clz64(i64 %45)
%47 = sub i64 %44, %46
store i64 %47, i64* %3
br label %51
48: ; preds = %31
br label %28
49: ; preds = %28
%50 = load i64, i64* %5
store i64 %50, i64* %3
br label %51
51: ; preds = %49, %40
%52 = load i64, i64* %3
ret i64 %52
}

LLVM IR (-O2):
define i64 @find_last_bit(i64* nocapture %0, i64 %1) {
%3 = lshr i64 %1, 6
%4 = and i64 %1, 63
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = getelementptr inbounds i64, i64* %0, i64 %3
%8 = load i64, i64* %7
%9 = sub nuw nsw i64 64, %4
%10 = lshr i64 -1, %9
%11 = and i64 %8, %10
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %6, %2
br label %14
14: ; preds = %13, %17
%15 = phi i64 [ %18, %17 ], [ %3, %13 ]
%16 = icmp eq i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %14
%18 = add nsw i64 %15, -1
%19 = getelementptr inbounds i64, i64* %0, i64 %18
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %14, label %22
22: ; preds = %17, %6
%23 = phi i64 [ %3, %6 ], [ %18, %17 ]
%24 = phi i64 [ %11, %6 ], [ %20, %17 ]
%25 = shl i64 %23, 6
%26 = or i64 %25, 63
%27 = tail call i64 @clz64(i64 %24)
%28 = sub i64 %26, %27
br label %29
29: ; preds = %14, %22
%30 = phi i64 [ %28, %22 ], [ %1, %14 ]
ret i64 %30
}