#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int u_int32_t;

/* Variables and functions */
int log_base_2(u_int32_t num) {
  int ret = 0;
  if (num <= 0 || (num & (num - 1)) != 0)
    return -1;
  while (num >>= 1)
    ret++;
  return ret;
}


x86 assembly (clang, -O0):
<log_base_2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    112c <log_base_2+0x2c>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1138 <log_base_2+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1163 <log_base_2+0x63>
jmp    113d <log_base_2+0x3d>
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     115d <log_base_2+0x5d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113d <log_base_2+0x3d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_base_2>:
mov    $0xffffffff,%eax
test   %edi,%edi
jle    115c <log_base_2+0x5c>
mov    %edi,%ecx
shr    %ecx
and    $0x55555555,%ecx
mov    %edi,%edx
sub    %ecx,%edx
mov    %edx,%ecx
and    $0x33333333,%ecx
shr    $0x2,%edx
and    $0x33333333,%edx
add    %ecx,%edx
mov    %edx,%ecx
shr    $0x4,%ecx
add    %edx,%ecx
and    $0xf0f0f0f,%ecx
imul   $0x1010101,%ecx,%ecx
shr    $0x18,%ecx
cmp    $0x1,%ecx
ja     115c <log_base_2+0x5c>
xor    %eax,%eax
cmp    $0x2,%edi
jb     115c <log_base_2+0x5c>
xor    %eax,%eax
mov    %edi,%ecx
sar    %ecx
add    $0x1,%eax
cmp    $0x3,%edi
mov    %ecx,%edi
ja     1150 <log_base_2+0x50>
ret

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

LLVM IR (-O2):
define i32 @log_base_2(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = tail call i32 @llvm.ctpop.i32(i32 %0)
%4 = icmp ult i32 %3, 2
%5 = and i1 %2, %4
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = icmp ult i32 %0, 2
br i1 %7, label %14, label %8
8: ; preds = %6, %8
%9 = phi i32 [ %12, %8 ], [ 0, %6 ]
%10 = phi i32 [ %11, %8 ], [ %0, %6 ]
%11 = ashr i32 %10, 1
%12 = add nuw nsw i32 %9, 1
%13 = icmp ult i32 %10, 4
br i1 %13, label %14, label %8
14: ; preds = %8, %6, %1
%15 = phi i32 [ -1, %1 ], [ 0, %6 ], [ %12, %8 ]
ret i32 %15
}