int __popcounthi2(unsigned short x) {
  int i;
  int count = 0;
  for (i = 0; i < 16; i++)
    if (x & ((unsigned short)1 << i))
      count++;
  return count;
}


x86 assembly (clang, -O0):
<__popcounthi2>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x10,-0x8(%rbp)
jge    115a <__popcounthi2+0x5a>
movzwl -0x2(%rbp),%eax
mov    -0x8(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1147 <__popcounthi2+0x47>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    114c <__popcounthi2+0x4c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1119 <__popcounthi2+0x19>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<__popcounthi2>:
mov    %edi,%eax
and    $0x1,%eax
mov    %edi,%ecx
shr    %ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0x2,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0x3,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0x4,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0x5,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0x6,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0x7,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0x8,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0x9,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0xa,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0xb,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0xc,%eax
and    $0x1,%eax
add    %ecx,%eax
mov    %edi,%ecx
shr    $0xd,%ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %edi,%eax
shr    $0xe,%eax
and    $0x1,%eax
add    %ecx,%eax
shr    $0xf,%edi
add    %edi,%eax
ret

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

LLVM IR (-O2):
define i32 @__popcounthi2(i16 zeroext %0) {
%2 = zext i16 %0 to i32
%3 = and i32 %2, 1
%4 = lshr i32 %2, 1
%5 = and i32 %4, 1
%6 = add nuw nsw i32 %5, %3
%7 = lshr i32 %2, 2
%8 = and i32 %7, 1
%9 = add nuw nsw i32 %8, %6
%10 = lshr i32 %2, 3
%11 = and i32 %10, 1
%12 = add nuw nsw i32 %11, %9
%13 = lshr i32 %2, 4
%14 = and i32 %13, 1
%15 = add nuw nsw i32 %14, %12
%16 = lshr i32 %2, 5
%17 = and i32 %16, 1
%18 = add nuw nsw i32 %17, %15
%19 = lshr i32 %2, 6
%20 = and i32 %19, 1
%21 = add nuw nsw i32 %20, %18
%22 = lshr i32 %2, 7
%23 = and i32 %22, 1
%24 = add nuw nsw i32 %23, %21
%25 = lshr i32 %2, 8
%26 = and i32 %25, 1
%27 = add nuw nsw i32 %26, %24
%28 = lshr i32 %2, 9
%29 = and i32 %28, 1
%30 = add nuw nsw i32 %29, %27
%31 = lshr i32 %2, 10
%32 = and i32 %31, 1
%33 = add nuw nsw i32 %32, %30
%34 = lshr i32 %2, 11
%35 = and i32 %34, 1
%36 = add nuw nsw i32 %35, %33
%37 = lshr i32 %2, 12
%38 = and i32 %37, 1
%39 = add nuw nsw i32 %38, %36
%40 = lshr i32 %2, 13
%41 = and i32 %40, 1
%42 = add nuw nsw i32 %41, %39
%43 = lshr i32 %2, 14
%44 = and i32 %43, 1
%45 = add nuw nsw i32 %44, %42
%46 = lshr i32 %2, 15
%47 = add nuw nsw i32 %46, %45
ret i32 %47
}