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


x86 assembly (clang, -O0):
<gf_tw_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 <gf_tw_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 <gf_tw_find_last_bit+0x6e>
jmp    11bf <gf_tw_find_last_bit+0xaf>
jmp    1183 <gf_tw_find_last_bit+0x73>
jmp    1188 <gf_tw_find_last_bit+0x78>
cmpq   $0x0,-0x20(%rbp)
je     11ec <gf_tw_find_last_bit+0xdc>
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     11e7 <gf_tw_find_last_bit+0xd7>
jmp    11bf <gf_tw_find_last_bit+0xaf>
mov    -0x20(%rbp),%rax
shl    $0x6,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fls@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11f4 <gf_tw_find_last_bit+0xe4>
jmp    1188 <gf_tw_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):
<gf_tw_find_last_bit>:
push   %rbx
mov    %rsi,%rax
mov    %rsi,%rbx
shr    $0x6,%rbx
mov    %rsi,%rcx
and    $0x3f,%rcx
je     1146 <gf_tw_find_last_bit+0x36>
mov    (%rdi,%rbx,8),%rdx
neg    %cl
shl    %cl,%rdx
shr    %cl,%rdx
test   %rdx,%rdx
je     1146 <gf_tw_find_last_bit+0x36>
shl    $0x6,%rbx
mov    %rdx,%rdi
call   1030 <fls@plt>
add    %rbx,%rax
jmp    1157 <gf_tw_find_last_bit+0x47>
sub    $0x1,%rbx
jb     1157 <gf_tw_find_last_bit+0x47>
mov    (%rdi,%rbx,8),%rdx
test   %rdx,%rdx
je     1146 <gf_tw_find_last_bit+0x36>
jmp    1135 <gf_tw_find_last_bit+0x25>
pop    %rbx
ret

LLVM IR (-O0):
define i64 @gf_tw_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 = %46, %27
%29 = load i64, i64* %6
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %47
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 %46
39: ; preds = %31
br label %40
40: ; preds = %39, %25
%41 = load i64, i64* %6
%42 = mul i64 %41, 64
%43 = load i64, i64* %7
%44 = call i64 @fls(i64 %43)
%45 = add i64 %42, %44
store i64 %45, i64* %3
br label %49
46: ; preds = %31
br label %28
47: ; preds = %28
%48 = load i64, i64* %5
store i64 %48, i64* %3
br label %49
49: ; preds = %47, %40
%50 = load i64, i64* %3
ret i64 %50
}

LLVM IR (-O2):
define i64 @gf_tw_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 %28, 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 = tail call i64 @fls(i64 %24)
%27 = add i64 %26, %25
br label %28
28: ; preds = %14, %22
%29 = phi i64 [ %27, %22 ], [ %1, %14 ]
ret i64 %29
}