unsigned int brp_bit_length(unsigned int x) {
  unsigned int i;
  if (x > 16777216)
    i = 31;
  else if (x > 65536)
    i = 23;
  else if (x > 256)
    i = 15;
  else
    i = 8;
  while (1) {
    if ((x & (1 << i)) != 0) {
      return i;
    } else if (i == 0) {
      break;
    }
    i--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<brp_bit_length>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x1000000,-0x8(%rbp)
jbe    1120 <brp_bit_length+0x20>
movl   $0x1f,-0xc(%rbp)
jmp    1163 <brp_bit_length+0x63>
cmpl   $0x10000,-0x8(%rbp)
jbe    1139 <brp_bit_length+0x39>
movl   $0x17,-0xc(%rbp)
jmp    115e <brp_bit_length+0x5e>
cmpl   $0x100,-0x8(%rbp)
jbe    1152 <brp_bit_length+0x52>
movl   $0xf,-0xc(%rbp)
jmp    1159 <brp_bit_length+0x59>
movl   $0x8,-0xc(%rbp)
jmp    115e <brp_bit_length+0x5e>
jmp    1163 <brp_bit_length+0x63>
jmp    1168 <brp_bit_length+0x68>
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     118d <brp_bit_length+0x8d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <brp_bit_length+0xb6>
cmpl   $0x0,-0xc(%rbp)
jne    119c <brp_bit_length+0x9c>
jmp    11af <brp_bit_length+0xaf>
jmp    11a1 <brp_bit_length+0xa1>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1168 <brp_bit_length+0x68>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<brp_bit_length>:
mov    $0x1f,%eax
cmp    $0x1000000,%edi
ja     1130 <brp_bit_length+0x30>
mov    $0x17,%eax
cmp    $0x10000,%edi
ja     1130 <brp_bit_length+0x30>
cmp    $0x101,%edi
mov    $0xf,%ecx
mov    $0x8,%eax
cmovae %ecx,%eax
nopl   (%rax)
mov    %eax,%edx
sub    $0x1,%eax
setb   %cl
bt     %edx,%edi
jb     1141 <brp_bit_length+0x41>
test   %cl,%cl
je     1130 <brp_bit_length+0x30>
add    $0x1,%eax
ret

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

LLVM IR (-O2):
define i32 @brp_bit_length(i32 %0) {
%2 = icmp ugt i32 %0, 16777216
br i1 %2, label %8, label %3
3: ; preds = %1
%4 = icmp ugt i32 %0, 65536
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = icmp ugt i32 %0, 256
%7 = select i1 %6, i32 15, i32 8
br label %8
8: ; preds = %5, %3, %1
%9 = phi i32 [ %7, %5 ], [ 23, %3 ], [ 31, %1 ]
br label %10
10: ; preds = %8, %10
%11 = phi i32 [ %17, %10 ], [ %9, %8 ]
%12 = shl nuw i32 1, %11
%13 = and i32 %12, %0
%14 = icmp ne i32 %13, 0
%15 = icmp eq i32 %11, 0
%16 = select i1 %14, i1 true, i1 %15
%17 = add nsw i32 %11, -1
br i1 %16, label %18, label %10
18: ; preds = %10
ret i32 %11
}