#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*/ assert(int);
int floor_log2(unsigned int n) {
  assert(n != 0);
  int pos = 0;
  if (n >= 1 << 16) {
    n >>= 16;
    pos += 16;
  }
  if (n >= 1 << 8) {
    n >>= 8;
    pos += 8;
  }
  if (n >= 1 << 4) {
    n >>= 4;
    pos += 4;
  }
  if (n >= 1 << 2) {
    n >>= 2;
    pos += 2;
  }
  if (n >= 1 << 1) {
    pos += 1;
  }
  return pos;
}


x86 assembly (clang, -O0):
<floor_log2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x10000,-0x4(%rbp)
jb     1152 <floor_log2+0x42>
mov    -0x4(%rbp),%eax
shr    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x100,-0x4(%rbp)
jb     1171 <floor_log2+0x61>
mov    -0x4(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x10,-0x4(%rbp)
jb     118d <floor_log2+0x7d>
mov    -0x4(%rbp),%eax
shr    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x4,-0x4(%rbp)
jb     11a9 <floor_log2+0x99>
mov    -0x4(%rbp),%eax
shr    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x4(%rbp)
jb     11bc <floor_log2+0xac>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floor_log2>:
push   %rbx
mov    %edi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setne  %dil
call   1030 <assert@plt>
mov    %ebx,%eax
shr    $0x10,%eax
xor    %ecx,%ecx
cmp    $0x10000,%ebx
setae  %cl
cmovb  %ebx,%eax
shl    $0x4,%ecx
mov    %eax,%edx
shr    $0x8,%edx
lea    0x8(%rcx),%esi
cmp    $0x100,%eax
cmovb  %eax,%edx
cmovb  %ecx,%esi
mov    %edx,%ecx
shr    $0x4,%ecx
lea    0x4(%rsi),%edi
cmp    $0x10,%edx
cmovb  %edx,%ecx
cmovb  %esi,%edi
mov    %ecx,%edx
shr    $0x2,%edx
lea    0x2(%rdi),%eax
cmp    $0x4,%ecx
cmovb  %ecx,%edx
cmovb  %edi,%eax
cmp    $0x2,%edx
sbb    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @floor_log2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp ne i32 %4, 0
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
store i32 0, i32* %3
%8 = load i32, i32* %2
%9 = icmp uge i32 %8, 65536
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* %2
%12 = lshr i32 %11, 16
store i32 %12, i32* %2
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 16
store i32 %14, i32* %3
br label %15
15: ; preds = %10, %1
%16 = load i32, i32* %2
%17 = icmp uge i32 %16, 256
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* %2
%20 = lshr i32 %19, 8
store i32 %20, i32* %2
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 8
store i32 %22, i32* %3
br label %23
23: ; preds = %18, %15
%24 = load i32, i32* %2
%25 = icmp uge i32 %24, 16
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* %2
%28 = lshr i32 %27, 4
store i32 %28, i32* %2
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 4
store i32 %30, i32* %3
br label %31
31: ; preds = %26, %23
%32 = load i32, i32* %2
%33 = icmp uge i32 %32, 4
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* %2
%36 = lshr i32 %35, 2
store i32 %36, i32* %2
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 2
store i32 %38, i32* %3
br label %39
39: ; preds = %34, %31
%40 = load i32, i32* %2
%41 = icmp uge i32 %40, 2
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @floor_log2(i32 %0) {
%2 = icmp ne i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = icmp ugt i32 %0, 65535
%6 = lshr i32 %0, 16
%7 = select i1 %5, i32 %6, i32 %0
%8 = select i1 %5, i32 16, i32 0
%9 = icmp ugt i32 %7, 255
%10 = lshr i32 %7, 8
%11 = or i32 %8, 8
%12 = select i1 %9, i32 %10, i32 %7
%13 = select i1 %9, i32 %11, i32 %8
%14 = icmp ugt i32 %12, 15
%15 = lshr i32 %12, 4
%16 = or i32 %13, 4
%17 = select i1 %14, i32 %15, i32 %12
%18 = select i1 %14, i32 %16, i32 %13
%19 = icmp ugt i32 %17, 3
%20 = lshr i32 %17, 2
%21 = or i32 %18, 2
%22 = select i1 %19, i32 %20, i32 %17
%23 = select i1 %19, i32 %21, i32 %18
%24 = icmp ugt i32 %22, 1
%25 = zext i1 %24 to i32
%26 = add nsw i32 %23, %25
ret i32 %26
}