#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 CHAR_BIT;
int bit_num_leading_zeros(unsigned int n) {
  if (n == 0)
    return (CHAR_BIT * sizeof(unsigned int));
  else {
    int cnt = 0;
    if (n <= 0x0000FFFF) {
      cnt += 16;
      n <<= 16;
    }
    if (n <= 0x00FFFFFF) {
      cnt += 8;
      n <<= 8;
    }
    if (n <= 0x0FFFFFFF) {
      cnt += 4;
      n <<= 4;
    }
    if (n <= 0x3FFFFFFF) {
      cnt += 2;
      n <<= 2;
    }
    if (n <= 0x7FFFFFFF) {
      cnt += 1;
    }
    return cnt;
  }
}


x86 assembly (clang, -O0):
<bit_num_leading_zeros>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1127 <bit_num_leading_zeros+0x27>
mov    0x2ec8(%rip),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,-0x4(%rbp)
jmp    11c6 <bit_num_leading_zeros+0xc6>
movl   $0x0,-0xc(%rbp)
cmpl   $0xffff,-0x8(%rbp)
ja     114d <bit_num_leading_zeros+0x4d>
mov    -0xc(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0xffffff,-0x8(%rbp)
ja     116c <bit_num_leading_zeros+0x6c>
mov    -0xc(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0xfffffff,-0x8(%rbp)
ja     118b <bit_num_leading_zeros+0x8b>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x3fffffff,-0x8(%rbp)
ja     11aa <bit_num_leading_zeros+0xaa>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x7fffffff,-0x8(%rbp)
ja     11c0 <bit_num_leading_zeros+0xc0>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_num_leading_zeros>:
test   %edi,%edi
je     115f <bit_num_leading_zeros+0x5f>
mov    %edi,%eax
shl    $0x10,%eax
xor    %ecx,%ecx
cmp    $0x10000,%edi
setb   %cl
cmovae %edi,%eax
shl    $0x4,%ecx
lea    0x8(%rcx),%edx
mov    %eax,%esi
shl    $0x8,%esi
cmp    $0x1000000,%eax
cmovae %eax,%esi
cmovae %ecx,%edx
lea    0x4(%rdx),%ecx
mov    %esi,%edi
shl    $0x4,%edi
cmp    $0x10000000,%esi
cmovae %esi,%edi
cmovae %edx,%ecx
lea    0x2(%rcx),%edx
lea    0x0(,%rdi,4),%eax
cmp    $0x40000000,%edi
cmovae %edi,%eax
cmovae %ecx,%edx
not    %eax
shr    $0x1f,%eax
add    %edx,%eax
ret
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
shl    $0x2,%eax
ret

LLVM IR (-O0):
@CHAR_BIT = global i32 0
define i32 @bit_num_leading_zeros(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @CHAR_BIT
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 4
%11 = trunc i64 %10 to i32
store i32 %11, i32* %2
br label %52
12: ; preds = %1
store i32 0, i32* %4
%13 = load i32, i32* %3
%14 = icmp ule i32 %13, 65535
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 16
store i32 %17, i32* %4
%18 = load i32, i32* %3
%19 = shl i32 %18, 16
store i32 %19, i32* %3
br label %20
20: ; preds = %15, %12
%21 = load i32, i32* %3
%22 = icmp ule i32 %21, 16777215
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 8
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = shl i32 %26, 8
store i32 %27, i32* %3
br label %28
28: ; preds = %23, %20
%29 = load i32, i32* %3
%30 = icmp ule i32 %29, 268435455
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 4
store i32 %33, i32* %4
%34 = load i32, i32* %3
%35 = shl i32 %34, 4
store i32 %35, i32* %3
br label %36
36: ; preds = %31, %28
%37 = load i32, i32* %3
%38 = icmp ule i32 %37, 1073741823
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 2
store i32 %41, i32* %4
%42 = load i32, i32* %3
%43 = shl i32 %42, 2
store i32 %43, i32* %3
br label %44
44: ; preds = %39, %36
%45 = load i32, i32* %3
%46 = icmp ule i32 %45, 2147483647
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
store i32 %49, i32* %4
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %4
store i32 %51, i32* %2
br label %52
52: ; preds = %50, %7
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@CHAR_BIT = global i32 0
define i32 @bit_num_leading_zeros(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @CHAR_BIT
%5 = shl i32 %4, 2
br label %29
6: ; preds = %1
%7 = icmp ult i32 %0, 65536
%8 = shl i32 %0, 16
%9 = select i1 %7, i32 %8, i32 %0
%10 = select i1 %7, i32 16, i32 0
%11 = icmp ult i32 %9, 16777216
%12 = or i32 %10, 8
%13 = shl i32 %9, 8
%14 = select i1 %11, i32 %13, i32 %9
%15 = select i1 %11, i32 %12, i32 %10
%16 = icmp ult i32 %14, 268435456
%17 = or i32 %15, 4
%18 = shl i32 %14, 4
%19 = select i1 %16, i32 %18, i32 %14
%20 = select i1 %16, i32 %17, i32 %15
%21 = icmp ult i32 %19, 1073741824
%22 = or i32 %20, 2
%23 = shl i32 %19, 2
%24 = select i1 %21, i32 %23, i32 %19
%25 = select i1 %21, i32 %22, i32 %20
%26 = xor i32 %24, -1
%27 = lshr i32 %26, 31
%28 = add nsw i32 %27, %25
br label %29
29: ; preds = %6, %3
%30 = phi i32 [ %5, %3 ], [ %28, %6 ]
ret i32 %30
}