int clz(unsigned int x) {
  static const unsigned char tab[32] = {
      0, 31, 9, 30, 3, 8,  18, 29, 2,  5,  7,  14, 12, 17, 22, 28,
      1, 10, 4, 19, 6, 15, 13, 23, 11, 20, 16, 24, 21, 25, 26, 27};
  if (x == 0)
    return 32;
  x |= x >> 1;
  x |= x >> 2;
  x |= x >> 4;
  x |= x >> 8;
  x |= x >> 16;
  return tab[(0x07dcd629 * (x + 1)) >> 27];
}


x86 assembly (clang, -O0):
<clz>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    111d <clz+0x1d>
movl   $0x20,-0x4(%rbp)
jmp    117a <clz+0x7a>
mov    -0x8(%rbp),%eax
shr    $0x1,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x2,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x4,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x8,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x10,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
imul   $0x7dcd629,%eax,%eax
shr    $0x1b,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe8d(%rip),%rax
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<clz>:
test   %edi,%edi
je     1141 <clz+0x41>
mov    %edi,%eax
shr    %eax
or     %edi,%eax
mov    %eax,%ecx
shr    $0x2,%ecx
or     %eax,%ecx
mov    %ecx,%eax
shr    $0x4,%eax
or     %ecx,%eax
mov    %eax,%ecx
shr    $0x8,%ecx
or     %eax,%ecx
mov    %ecx,%eax
shr    $0x10,%eax
or     %ecx,%eax
imul   $0x7dcd629,%eax,%eax
add    $0x7dcd629,%eax
shr    $0x1b,%rax
lea    0xec4(%rip),%rcx
movzbl (%rax,%rcx,1),%eax
ret
mov    $0x20,%eax
ret

LLVM IR (-O0):
@clz.tab = internal constant [32 x i8] c"\00\1F\09\1E\03\08\12\1D\02\05\07\0E\0C\11\16\1C\01\0A\04\13\06\0F\0D\17\0B\14\10\18\15\19\1A\1B"
define i32 @clz(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 32, i32* %2
br label %36
7: ; preds = %1
%8 = load i32, i32* %3
%9 = lshr i32 %8, 1
%10 = load i32, i32* %3
%11 = or i32 %10, %9
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = lshr i32 %12, 2
%14 = load i32, i32* %3
%15 = or i32 %14, %13
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = lshr i32 %16, 4
%18 = load i32, i32* %3
%19 = or i32 %18, %17
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = lshr i32 %20, 8
%22 = load i32, i32* %3
%23 = or i32 %22, %21
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = lshr i32 %24, 16
%26 = load i32, i32* %3
%27 = or i32 %26, %25
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = add i32 %28, 1
%30 = mul i32 131913257, %29
%31 = lshr i32 %30, 27
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds [32 x i8], [32 x i8]* @clz.tab, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
store i32 %35, i32* %2
br label %36
36: ; preds = %7, %6
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@clz.tab = internal unnamed_addr constant [32 x i8] c"\00\1F\09\1E\03\08\12\1D\02\05\07\0E\0C\11\16\1C\01\0A\04\13\06\0F\0D\17\0B\14\10\18\15\19\1A\1B"
define i32 @clz(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %21, label %3
3: ; preds = %1
%4 = lshr i32 %0, 1
%5 = or i32 %4, %0
%6 = lshr i32 %5, 2
%7 = or i32 %6, %5
%8 = lshr i32 %7, 4
%9 = or i32 %8, %7
%10 = lshr i32 %9, 8
%11 = or i32 %10, %9
%12 = lshr i32 %11, 16
%13 = or i32 %12, %11
%14 = mul i32 %13, 131913257
%15 = add i32 %14, 131913257
%16 = lshr i32 %15, 27
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [32 x i8], [32 x i8]* @clz.tab, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
br label %21
21: ; preds = %1, %3
%22 = phi i32 [ %20, %3 ], [ 32, %1 ]
ret i32 %22
}