unsigned char bitnum(unsigned short value) {
  unsigned char ret = 0;
  int i;
  for (i = 0; i < 15; i++) {
    if (value & 0x01)
      ret = i + 1;
    value = (value >> 1);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<bitnum>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movb   $0x0,-0x3(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xf,-0x8(%rbp)
jge    1152 <bitnum+0x52>
movzwl -0x2(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1139 <bitnum+0x39>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %al,-0x3(%rbp)
movzwl -0x2(%rbp),%eax
sar    $0x1,%eax
mov    %ax,-0x2(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1116 <bitnum+0x16>
movzbl -0x3(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitnum>:
mov    %edi,%eax
and    $0x1,%al
test   $0x2,%dil
movzbl %al,%eax
mov    $0x2,%ecx
cmove  %eax,%ecx
test   $0x4,%dil
mov    $0x3,%eax
cmove  %ecx,%eax
test   $0x8,%dil
mov    $0x4,%ecx
cmove  %eax,%ecx
test   $0x10,%dil
mov    $0x5,%eax
cmove  %ecx,%eax
test   $0x20,%dil
mov    $0x6,%ecx
cmove  %eax,%ecx
test   $0x40,%dil
mov    $0x7,%eax
cmove  %ecx,%eax
movd   %edi,%xmm0
pshuflw $0x0,%xmm0,%xmm0
pshufd $0x0,%xmm0,%xmm0
pand   0xe9b(%rip),%xmm0
pxor   %xmm1,%xmm1
pcmpeqw %xmm0,%xmm1
packsswb %xmm1,%xmm1
pmovmskb %xmm1,%ecx
test   $0x80,%cl
mov    $0x8,%edx
cmovne %eax,%edx
test   $0x40,%cl
mov    $0x9,%eax
cmovne %edx,%eax
test   $0x20,%cl
mov    $0xa,%edx
cmovne %eax,%edx
test   $0x10,%cl
mov    $0xb,%eax
cmovne %edx,%eax
test   $0x8,%cl
mov    $0xc,%edx
cmovne %eax,%edx
test   $0x4,%cl
mov    $0xd,%eax
cmovne %edx,%eax
test   $0x2,%cl
mov    $0xe,%edx
cmovne %eax,%edx
test   $0x1,%cl
mov    $0xf,%eax
cmovne %edx,%eax
ret

LLVM IR (-O0):
define zeroext i8 @bitnum(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i8
%4 = alloca i32
store i16 %0, i16* %2
store i8 0, i8* %3
store i32 0, i32* %4
br label %5
5: ; preds = %22, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 15
br i1 %7, label %8, label %25
8: ; preds = %5
%9 = load i16, i16* %2
%10 = zext i16 %9 to i32
%11 = and i32 %10, 1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
%16 = trunc i32 %15 to i8
store i8 %16, i8* %3
br label %17
17: ; preds = %13, %8
%18 = load i16, i16* %2
%19 = zext i16 %18 to i32
%20 = ashr i32 %19, 1
%21 = trunc i32 %20 to i16
store i16 %21, i16* %2
br label %22
22: ; preds = %17
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %5
25: ; preds = %5
%26 = load i8, i8* %3
ret i8 %26
}

LLVM IR (-O2):
define zeroext i8 @bitnum(i16 zeroext %0) {
%2 = trunc i16 %0 to i8
%3 = and i8 %2, 1
%4 = and i16 %0, 2
%5 = icmp eq i16 %4, 0
%6 = select i1 %5, i8 %3, i8 2
%7 = and i16 %0, 4
%8 = icmp eq i16 %7, 0
%9 = select i1 %8, i8 %6, i8 3
%10 = and i16 %0, 8
%11 = icmp eq i16 %10, 0
%12 = select i1 %11, i8 %9, i8 4
%13 = and i16 %0, 16
%14 = icmp eq i16 %13, 0
%15 = select i1 %14, i8 %12, i8 5
%16 = and i16 %0, 32
%17 = icmp eq i16 %16, 0
%18 = select i1 %17, i8 %15, i8 6
%19 = and i16 %0, 64
%20 = icmp eq i16 %19, 0
%21 = select i1 %20, i8 %18, i8 7
%22 = insertelement <8 x i16> poison, i16 %0, i64 0
%23 = shufflevector <8 x i16> %22, <8 x i16> poison, <8 x i32> zeroinitializer
%24 = and <8 x i16> %23, <i16 16384, i16 8192, i16 4096, i16 2048, i16 1024, i16 512, i16 256, i16 128>
%25 = icmp eq <8 x i16> %24, zeroinitializer
%26 = extractelement <8 x i1> %25, i64 7
%27 = select i1 %26, i8 %21, i8 8
%28 = extractelement <8 x i1> %25, i64 6
%29 = select i1 %28, i8 %27, i8 9
%30 = extractelement <8 x i1> %25, i64 5
%31 = select i1 %30, i8 %29, i8 10
%32 = extractelement <8 x i1> %25, i64 4
%33 = select i1 %32, i8 %31, i8 11
%34 = extractelement <8 x i1> %25, i64 3
%35 = select i1 %34, i8 %33, i8 12
%36 = extractelement <8 x i1> %25, i64 2
%37 = select i1 %36, i8 %35, i8 13
%38 = extractelement <8 x i1> %25, i64 1
%39 = select i1 %38, i8 %37, i8 14
%40 = extractelement <8 x i1> %25, i64 0
%41 = select i1 %40, i8 %39, i8 15
ret i8 %41
}