#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 */
unsigned clz_32(uint32_t v) {
  unsigned r = 0;
  if ((v & 0xFFFF0000) == 0) {
    v <<= 16;
    r += 16;
  }
  if ((v & 0xFF000000) == 0) {
    v <<= 8;
    r += 8;
  }
  if ((v & 0xF0000000) == 0) {
    v <<= 4;
    r += 4;
  }
  if ((v & 0xC0000000) == 0) {
    v <<= 2;
    r += 2;
  }
  r += (uint32_t)~v >> 31;
  return (r);
}


x86 assembly (clang, -O0):
<clz_32>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffff0000,%eax
cmp    $0x0,%eax
jne    1131 <clz_32+0x31>
mov    -0x4(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xff000000,%eax
cmp    $0x0,%eax
jne    1154 <clz_32+0x54>
mov    -0x4(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf0000000,%eax
cmp    $0x0,%eax
jne    1177 <clz_32+0x77>
mov    -0x4(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xc0000000,%eax
cmp    $0x0,%eax
jne    119a <clz_32+0x9a>
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
xor    $0xffffffff,%eax
sar    $0x1f,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<clz_32>:
mov    %edi,%eax
shl    $0x10,%eax
xor    %ecx,%ecx
cmp    $0x10000,%edi
setb   %cl
cmovae %edi,%eax
shl    $0x4,%ecx
mov    %eax,%edx
shl    $0x8,%edx
lea    0x8(%rcx),%esi
cmp    $0x1000000,%eax
cmovae %eax,%edx
cmovae %ecx,%esi
mov    %edx,%ecx
shl    $0x4,%ecx
lea    0x4(%rsi),%edi
cmp    $0x10000000,%edx
cmovae %edx,%ecx
cmovae %esi,%edi
lea    0x0(,%rcx,4),%eax
lea    0x2(%rdi),%edx
cmp    $0x40000000,%ecx
cmovae %ecx,%eax
cmovae %edi,%edx
sar    $0x1f,%eax
not    %eax
add    %edx,%eax
ret

LLVM IR (-O0):
define i32 @clz_32(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = and i32 %4, -65536
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* %2
%9 = shl i32 %8, 16
store i32 %9, i32* %2
%10 = load i32, i32* %3
%11 = add i32 %10, 16
store i32 %11, i32* %3
br label %12
12: ; preds = %7, %1
%13 = load i32, i32* %2
%14 = and i32 %13, -16777216
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* %2
%18 = shl i32 %17, 8
store i32 %18, i32* %2
%19 = load i32, i32* %3
%20 = add i32 %19, 8
store i32 %20, i32* %3
br label %21
21: ; preds = %16, %12
%22 = load i32, i32* %2
%23 = and i32 %22, -268435456
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* %2
%27 = shl i32 %26, 4
store i32 %27, i32* %2
%28 = load i32, i32* %3
%29 = add i32 %28, 4
store i32 %29, i32* %3
br label %30
30: ; preds = %25, %21
%31 = load i32, i32* %2
%32 = and i32 %31, -1073741824
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* %2
%36 = shl i32 %35, 2
store i32 %36, i32* %2
%37 = load i32, i32* %3
%38 = add i32 %37, 2
store i32 %38, i32* %3
br label %39
39: ; preds = %34, %30
%40 = load i32, i32* %2
%41 = xor i32 %40, -1
%42 = ashr i32 %41, 31
%43 = load i32, i32* %3
%44 = add i32 %43, %42
store i32 %44, i32* %3
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
define i32 @clz_32(i32 %0) {
%2 = icmp ult 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 ult i32 %4, 16777216
%7 = shl i32 %4, 8
%8 = or i32 %5, 8
%9 = select i1 %6, i32 %7, i32 %4
%10 = select i1 %6, i32 %8, i32 %5
%11 = icmp ult i32 %9, 268435456
%12 = shl i32 %9, 4
%13 = or i32 %10, 4
%14 = select i1 %11, i32 %12, i32 %9
%15 = select i1 %11, i32 %13, i32 %10
%16 = icmp ult i32 %14, 1073741824
%17 = shl i32 %14, 2
%18 = or i32 %15, 2
%19 = select i1 %16, i32 %17, i32 %14
%20 = select i1 %16, i32 %18, i32 %15
%21 = ashr i32 %19, 31
%22 = xor i32 %21, -1
%23 = add i32 %20, %22
ret i32 %23
}