#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 firstbit(uint64_t b) {
  int n;
  static const char bits[] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
  n = 0;
  if (!(b & 0xffffffff)) {
    n += 32;
    b >>= 32;
  }
  if (!(b & 0xffff)) {
    n += 16;
    b >>= 16;
  }
  if (!(b & 0xff)) {
    n += 8;
    b >>= 8;
  }
  if (!(b & 0xf)) {
    n += 4;
    b >>= 4;
  }
  n += bits[b & 0xf];
  return n;
}


x86 assembly (clang, -O0):
<firstbit>:
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 <firstbit+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 <firstbit+0x56>
mov    -0x8(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
jne    1179 <firstbit+0x79>
mov    -0x8(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    119a <firstbit+0x9a>
mov    -0x8(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe56(%rip),%rax
movsbl (%rax,%rcx,1),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstbit>:
xor    %eax,%eax
test   %edi,%edi
sete   %al
shl    $0x5,%eax
lea    0x10(%rax),%ecx
mov    %edi,%edx
sar    $0x10,%edx
test   %di,%di
cmovne %edi,%edx
cmovne %eax,%ecx
lea    0x8(%rcx),%eax
mov    %edx,%esi
sar    $0x8,%esi
test   %dl,%dl
cmovne %edx,%esi
cmovne %ecx,%eax
lea    0x4(%rax),%ecx
mov    %esi,%edx
shr    $0x4,%edx
test   $0xf,%sil
cmovne %esi,%edx
cmovne %eax,%ecx
and    $0xf,%edx
lea    0xeb9(%rip),%rax
movsbl (%rdx,%rax,1),%eax
add    %ecx,%eax
ret

LLVM IR (-O0):
@firstbit.bits = internal constant [16 x i8] c"\04\00\01\00\02\00\01\00\03\00\01\00\02\00\01\00"
define i32 @firstbit(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 ne i32 %5, 0
br i1 %6, label %12, label %7
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* %3
%18 = add nsw i32 %17, 16
store i32 %18, i32* %3
%19 = load i32, i32* %2
%20 = ashr i32 %19, 16
store i32 %20, i32* %2
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* %3
%27 = add nsw i32 %26, 8
store i32 %27, i32* %3
%28 = load i32, i32* %2
%29 = ashr i32 %28, 8
store i32 %29, i32* %2
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* %3
%36 = add nsw i32 %35, 4
store i32 %36, i32* %3
%37 = load i32, i32* %2
%38 = ashr i32 %37, 4
store i32 %38, i32* %2
br label %39
39: ; preds = %34, %30
%40 = load i32, i32* %2
%41 = and i32 %40, 15
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [16 x i8], [16 x i8]* @firstbit.bits, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = load i32, i32* %3
%47 = add nsw i32 %46, %45
store i32 %47, i32* %3
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@firstbit.bits = internal unnamed_addr constant [16 x i8] c"\04\00\01\00\02\00\01\00\03\00\01\00\02\00\01\00"
define i32 @firstbit(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 = or i32 %3, 16
%7 = ashr i32 %0, 16
%8 = select i1 %5, i32 %7, i32 %0
%9 = select i1 %5, i32 %6, i32 %3
%10 = and i32 %8, 255
%11 = icmp eq i32 %10, 0
%12 = or i32 %9, 8
%13 = ashr i32 %8, 8
%14 = select i1 %11, i32 %13, i32 %8
%15 = select i1 %11, i32 %12, i32 %9
%16 = and i32 %14, 15
%17 = icmp eq i32 %16, 0
%18 = or i32 %15, 4
%19 = lshr i32 %14, 4
%20 = select i1 %17, i32 %19, i32 %14
%21 = select i1 %17, i32 %18, i32 %15
%22 = and i32 %20, 15
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* @firstbit.bits, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = add nsw i32 %21, %26
ret i32 %27
}