#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 /*<<< orphan*/ x;

/* Variables and functions */
int /*<<< orphan*/ assert(unsigned long);
int bit_clz(unsigned long x) {
  int nr = 0;
  int sh;
  assert(x);
  if (sizeof(x) == 8) {
    sh = !(x & (~(~0x0ul >> (sizeof(x) * 8 / 2)))) << 5;
    nr += sh;
    x <<= sh;
  }
  sh = !(x & (~(~0x0ul >> (1 << 4)))) << 4;
  nr += sh;
  x <<= sh;
  sh = !(x & (~(~0x0ul >> (1 << 3)))) << 3;
  nr += sh;
  x <<= sh;
  sh = !(x & (~(~0x0ul >> (1 << 2)))) << 2;
  nr += sh;
  x <<= sh;
  sh = !(x & (~(~0x0ul >> (1 << 1)))) << 1;
  nr += sh;
  x <<= sh;
  sh = !(x & (~(~0x0ul >> (1 << 0)))) << 0;
  nr += sh;
  return nr;
}


x86 assembly (clang, -O0):
<bit_clz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <assert@plt>
movabs $0xffffffff00000000,%rax
ff ff ff
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x5,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
movabs $0xffff000000000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x4,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
movabs $0xff00000000000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x3,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
movabs $0xf000000000000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
movabs $0xc000000000000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    %cl,%rax
mov    %rax,-0x8(%rbp)
movabs $0x8000000000000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
shl    $0x0,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_clz>:
push   %rbx
mov    %rdi,%rbx
call   1030 <assert@plt>
xor    %eax,%eax
mov    %rbx,%rcx
shr    $0x20,%rcx
sete   %al
shl    $0x5,%eax
mov    %eax,%ecx
shl    %cl,%rbx
xor    %ecx,%ecx
mov    %rbx,%rdx
shr    $0x30,%rdx
sete   %cl
shl    $0x4,%ecx
shl    %cl,%rbx
or     %ecx,%eax
xor    %edx,%edx
mov    %rbx,%rcx
shr    $0x38,%rcx
sete   %dl
mov    %edx,%ecx
shl    $0x3,%ecx
shl    %cl,%rbx
lea    (%rax,%rdx,8),%eax
xor    %edx,%edx
mov    %rbx,%rcx
shr    $0x3c,%rcx
sete   %dl
mov    %edx,%ecx
shl    $0x2,%ecx
shl    %cl,%rbx
lea    (%rax,%rdx,4),%eax
xor    %edx,%edx
mov    %rbx,%rcx
shr    $0x3e,%rcx
sete   %dl
mov    %edx,%ecx
add    %edx,%ecx
shl    %cl,%rbx
lea    (%rax,%rdx,2),%eax
shr    $0x3f,%rbx
or     %eax,%ebx
xor    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @bit_clz(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %3
%5 = load i64, i64* %2
%6 = call i32 @assert(i64 %5)
%7 = load i64, i64* %2
%8 = and i64 %7, -4294967296
%9 = icmp ne i64 %8, 0
%10 = xor i1 %9, true
%11 = zext i1 %10 to i32
%12 = shl i32 %11, 5
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* %3
%15 = add nsw i32 %14, %13
store i32 %15, i32* %3
%16 = load i32, i32* %4
%17 = load i64, i64* %2
%18 = zext i32 %16 to i64
%19 = shl i64 %17, %18
store i64 %19, i64* %2
%20 = load i64, i64* %2
%21 = and i64 %20, -281474976710656
%22 = icmp ne i64 %21, 0
%23 = xor i1 %22, true
%24 = zext i1 %23 to i32
%25 = shl i32 %24, 4
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = load i32, i32* %3
%28 = add nsw i32 %27, %26
store i32 %28, i32* %3
%29 = load i32, i32* %4
%30 = load i64, i64* %2
%31 = zext i32 %29 to i64
%32 = shl i64 %30, %31
store i64 %32, i64* %2
%33 = load i64, i64* %2
%34 = and i64 %33, -72057594037927936
%35 = icmp ne i64 %34, 0
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
%38 = shl i32 %37, 3
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = load i32, i32* %3
%41 = add nsw i32 %40, %39
store i32 %41, i32* %3
%42 = load i32, i32* %4
%43 = load i64, i64* %2
%44 = zext i32 %42 to i64
%45 = shl i64 %43, %44
store i64 %45, i64* %2
%46 = load i64, i64* %2
%47 = and i64 %46, -1152921504606846976
%48 = icmp ne i64 %47, 0
%49 = xor i1 %48, true
%50 = zext i1 %49 to i32
%51 = shl i32 %50, 2
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = load i32, i32* %3
%54 = add nsw i32 %53, %52
store i32 %54, i32* %3
%55 = load i32, i32* %4
%56 = load i64, i64* %2
%57 = zext i32 %55 to i64
%58 = shl i64 %56, %57
store i64 %58, i64* %2
%59 = load i64, i64* %2
%60 = and i64 %59, -4611686018427387904
%61 = icmp ne i64 %60, 0
%62 = xor i1 %61, true
%63 = zext i1 %62 to i32
%64 = shl i32 %63, 1
store i32 %64, i32* %4
%65 = load i32, i32* %4
%66 = load i32, i32* %3
%67 = add nsw i32 %66, %65
store i32 %67, i32* %3
%68 = load i32, i32* %4
%69 = load i64, i64* %2
%70 = zext i32 %68 to i64
%71 = shl i64 %69, %70
store i64 %71, i64* %2
%72 = load i64, i64* %2
%73 = and i64 %72, -9223372036854775808
%74 = icmp ne i64 %73, 0
%75 = xor i1 %74, true
%76 = zext i1 %75 to i32
%77 = shl i32 %76, 0
store i32 %77, i32* %4
%78 = load i32, i32* %4
%79 = load i32, i32* %3
%80 = add nsw i32 %79, %78
store i32 %80, i32* %3
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
define i32 @bit_clz(i64 %0) {
%2 = tail call i32 @assert(i64 %0)
%3 = icmp ult i64 %0, 4294967296
%4 = select i1 %3, i32 32, i32 0
%5 = zext i32 %4 to i64
%6 = shl i64 %0, %5
%7 = icmp ult i64 %6, 281474976710656
%8 = select i1 %7, i32 16, i32 0
%9 = or i32 %8, %4
%10 = zext i32 %8 to i64
%11 = shl i64 %6, %10
%12 = icmp ult i64 %11, 72057594037927936
%13 = select i1 %12, i32 8, i32 0
%14 = or i32 %9, %13
%15 = zext i32 %13 to i64
%16 = shl i64 %11, %15
%17 = icmp ult i64 %16, 1152921504606846976
%18 = select i1 %17, i32 4, i32 0
%19 = or i32 %14, %18
%20 = zext i32 %18 to i64
%21 = shl i64 %16, %20
%22 = icmp ult i64 %21, 4611686018427387904
%23 = select i1 %22, i32 2, i32 0
%24 = or i32 %19, %23
%25 = zext i32 %23 to i64
%26 = shl i64 %21, %25
%27 = lshr i64 %26, 63
%28 = trunc i64 %27 to i32
%29 = or i32 %24, %28
%30 = xor i32 %29, 1
ret i32 %30
}