unsigned int quick_log2(unsigned int x) {
  switch (x) {
  case 1:
    return 0;
  case 2:
    return 1;
  case 4:
    return 2;
  case 8:
    return 3;
  case 16:
    return 4;
  case 32:
    return 5;
  case 64:
    return 6;
  case 128:
    return 7;
  case 256:
    return 8;
  case 512:
    return 9;
  case 1024:
    return 10;
  case 2048:
    return 11;
  case 4096:
    return 12;
  case 8192:
    return 13;
  case 16384:
    return 14;
  }
  return -1;
}


x86 assembly (clang, -O0):
<quick_log2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0x1,%eax
je     1219 <quick_log2+0x119>
jmp    111b <quick_log2+0x1b>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
je     1225 <quick_log2+0x125>
jmp    112c <quick_log2+0x2c>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
je     1231 <quick_log2+0x131>
jmp    113d <quick_log2+0x3d>
mov    -0xc(%rbp),%eax
sub    $0x8,%eax
je     123d <quick_log2+0x13d>
jmp    114e <quick_log2+0x4e>
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
je     1249 <quick_log2+0x149>
jmp    115f <quick_log2+0x5f>
mov    -0xc(%rbp),%eax
sub    $0x20,%eax
je     1255 <quick_log2+0x155>
jmp    1170 <quick_log2+0x70>
mov    -0xc(%rbp),%eax
sub    $0x40,%eax
je     1261 <quick_log2+0x161>
jmp    1181 <quick_log2+0x81>
mov    -0xc(%rbp),%eax
sub    $0x80,%eax
je     126d <quick_log2+0x16d>
jmp    1194 <quick_log2+0x94>
mov    -0xc(%rbp),%eax
sub    $0x100,%eax
je     1279 <quick_log2+0x179>
jmp    11a7 <quick_log2+0xa7>
mov    -0xc(%rbp),%eax
sub    $0x200,%eax
je     1285 <quick_log2+0x185>
jmp    11ba <quick_log2+0xba>
mov    -0xc(%rbp),%eax
sub    $0x400,%eax
je     1291 <quick_log2+0x191>
jmp    11cd <quick_log2+0xcd>
mov    -0xc(%rbp),%eax
sub    $0x800,%eax
je     129d <quick_log2+0x19d>
jmp    11e0 <quick_log2+0xe0>
mov    -0xc(%rbp),%eax
sub    $0x1000,%eax
je     12a9 <quick_log2+0x1a9>
jmp    11f3 <quick_log2+0xf3>
mov    -0xc(%rbp),%eax
sub    $0x2000,%eax
je     12b5 <quick_log2+0x1b5>
jmp    1206 <quick_log2+0x106>
mov    -0xc(%rbp),%eax
sub    $0x4000,%eax
je     12c1 <quick_log2+0x1c1>
jmp    12cd <quick_log2+0x1cd>
movl   $0x0,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x1,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x2,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x3,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x4,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x5,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x6,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x7,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x8,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0x9,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xa,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xb,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xc,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xd,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xe,-0x4(%rbp)
jmp    12d4 <quick_log2+0x1d4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<quick_log2>:
cmp    $0x7f,%edi
jle    1133 <quick_log2+0x33>
cmp    $0x7ff,%edi
jg     1157 <quick_log2+0x57>
cmp    $0x1ff,%edi
jg     1175 <quick_log2+0x75>
cmp    $0x80,%edi
je     11a1 <quick_log2+0xa1>
cmp    $0x100,%edi
jne    11d7 <quick_log2+0xd7>
mov    $0x8,%eax
ret
add    $0xffffffff,%edi
cmp    $0x3f,%edi
ja     11d7 <quick_log2+0xd7>
xor    %eax,%eax
lea    0xeb8(%rip),%rcx
movslq (%rcx,%rdi,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
mov    $0x1,%eax
ret
cmp    $0x1fff,%edi
jg     118b <quick_log2+0x8b>
cmp    $0x800,%edi
je     11c5 <quick_log2+0xc5>
cmp    $0x1000,%edi
jne    11d7 <quick_log2+0xd7>
mov    $0xc,%eax
ret
cmp    $0x200,%edi
je     11cb <quick_log2+0xcb>
cmp    $0x400,%edi
jne    11d7 <quick_log2+0xd7>
mov    $0xa,%eax
ret
cmp    $0x2000,%edi
je     11d1 <quick_log2+0xd1>
cmp    $0x4000,%edi
jne    11d7 <quick_log2+0xd7>
mov    $0xe,%eax
ret
mov    $0x7,%eax
ret
mov    $0x2,%eax
ret
mov    $0x3,%eax
ret
mov    $0x4,%eax
ret
mov    $0x5,%eax
ret
mov    $0x6,%eax
ret
mov    $0xb,%eax
ret
mov    $0x9,%eax
ret
mov    $0xd,%eax
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
define i32 @quick_log2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
switch i32 %4, label %20 [
i32 1, label %5
i32 2, label %6
i32 4, label %7
i32 8, label %8
i32 16, label %9
i32 32, label %10
i32 64, label %11
i32 128, label %12
i32 256, label %13
i32 512, label %14
i32 1024, label %15
i32 2048, label %16
i32 4096, label %17
i32 8192, label %18
i32 16384, label %19
]
5: ; preds = %1
store i32 0, i32* %2
br label %21
6: ; preds = %1
store i32 1, i32* %2
br label %21
7: ; preds = %1
store i32 2, i32* %2
br label %21
8: ; preds = %1
store i32 3, i32* %2
br label %21
9: ; preds = %1
store i32 4, i32* %2
br label %21
10: ; preds = %1
store i32 5, i32* %2
br label %21
11: ; preds = %1
store i32 6, i32* %2
br label %21
12: ; preds = %1
store i32 7, i32* %2
br label %21
13: ; preds = %1
store i32 8, i32* %2
br label %21
14: ; preds = %1
store i32 9, i32* %2
br label %21
15: ; preds = %1
store i32 10, i32* %2
br label %21
16: ; preds = %1
store i32 11, i32* %2
br label %21
17: ; preds = %1
store i32 12, i32* %2
br label %21
18: ; preds = %1
store i32 13, i32* %2
br label %21
19: ; preds = %1
store i32 14, i32* %2
br label %21
20: ; preds = %1
store i32 -1, i32* %2
br label %21
21: ; preds = %20, %19, %18, %17, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
define i32 @quick_log2(i32 %0) {
switch i32 %0, label %16 [
i32 1, label %17
i32 2, label %2
i32 4, label %3
i32 8, label %4
i32 16, label %5
i32 32, label %6
i32 64, label %7
i32 128, label %8
i32 256, label %9
i32 512, label %10
i32 1024, label %11
i32 2048, label %12
i32 4096, label %13
i32 8192, label %14
i32 16384, label %15
]
2: ; preds = %1
br label %17
3: ; preds = %1
br label %17
4: ; preds = %1
br label %17
5: ; preds = %1
br label %17
6: ; preds = %1
br label %17
7: ; preds = %1
br label %17
8: ; preds = %1
br label %17
9: ; preds = %1
br label %17
10: ; preds = %1
br label %17
11: ; preds = %1
br label %17
12: ; preds = %1
br label %17
13: ; preds = %1
br label %17
14: ; preds = %1
br label %17
15: ; preds = %1
br label %17
16: ; preds = %1
br label %17
17: ; preds = %1, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5, %4, %3, %2
%18 = phi i32 [ -1, %16 ], [ 14, %15 ], [ 13, %14 ], [ 12, %13 ], [ 11, %12 ], [ 10, %11 ], [ 9, %10 ], [ 8, %9 ], [ 7, %8 ], [ 6, %7 ], [ 5, %6 ], [ 4, %5 ], [ 3, %4 ], [ 2, %3 ], [ 1, %2 ], [ 0, %1 ]
ret i32 %18
}