int count_bits(unsigned int v) {
  unsigned int c;
  static const unsigned int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F,
                                   0x00FF00FF, 0x0000FFFF};
  c = v - ((v >> 1) & B[0]);
  c = ((c >> 2) & B[1]) + (c & B[1]);
  c = ((c >> 4) + c) & B[2];
  c = ((c >> 8) + c) & B[3];
  c = ((c >> 16) + c) & B[4];
  return (int)c;
}


x86 assembly (clang, -O0):
<count_bits>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
shr    $0x1,%ecx
and    0xeea(%rip),%ecx
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x2,%eax
and    0xedd(%rip),%eax
mov    -0x8(%rbp),%ecx
and    0xed4(%rip),%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x4,%eax
add    -0x8(%rbp),%eax
and    0xec4(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x8,%eax
add    -0x8(%rbp),%eax
and    0xeb6(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x10,%eax
add    -0x8(%rbp),%eax
and    0xea8(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_bits>:
mov    %edi,%eax
shr    %eax
and    $0x55555555,%eax
sub    %eax,%edi
mov    %edi,%eax
shr    $0x2,%eax
and    $0x33333333,%eax
and    $0x33333333,%edi
add    %eax,%edi
mov    %edi,%eax
shr    $0x4,%eax
add    %edi,%eax
and    $0xf0f0f0f,%eax
mov    %eax,%ecx
shr    $0x8,%ecx
add    %eax,%ecx
mov    %ecx,%eax
shr    $0x10,%eax
add    %ecx,%eax
movzbl %al,%eax
ret

LLVM IR (-O0):
@count_bits.B = internal constant [5 x i32] [i32 1431655765, i32 858993459, i32 252645135, i32 16711935, i32 65535]
define i32 @count_bits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* %2
%6 = lshr i32 %5, 1
%7 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 0)
%8 = and i32 %6, %7
%9 = sub i32 %4, %8
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = lshr i32 %10, 2
%12 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 1)
%13 = and i32 %11, %12
%14 = load i32, i32* %3
%15 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 1)
%16 = and i32 %14, %15
%17 = add i32 %13, %16
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = lshr i32 %18, 4
%20 = load i32, i32* %3
%21 = add i32 %19, %20
%22 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 2)
%23 = and i32 %21, %22
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = lshr i32 %24, 8
%26 = load i32, i32* %3
%27 = add i32 %25, %26
%28 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 3)
%29 = and i32 %27, %28
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = lshr i32 %30, 16
%32 = load i32, i32* %3
%33 = add i32 %31, %32
%34 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @count_bits.B, i64 0, i64 4)
%35 = and i32 %33, %34
store i32 %35, i32* %3
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @count_bits(i32 %0) {
%2 = lshr i32 %0, 1
%3 = and i32 %2, 1431655765
%4 = sub i32 %0, %3
%5 = lshr i32 %4, 2
%6 = and i32 %5, 858993459
%7 = and i32 %4, 858993459
%8 = add nuw nsw i32 %6, %7
%9 = lshr i32 %8, 4
%10 = add nuw nsw i32 %9, %8
%11 = and i32 %10, 252645135
%12 = lshr i32 %11, 8
%13 = add nuw nsw i32 %12, %11
%14 = lshr i32 %13, 16
%15 = and i32 %14, 31
%16 = add nuw nsw i32 %15, %13
%17 = and i32 %16, 63
ret i32 %17
}