unsigned int log_2(int size) {
  unsigned int i;
  for (i = 0; i < 24; i++) {
    if (1 << i == size)
      return i;
  }
  return 24;
}


x86 assembly (clang, -O0):
<log_2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x18,-0xc(%rbp)
jae    1149 <log_2+0x49>
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cmp    -0x8(%rbp),%eax
jne    1136 <log_2+0x36>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1150 <log_2+0x50>
jmp    113b <log_2+0x3b>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    110e <log_2+0xe>
movl   $0x18,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_2>:
cmp    $0xfff,%edi
jg     1135 <log_2+0x35>
cmp    $0x7f,%edi
jg     11a9 <log_2+0xa9>
add    $0xffffffff,%edi
cmp    $0x3f,%edi
ja     1299 <log_2+0x199>
xor    %eax,%eax
lea    0xeda(%rip),%rcx
movslq (%rcx,%rdi,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
mov    $0x1,%eax
ret
cmp    $0x3ffff,%edi
jg     1173 <log_2+0x73>
cmp    $0x7fff,%edi
jg     11db <log_2+0xdb>
cmp    $0x1000,%edi
je     1257 <log_2+0x157>
cmp    $0x2000,%edi
je     125d <log_2+0x15d>
cmp    $0x4000,%edi
jne    1299 <log_2+0x199>
mov    $0xe,%eax
ret
cmp    $0x1fffff,%edi
jg     1205 <log_2+0x105>
cmp    $0x40000,%edi
je     1263 <log_2+0x163>
cmp    $0x80000,%edi
je     1269 <log_2+0x169>
cmp    $0x100000,%edi
jne    1299 <log_2+0x199>
mov    $0x14,%eax
ret
cmp    $0x1ff,%edi
jle    1223 <log_2+0x123>
cmp    $0x200,%edi
je     126f <log_2+0x16f>
cmp    $0x400,%edi
je     1275 <log_2+0x175>
cmp    $0x800,%edi
jne    1299 <log_2+0x199>
mov    $0xb,%eax
ret
cmp    $0x8000,%edi
je     127b <log_2+0x17b>
cmp    $0x10000,%edi
je     1281 <log_2+0x181>
cmp    $0x20000,%edi
jne    1299 <log_2+0x199>
mov    $0x11,%eax
ret
cmp    $0x200000,%edi
je     1287 <log_2+0x187>
cmp    $0x400000,%edi
je     128d <log_2+0x18d>
cmp    $0x800000,%edi
jne    1299 <log_2+0x199>
mov    $0x17,%eax
ret
cmp    $0x80,%edi
je     1293 <log_2+0x193>
cmp    $0x100,%edi
jne    1299 <log_2+0x199>
mov    $0x8,%eax
ret
mov    $0x2,%eax
ret
mov    $0x3,%eax
ret
mov    $0x4,%eax
ret
mov    $0x5,%eax
ret
mov    $0x6,%eax
ret
mov    $0xc,%eax
ret
mov    $0xd,%eax
ret
mov    $0x12,%eax
ret
mov    $0x13,%eax
ret
mov    $0x9,%eax
ret
mov    $0xa,%eax
ret
mov    $0xf,%eax
ret
mov    $0x10,%eax
ret
mov    $0x15,%eax
ret
mov    $0x16,%eax
ret
mov    $0x7,%eax
ret
mov    $0x18,%eax
ret

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

LLVM IR (-O2):
define i32 @log_2(i32 %0) {
switch i32 %0, label %2 [
i32 1, label %26
i32 2, label %3
i32 4, label %4
i32 8, label %5
i32 16, label %6
i32 32, label %7
i32 64, label %8
i32 128, label %9
i32 256, label %10
i32 512, label %11
i32 1024, label %12
i32 2048, label %13
i32 4096, label %14
i32 8192, label %15
i32 16384, label %16
i32 32768, label %17
i32 65536, label %18
i32 131072, label %19
i32 262144, label %20
i32 524288, label %21
i32 1048576, label %22
i32 2097152, label %23
i32 4194304, label %24
i32 8388608, label %25
]
2: ; preds = %1
br label %26
3: ; preds = %1
br label %26
4: ; preds = %1
br label %26
5: ; preds = %1
br label %26
6: ; preds = %1
br label %26
7: ; preds = %1
br label %26
8: ; preds = %1
br label %26
9: ; preds = %1
br label %26
10: ; preds = %1
br label %26
11: ; preds = %1
br label %26
12: ; preds = %1
br label %26
13: ; preds = %1
br label %26
14: ; preds = %1
br label %26
15: ; preds = %1
br label %26
16: ; preds = %1
br label %26
17: ; preds = %1
br label %26
18: ; preds = %1
br label %26
19: ; preds = %1
br label %26
20: ; preds = %1
br label %26
21: ; preds = %1
br label %26
22: ; preds = %1
br label %26
23: ; preds = %1
br label %26
24: ; preds = %1
br label %26
25: ; preds = %1
br label %26
26: ; preds = %1, %25, %24, %23, %22, %21, %20, %19, %18, %17, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5, %4, %3, %2
%27 = phi i32 [ 0, %1 ], [ 24, %2 ], [ 1, %3 ], [ 2, %4 ], [ 3, %5 ], [ 4, %6 ], [ 5, %7 ], [ 6, %8 ], [ 7, %9 ], [ 8, %10 ], [ 9, %11 ], [ 10, %12 ], [ 11, %13 ], [ 12, %14 ], [ 13, %15 ], [ 14, %16 ], [ 15, %17 ], [ 16, %18 ], [ 17, %19 ], [ 18, %20 ], [ 19, %21 ], [ 20, %22 ], [ 21, %23 ], [ 22, %24 ], [ 23, %25 ]
ret i32 %27
}