#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);
unsigned kvz_math_floor_log2(unsigned value) {
  assert(value > 0);
  unsigned result = 0;
  for (int i = 4; i >= 0; --i) {
    unsigned bits = 1ull << i;
    unsigned shift = value >= (1 << bits) ? bits : 0;
    result += shift;
    value >>= shift;
  }
  return result;
}


x86 assembly (clang, -O0):
<kvz_math_floor_log2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x4,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a8 <kvz_math_floor_log2+0x98>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jb     1176 <kvz_math_floor_log2+0x66>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <kvz_math_floor_log2+0x70>
xor    %eax,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <kvz_math_floor_log2+0x70>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
shr    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <kvz_math_floor_log2+0x2a>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvz_math_floor_log2>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %r14d,%r14d
cmp    $0x10000,%edi
setae  %r14b
xor    %edi,%edi
test   %ebx,%ebx
setne  %dil
call   1030 <assert@plt>
shl    $0x4,%r14d
mov    %r14d,%ecx
shr    %cl,%ebx
xor    %eax,%eax
cmp    $0x100,%ebx
setae  %al
lea    (%r14,%rax,8),%edx
mov    %eax,%ecx
shl    $0x3,%ecx
shr    %cl,%ebx
xor    %eax,%eax
cmp    $0x10,%ebx
setae  %al
lea    (%rdx,%rax,4),%edx
mov    %eax,%ecx
shl    $0x2,%ecx
shr    %cl,%ebx
xor    %eax,%eax
cmp    $0x4,%ebx
setae  %al
lea    (%rdx,%rax,2),%edx
mov    %eax,%ecx
add    %eax,%ecx
shr    %cl,%ebx
xor    %eax,%eax
cmp    $0x2,%ebx
setae  %al
or     %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @kvz_math_floor_log2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = icmp ugt i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
store i32 0, i32* %3
store i32 4, i32* %4
br label %11
11: ; preds = %34, %1
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = load i32, i32* %4
%16 = zext i32 %15 to i64
%17 = shl i64 1, %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %5
%19 = load i32, i32* %2
%20 = load i32, i32* %5
%21 = shl i32 1, %20
%22 = icmp uge i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = load i32, i32* %5
br label %26
25: ; preds = %14
br label %26
26: ; preds = %25, %23
%27 = phi i32 [ %24, %23 ], [ 0, %25 ]
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = load i32, i32* %3
%30 = add i32 %29, %28
store i32 %30, i32* %3
%31 = load i32, i32* %6
%32 = load i32, i32* %2
%33 = lshr i32 %32, %31
store i32 %33, i32* %2
br label %34
34: ; preds = %26
%35 = load i32, i32* %4
%36 = add nsw i32 %35, -1
store i32 %36, i32* %4
br label %11
37: ; preds = %11
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
define i32 @kvz_math_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 ult i32 %0, 65536
%6 = select i1 %5, i32 0, i32 16
%7 = lshr i32 %0, %6
%8 = icmp ult i32 %7, 256
%9 = select i1 %8, i32 0, i32 8
%10 = or i32 %9, %6
%11 = lshr i32 %7, %9
%12 = icmp ult i32 %11, 16
%13 = select i1 %12, i32 0, i32 4
%14 = or i32 %13, %10
%15 = lshr i32 %11, %13
%16 = icmp ult i32 %15, 4
%17 = select i1 %16, i32 0, i32 2
%18 = or i32 %17, %14
%19 = lshr i32 %15, %17
%20 = icmp ugt i32 %19, 1
%21 = zext i1 %20 to i32
%22 = or i32 %18, %21
ret i32 %22
}