int ilog2(int i) {
  int log2;
  int exp2 = 1;
  if (i <= 0)
    return (-1);
  for (log2 = 0; log2 < 20; log2++) {
    if (exp2 == i)
      return (log2);
    exp2 *= 2;
  }
  return (-1);
}


x86 assembly (clang, -O0):
<ilog2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1124 <ilog2+0x24>
movl   $0xffffffff,-0x4(%rbp)
jmp    116a <ilog2+0x6a>
movl   $0x0,-0xc(%rbp)
cmpl   $0x14,-0xc(%rbp)
jge    1163 <ilog2+0x63>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    114c <ilog2+0x4c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    116a <ilog2+0x6a>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112b <ilog2+0x2b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ilog2>:
mov    $0xffffffff,%eax
test   %edi,%edi
jle    11ad <ilog2+0xad>
cmp    $0x3ff,%edi
jg     1142 <ilog2+0x42>
cmp    $0x7f,%edi
jg     11ae <ilog2+0xae>
add    $0xffffffff,%edi
cmp    $0x3f,%edi
ja     124a <ilog2+0x14a>
xor    %eax,%eax
lea    0xecd(%rip),%rcx
movslq (%rcx,%rdi,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
mov    $0x1,%eax
ret
cmp    $0x7fff,%edi
jg     117c <ilog2+0x7c>
cmp    $0xfff,%edi
jle    11d0 <ilog2+0xd0>
cmp    $0x1000,%edi
je     121a <ilog2+0x11a>
cmp    $0x2000,%edi
je     1220 <ilog2+0x120>
cmp    $0x4000,%edi
jne    124a <ilog2+0x14a>
mov    $0xe,%eax
ret
cmp    $0x1ffff,%edi
jle    11e6 <ilog2+0xe6>
cmp    $0x20000,%edi
je     1226 <ilog2+0x126>
cmp    $0x40000,%edi
je     122c <ilog2+0x12c>
cmp    $0x80000,%edi
jne    124a <ilog2+0x14a>
mov    $0x13,%eax
ret
cmp    $0x80,%edi
je     1232 <ilog2+0x132>
cmp    $0x100,%edi
je     1238 <ilog2+0x138>
cmp    $0x200,%edi
jne    124a <ilog2+0x14a>
mov    $0x9,%eax
ret
cmp    $0x400,%edi
je     123e <ilog2+0x13e>
cmp    $0x800,%edi
jne    124a <ilog2+0x14a>
mov    $0xb,%eax
ret
cmp    $0x8000,%edi
je     1244 <ilog2+0x144>
cmp    $0x10000,%edi
jne    124a <ilog2+0x14a>
mov    $0x10,%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    $0x11,%eax
ret
mov    $0x12,%eax
ret
mov    $0x7,%eax
ret
mov    $0x8,%eax
ret
mov    $0xa,%eax
ret
mov    $0xf,%eax
ret
mov    $0xffffffff,%eax
ret

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

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