#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 uint_fast8_t;
typedef int uint32_t;

/* Variables and functions */
uint_fast8_t fls_uint32_t(uint32_t x) {
  uint_fast8_t r = 32;
  if (!x)
    return 0;
  if (!(x & 0xffff0000u)) {
    x <<= 16;
    r -= 16;
  }
  if (!(x & 0xff000000u)) {
    x <<= 8;
    r -= 8;
  }
  if (!(x & 0xf0000000u)) {
    x <<= 4;
    r -= 4;
  }
  if (!(x & 0xc0000000u)) {
    x <<= 2;
    r -= 2;
  }
  if (!(x & 0x80000000u)) {
    x <<= 1;
    r -= 1;
  }
  return r;
}


x86 assembly (clang, -O0):
<fls_uint32_t>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x20,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1124 <fls_uint32_t+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <fls_uint32_t+0xd9>
mov    -0x8(%rbp),%eax
and    $0xffff0000,%eax
cmp    $0x0,%eax
jne    1147 <fls_uint32_t+0x47>
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff000000,%eax
cmp    $0x0,%eax
jne    116a <fls_uint32_t+0x6a>
mov    -0x8(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0xf0000000,%eax
cmp    $0x0,%eax
jne    118d <fls_uint32_t+0x8d>
mov    -0x8(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0xc0000000,%eax
cmp    $0x0,%eax
jne    11b0 <fls_uint32_t+0xb0>
mov    -0x8(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
jne    11d3 <fls_uint32_t+0xd3>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $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):
<fls_uint32_t>:
test   %edi,%edi
je     1162 <fls_uint32_t+0x62>
mov    %edi,%eax
shl    $0x10,%eax
xor    %ecx,%ecx
cmp    $0x10000,%edi
setae  %cl
cmovae %edi,%eax
shl    $0x4,%ecx
lea    0x10(%rcx),%edx
mov    %eax,%esi
shl    $0x8,%esi
or     $0x8,%ecx
cmp    $0x1000000,%eax
cmovae %eax,%esi
cmovae %edx,%ecx
mov    %esi,%edx
shl    $0x4,%edx
lea    -0x4(%rcx),%edi
cmp    $0x10000000,%esi
cmovae %esi,%edx
cmovae %ecx,%edi
lea    0x0(,%rdx,4),%eax
lea    -0x2(%rdi),%ecx
cmp    $0x40000000,%edx
cmovae %edx,%eax
cmovae %edi,%ecx
sar    $0x1f,%eax
not    %eax
add    %ecx,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @fls_uint32_t(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 32, i32* %4
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 0, i32* %2
br label %55
8: ; preds = %1
%9 = load i32, i32* %3
%10 = and i32 %9, -65536
%11 = icmp ne i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = load i32, i32* %3
%14 = shl i32 %13, 16
store i32 %14, i32* %3
%15 = load i32, i32* %4
%16 = sub nsw i32 %15, 16
store i32 %16, i32* %4
br label %17
17: ; preds = %12, %8
%18 = load i32, i32* %3
%19 = and i32 %18, -16777216
%20 = icmp ne i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = load i32, i32* %3
%23 = shl i32 %22, 8
store i32 %23, i32* %3
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 8
store i32 %25, i32* %4
br label %26
26: ; preds = %21, %17
%27 = load i32, i32* %3
%28 = and i32 %27, -268435456
%29 = icmp ne i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = load i32, i32* %3
%32 = shl i32 %31, 4
store i32 %32, i32* %3
%33 = load i32, i32* %4
%34 = sub nsw i32 %33, 4
store i32 %34, i32* %4
br label %35
35: ; preds = %30, %26
%36 = load i32, i32* %3
%37 = and i32 %36, -1073741824
%38 = icmp ne i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* %3
%41 = shl i32 %40, 2
store i32 %41, i32* %3
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 2
store i32 %43, i32* %4
br label %44
44: ; preds = %39, %35
%45 = load i32, i32* %3
%46 = and i32 %45, -2147483648
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %44
%49 = load i32, i32* %3
%50 = shl i32 %49, 1
store i32 %50, i32* %3
%51 = load i32, i32* %4
%52 = sub nsw i32 %51, 1
store i32 %52, i32* %4
br label %53
53: ; preds = %48, %44
%54 = load i32, i32* %4
store i32 %54, i32* %2
br label %55
55: ; preds = %53, %7
%56 = load i32, i32* %2
ret i32 %56
}

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