int which_bit(unsigned int i) {
  char x;
  for (x = 0; x < 32; x++) {
    if ((1U << x) == i) {
      return x + 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<which_bit>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x20,%eax
jge    114d <which_bit+0x4d>
movsbl -0x9(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cmp    -0x8(%rbp),%eax
jne    113b <which_bit+0x3b>
movsbl -0x9(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1154 <which_bit+0x54>
jmp    1140 <which_bit+0x40>
mov    -0x9(%rbp),%al
add    $0x1,%al
mov    %al,-0x9(%rbp)
jmp    110b <which_bit+0xb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<which_bit>:
xor    %eax,%eax
cmp    $0x7fff,%edi
jg     1135 <which_bit+0x35>
cmp    $0x7f,%edi
jg     1177 <which_bit+0x77>
lea    -0x1(%rdi),%ecx
cmp    $0x3f,%ecx
ja     1341 <which_bit+0x241>
lea    0xede(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0x2,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x7fffff,%edi
jg     11b1 <which_bit+0xb1>
cmp    $0x7ffff,%edi
jg     11eb <which_bit+0xeb>
cmp    $0x1ffff,%edi
jg     1275 <which_bit+0x175>
cmp    $0x8000,%edi
je     1373 <which_bit+0x273>
cmp    $0x10000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x11,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x7ff,%edi
jg     1219 <which_bit+0x119>
cmp    $0x1ff,%edi
jg     1297 <which_bit+0x197>
cmp    $0x80,%edi
je     137a <which_bit+0x27a>
cmp    $0x100,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x9,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x7ffffff,%edi
jg     1247 <which_bit+0x147>
cmp    $0x1ffffff,%edi
jg     12b9 <which_bit+0x1b9>
cmp    $0x800000,%edi
je     1381 <which_bit+0x281>
cmp    $0x1000000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x19,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x1fffff,%edi
jg     12db <which_bit+0x1db>
cmp    $0x80000,%edi
je     1388 <which_bit+0x288>
cmp    $0x100000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x15,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x1fff,%edi
jg     12fd <which_bit+0x1fd>
cmp    $0x800,%edi
je     138f <which_bit+0x28f>
cmp    $0x1000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0xd,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x1fffffff,%edi
jg     131f <which_bit+0x21f>
cmp    $0x8000000,%edi
je     1396 <which_bit+0x296>
cmp    $0x10000000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x1d,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x20000,%edi
je     139d <which_bit+0x29d>
cmp    $0x40000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x13,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x200,%edi
je     13a4 <which_bit+0x2a4>
cmp    $0x400,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0xb,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x2000000,%edi
je     13ab <which_bit+0x2ab>
cmp    $0x4000000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x1b,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x200000,%edi
je     13b2 <which_bit+0x2b2>
cmp    $0x400000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x17,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x2000,%edi
je     13b9 <which_bit+0x2b9>
cmp    $0x4000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0xf,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x20000000,%edi
je     13c0 <which_bit+0x2c0>
cmp    $0x40000000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x1f,%edi
jmp    13c5 <which_bit+0x2c5>
cmp    $0x80000000,%edi
jne    13c7 <which_bit+0x2c7>
mov    $0x20,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x3,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x4,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x5,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x6,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x7,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x10,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x8,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x18,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x14,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0xc,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x1c,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x12,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0xa,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x1a,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x16,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0xe,%edi
jmp    13c5 <which_bit+0x2c5>
mov    $0x1e,%edi
mov    %edi,%eax
ret

LLVM IR (-O0):
define i32 @which_bit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
store i32 %0, i32* %3
store i8 0, i8* %4
br label %5
5: ; preds = %20, %1
%6 = load i8, i8* %4
%7 = sext i8 %6 to i32
%8 = icmp slt i32 %7, 32
br i1 %8, label %9, label %23
9: ; preds = %5
%10 = load i8, i8* %4
%11 = sext i8 %10 to i32
%12 = shl i32 1, %11
%13 = load i32, i32* %3
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i8, i8* %4
%17 = sext i8 %16 to i32
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
br label %24
19: ; preds = %9
br label %20
20: ; preds = %19
%21 = load i8, i8* %4
%22 = add i8 %21, 1
store i8 %22, i8* %4
br label %5
23: ; preds = %5
store i32 0, i32* %2
br label %24
24: ; preds = %23, %15
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
define i32 @which_bit(i32 %0) {
switch i32 %0, label %35 [
i32 1, label %33
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
i32 32768, label %16
i32 65536, label %17
i32 131072, label %18
i32 262144, label %19
i32 524288, label %20
i32 1048576, label %21
i32 2097152, label %22
i32 4194304, label %23
i32 8388608, label %24
i32 16777216, label %25
i32 33554432, label %26
i32 67108864, label %27
i32 134217728, label %28
i32 268435456, label %29
i32 536870912, label %30
i32 1073741824, label %31
i32 -2147483648, label %32
]
2: ; preds = %1
br label %33
3: ; preds = %1
br label %33
4: ; preds = %1
br label %33
5: ; preds = %1
br label %33
6: ; preds = %1
br label %33
7: ; preds = %1
br label %33
8: ; preds = %1
br label %33
9: ; preds = %1
br label %33
10: ; preds = %1
br label %33
11: ; preds = %1
br label %33
12: ; preds = %1
br label %33
13: ; preds = %1
br label %33
14: ; preds = %1
br label %33
15: ; preds = %1
br label %33
16: ; preds = %1
br label %33
17: ; preds = %1
br label %33
18: ; preds = %1
br label %33
19: ; preds = %1
br label %33
20: ; preds = %1
br label %33
21: ; preds = %1
br label %33
22: ; preds = %1
br label %33
23: ; preds = %1
br label %33
24: ; preds = %1
br label %33
25: ; preds = %1
br label %33
26: ; preds = %1
br label %33
27: ; preds = %1
br label %33
28: ; preds = %1
br label %33
29: ; preds = %1
br label %33
30: ; preds = %1
br label %33
31: ; preds = %1
br label %33
32: ; preds = %1
br label %33
33: ; preds = %1, %32, %31, %30, %29, %28, %27, %26, %25, %24, %23, %22, %21, %20, %19, %18, %17, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5, %4, %3, %2
%34 = phi i32 [ %0, %1 ], [ 2, %2 ], [ 3, %3 ], [ 4, %4 ], [ 5, %5 ], [ 6, %6 ], [ 7, %7 ], [ 8, %8 ], [ 9, %9 ], [ 10, %10 ], [ 11, %11 ], [ 12, %12 ], [ 13, %13 ], [ 14, %14 ], [ 15, %15 ], [ 16, %16 ], [ 17, %17 ], [ 18, %18 ], [ 19, %19 ], [ 20, %20 ], [ 21, %21 ], [ 22, %22 ], [ 23, %23 ], [ 24, %24 ], [ 25, %25 ], [ 26, %26 ], [ 27, %27 ], [ 28, %28 ], [ 29, %29 ], [ 30, %30 ], [ 31, %31 ], [ 32, %32 ]
br label %35
35: ; preds = %1, %33
%36 = phi i32 [ %34, %33 ], [ 0, %1 ]
ret i32 %36
}