#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 ffs64(uint64_t word) {
  int r = 0;
  if ((word & 0xffffffff) == 0) {
    r += 32;
    word >>= 32;
  }
  if (!(word & 0xffff)) {
    word >>= 16;
    r += 16;
  }
  if (!(word & 0xff)) {
    word >>= 8;
    r += 8;
  }
  if (!(word & 0xf)) {
    word >>= 4;
    r += 4;
  }
  if (!(word & 3)) {
    word >>= 2;
    r += 2;
  }
  if (!(word & 1)) {
    word >>= 1;
    r += 1;
  }
  return r;
}


x86 assembly (clang, -O0):
<ffs64>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffffffff,%eax
cmp    $0x0,%eax
jne    1133 <ffs64+0x33>
mov    -0x8(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffff,%eax
cmp    $0x0,%eax
jne    1156 <ffs64+0x56>
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
jne    1179 <ffs64+0x79>
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    119a <ffs64+0x9a>
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
jne    11bb <ffs64+0xbb>
mov    -0x4(%rbp),%eax
sar    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11dc <ffs64+0xdc>
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffs64>:
xor    %eax,%eax
test   %edi,%edi
sete   %al
shl    $0x5,%eax
mov    %edi,%ecx
sar    $0x10,%ecx
lea    0x10(%rax),%edx
test   %di,%di
cmovne %edi,%ecx
cmovne %eax,%edx
mov    %ecx,%eax
sar    $0x8,%eax
test   %cl,%cl
cmovne %ecx,%eax
lea    0x8(%rdx),%ecx
cmovne %edx,%ecx
mov    %eax,%edx
sar    $0x4,%edx
lea    0x4(%rcx),%esi
test   $0xf,%al
cmovne %eax,%edx
cmovne %ecx,%esi
mov    %edx,%eax
shr    $0x2,%eax
lea    0x2(%rsi),%ecx
test   $0x3,%dl
cmovne %edx,%eax
not    %eax
cmovne %esi,%ecx
and    $0x1,%eax
add    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @ffs64(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = and i32 %4, -1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 32
store i32 %9, i32* %3
%10 = load i32, i32* %2
%11 = ashr i32 %10, 32
store i32 %11, i32* %2
br label %12
12: ; preds = %7, %1
%13 = load i32, i32* %2
%14 = and i32 %13, 65535
%15 = icmp ne i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* %2
%18 = ashr i32 %17, 16
store i32 %18, i32* %2
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 16
store i32 %20, i32* %3
br label %21
21: ; preds = %16, %12
%22 = load i32, i32* %2
%23 = and i32 %22, 255
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = load i32, i32* %2
%27 = ashr i32 %26, 8
store i32 %27, i32* %2
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 8
store i32 %29, i32* %3
br label %30
30: ; preds = %25, %21
%31 = load i32, i32* %2
%32 = and i32 %31, 15
%33 = icmp ne i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load i32, i32* %2
%36 = ashr i32 %35, 4
store i32 %36, i32* %2
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 4
store i32 %38, i32* %3
br label %39
39: ; preds = %34, %30
%40 = load i32, i32* %2
%41 = and i32 %40, 3
%42 = icmp ne i32 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %39
%44 = load i32, i32* %2
%45 = ashr i32 %44, 2
store i32 %45, i32* %2
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 2
store i32 %47, i32* %3
br label %48
48: ; preds = %43, %39
%49 = load i32, i32* %2
%50 = and i32 %49, 1
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %48
%53 = load i32, i32* %2
%54 = ashr i32 %53, 1
store i32 %54, i32* %2
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %57
57: ; preds = %52, %48
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
define i32 @ffs64(i32 %0) {
%2 = icmp eq i32 %0, 0
%3 = select i1 %2, i32 32, i32 0
%4 = and i32 %0, 65535
%5 = icmp eq i32 %4, 0
%6 = ashr i32 %0, 16
%7 = or i32 %3, 16
%8 = select i1 %5, i32 %6, i32 %0
%9 = select i1 %5, i32 %7, i32 %3
%10 = and i32 %8, 255
%11 = icmp eq i32 %10, 0
%12 = ashr i32 %8, 8
%13 = or i32 %9, 8
%14 = select i1 %11, i32 %12, i32 %8
%15 = select i1 %11, i32 %13, i32 %9
%16 = and i32 %14, 15
%17 = icmp eq i32 %16, 0
%18 = ashr i32 %14, 4
%19 = or i32 %15, 4
%20 = select i1 %17, i32 %18, i32 %14
%21 = select i1 %17, i32 %19, i32 %15
%22 = and i32 %20, 3
%23 = icmp eq i32 %22, 0
%24 = lshr i32 %20, 2
%25 = add nsw i32 %21, 2
%26 = select i1 %23, i32 %24, i32 %20
%27 = select i1 %23, i32 %25, i32 %21
%28 = and i32 %26, 1
%29 = xor i32 %28, 1
%30 = add nsw i32 %29, %27
ret i32 %30
}