#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 */

/* Variables and functions */
int /*<<< orphan*/ is_power_of_two(unsigned int const);
int log_two(const unsigned int n) {
  if (!is_power_of_two(n))
    return -1;
  int power = 0;
  int i = 0;
  unsigned int number = n;
  for (i = 0; i < sizeof(int) << 3; i++) {
    if ((number & 1) == 1) {
      power = i;
      return power;
    }
    number = number >> 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<log_two>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <is_power_of_two@plt>
cmp    $0x0,%eax
jne    1138 <log_two+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <log_two+0x8f>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x20,%rax
jae    1198 <log_two+0x88>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    1181 <log_two+0x71>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <log_two+0x8f>
mov    -0x14(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1153 <log_two+0x43>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_two>:
push   %rbx
mov    %edi,%ebx
call   1030 <is_power_of_two@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
je     114f <log_two+0x3f>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
test   $0x1,%bl
jne    115e <log_two+0x4e>
test   $0x2,%bl
jne    1151 <log_two+0x41>
test   $0x4,%bl
jne    1156 <log_two+0x46>
test   $0x8,%bl
jne    115b <log_two+0x4b>
shr    $0x4,%ebx
add    $0x4,%ecx
cmp    $0x20,%ecx
jne    1130 <log_two+0x20>
pop    %rbx
ret
add    $0x1,%ecx
jmp    115e <log_two+0x4e>
add    $0x2,%ecx
jmp    115e <log_two+0x4e>
add    $0x3,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @log_two(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @is_power_of_two(i32 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 -1, i32* %2
br label %31
11: ; preds = %1
store i32 0, i32* %4
store i32 0, i32* %5
%12 = load i32, i32* %3
store i32 %12, i32* %6
store i32 0, i32* %5
br label %13
13: ; preds = %27, %11
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, 32
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = load i32, i32* %6
%19 = and i32 %18, 1
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %5
store i32 %22, i32* %4
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %31
24: ; preds = %17
%25 = load i32, i32* %6
%26 = lshr i32 %25, 1
store i32 %26, i32* %6
br label %27
27: ; preds = %24
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %13
30: ; preds = %13
store i32 -1, i32* %2
br label %31
31: ; preds = %30, %21, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
define i32 @log_two(i32 %0) {
%2 = tail call i32 @is_power_of_two(i32 %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %28, label %4
4: ; preds = %1, %18
%5 = phi i32 [ %19, %18 ], [ %0, %1 ]
%6 = phi i32 [ %20, %18 ], [ 0, %1 ]
%7 = and i32 %5, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %4
%10 = and i32 %5, 2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = and i32 %5, 4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = and i32 %5, 8
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = lshr i32 %5, 4
%20 = add nuw nsw i32 %6, 4
%21 = icmp eq i32 %20, 32
br i1 %21, label %28, label %4
22: ; preds = %15
%23 = or i32 %6, 3
br label %28
24: ; preds = %12
%25 = or i32 %6, 2
br label %28
26: ; preds = %9
%27 = or i32 %6, 1
br label %28
28: ; preds = %22, %24, %26, %18, %4, %1
%29 = phi i32 [ -1, %1 ], [ %23, %22 ], [ %25, %24 ], [ %27, %26 ], [ -1, %18 ], [ %6, %4 ]
ret i32 %29
}