int bitmask_to_bitnum(int mask) {
  switch (mask) {
  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;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bitmask_to_bitnum>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0x1,%eax
je     1194 <bitmask_to_bitnum+0x94>
jmp    111b <bitmask_to_bitnum+0x1b>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
je     11a0 <bitmask_to_bitnum+0xa0>
jmp    112c <bitmask_to_bitnum+0x2c>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
je     11ac <bitmask_to_bitnum+0xac>
jmp    113d <bitmask_to_bitnum+0x3d>
mov    -0xc(%rbp),%eax
sub    $0x8,%eax
je     11b8 <bitmask_to_bitnum+0xb8>
jmp    114e <bitmask_to_bitnum+0x4e>
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
je     11c4 <bitmask_to_bitnum+0xc4>
jmp    115f <bitmask_to_bitnum+0x5f>
mov    -0xc(%rbp),%eax
sub    $0x20,%eax
je     11d0 <bitmask_to_bitnum+0xd0>
jmp    1170 <bitmask_to_bitnum+0x70>
mov    -0xc(%rbp),%eax
sub    $0x40,%eax
je     11dc <bitmask_to_bitnum+0xdc>
jmp    1181 <bitmask_to_bitnum+0x81>
mov    -0xc(%rbp),%eax
sub    $0x80,%eax
je     11e8 <bitmask_to_bitnum+0xe8>
jmp    11f4 <bitmask_to_bitnum+0xf4>
movl   $0x0,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x1,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x2,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x3,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x4,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x5,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x6,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x7,-0x4(%rbp)
jmp    11fb <bitmask_to_bitnum+0xfb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmask_to_bitnum>:
xor    %eax,%eax
lea    -0x2(%rdi),%ecx
cmp    $0x1e,%ecx
ja     1120 <bitmask_to_bitnum+0x20>
lea    0xeef(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0x1,%eax
ret
cmp    $0x40,%edi
je     114b <bitmask_to_bitnum+0x4b>
cmp    $0x80,%edi
jne    1150 <bitmask_to_bitnum+0x50>
mov    $0x7,%eax
ret
mov    $0x2,%eax
ret
mov    $0x3,%eax
ret
mov    $0x4,%eax
ret
mov    $0x5,%eax
ret
mov    $0x6,%eax
ret

LLVM IR (-O0):
define i32 @bitmask_to_bitnum(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
switch i32 %4, label %13 [
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
]
5: ; preds = %1
store i32 0, i32* %2
br label %14
6: ; preds = %1
store i32 1, i32* %2
br label %14
7: ; preds = %1
store i32 2, i32* %2
br label %14
8: ; preds = %1
store i32 3, i32* %2
br label %14
9: ; preds = %1
store i32 4, i32* %2
br label %14
10: ; preds = %1
store i32 5, i32* %2
br label %14
11: ; preds = %1
store i32 6, i32* %2
br label %14
12: ; preds = %1
store i32 7, i32* %2
br label %14
13: ; preds = %1
store i32 0, i32* %2
br label %14
14: ; preds = %13, %12, %11, %10, %9, %8, %7, %6, %5
%15 = load i32, i32* %2
ret i32 %15
}

LLVM IR (-O2):
define i32 @bitmask_to_bitnum(i32 %0) {
switch i32 %0, label %9 [
i32 128, label %8
i32 2, label %2
i32 4, label %3
i32 8, label %4
i32 16, label %5
i32 32, label %6
i32 64, label %7
]
2: ; preds = %1
br label %9
3: ; preds = %1
br label %9
4: ; preds = %1
br label %9
5: ; preds = %1
br label %9
6: ; preds = %1
br label %9
7: ; preds = %1
br label %9
8: ; preds = %1
br label %9
9: ; preds = %1, %8, %7, %6, %5, %4, %3, %2
%10 = phi i32 [ 7, %8 ], [ 6, %7 ], [ 5, %6 ], [ 4, %5 ], [ 3, %4 ], [ 2, %3 ], [ 1, %2 ], [ 0, %1 ]
ret i32 %10
}