#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 uint32_t;

/* Variables and functions */
int nlz(uint32_t x) {
  int n = 0;
  if (x <= 0x0000FFFF) {
    n += 16;
    x <<= 16;
  }
  if (x <= 0x00FFFFFF) {
    n += 8;
    x <<= 8;
  }
  if (x <= 0x0FFFFFFF) {
    n += 4;
    x <<= 4;
  }
  if (x <= 0x3FFFFFFF) {
    n += 2;
    x <<= 2;
  }
  if (x <= 0x7FFFFFFF)
    n++;
  return n;
}


x86 assembly (clang, -O0):
<nlz>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xffff,-0x4(%rbp)
jg     112d <nlz+0x2d>
mov    -0x8(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0xffffff,-0x4(%rbp)
jg     114c <nlz+0x4c>
mov    -0x8(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0xfffffff,-0x4(%rbp)
jg     116b <nlz+0x6b>
mov    -0x8(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x3fffffff,-0x4(%rbp)
jg     118a <nlz+0x8a>
mov    -0x8(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x7fffffff,-0x4(%rbp)
jg     11a0 <nlz+0xa0>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlz>:
mov    %edi,%eax
shl    $0x10,%eax
xor    %ecx,%ecx
cmp    $0x10000,%edi
setl   %cl
cmovge %edi,%eax
shl    $0x4,%ecx
lea    0x8(%rcx),%edx
mov    %eax,%esi
shl    $0x8,%esi
cmp    $0x1000000,%eax
cmovge %eax,%esi
cmovge %ecx,%edx
mov    %esi,%ecx
shl    $0x4,%ecx
cmp    $0x10000000,%esi
cmovge %esi,%ecx
lea    0x4(%rdx),%esi
cmovge %edx,%esi
lea    0x2(%rsi),%eax
cmp    $0x40000000,%ecx
cmovge %esi,%eax
add    $0x1,%eax
ret

LLVM IR (-O0):
define i32 @nlz(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = icmp sle i32 %4, 65535
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 16
store i32 %8, i32* %3
%9 = load i32, i32* %2
%10 = shl i32 %9, 16
store i32 %10, i32* %2
br label %11
11: ; preds = %6, %1
%12 = load i32, i32* %2
%13 = icmp sle i32 %12, 16777215
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 8
store i32 %16, i32* %3
%17 = load i32, i32* %2
%18 = shl i32 %17, 8
store i32 %18, i32* %2
br label %19
19: ; preds = %14, %11
%20 = load i32, i32* %2
%21 = icmp sle i32 %20, 268435455
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 4
store i32 %24, i32* %3
%25 = load i32, i32* %2
%26 = shl i32 %25, 4
store i32 %26, i32* %2
br label %27
27: ; preds = %22, %19
%28 = load i32, i32* %2
%29 = icmp sle i32 %28, 1073741823
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 2
store i32 %32, i32* %3
%33 = load i32, i32* %2
%34 = shl i32 %33, 2
store i32 %34, i32* %2
br label %35
35: ; preds = %30, %27
%36 = load i32, i32* %2
%37 = icmp sle i32 %36, 2147483647
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %41
41: ; preds = %38, %35
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
define i32 @nlz(i32 %0) {
%2 = icmp slt i32 %0, 65536
%3 = shl i32 %0, 16
%4 = select i1 %2, i32 %3, i32 %0
%5 = select i1 %2, i32 16, i32 0
%6 = icmp slt i32 %4, 16777216
%7 = or i32 %5, 8
%8 = shl i32 %4, 8
%9 = select i1 %6, i32 %8, i32 %4
%10 = select i1 %6, i32 %7, i32 %5
%11 = icmp slt i32 %9, 268435456
%12 = or i32 %10, 4
%13 = shl i32 %9, 4
%14 = select i1 %11, i32 %13, i32 %9
%15 = select i1 %11, i32 %12, i32 %10
%16 = icmp slt i32 %14, 1073741824
%17 = or i32 %15, 2
%18 = select i1 %16, i32 %17, i32 %15
%19 = add nsw i32 %18, 1
ret i32 %19
}