int bit_population_count(unsigned int n) {
  if (n == 0)
    return 0;
  n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
  n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
  n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0F0F0F);
  n = (n & 0x00FF00FF) + ((n >> 8) & 0x00FF00FF);
  n = (n & 0x0000FFFF) + ((n >> 16) & 0x0000FFFF);
  return n;
}


x86 assembly (clang, -O0):
<bit_population_count>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    111d <bit_population_count+0x1d>
movl   $0x0,-0x4(%rbp)
jmp    11a0 <bit_population_count+0xa0>
mov    -0x8(%rbp),%eax
and    $0x55555555,%eax
mov    -0x8(%rbp),%ecx
shr    $0x1,%ecx
and    $0x55555555,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x33333333,%eax
mov    -0x8(%rbp),%ecx
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xf0f0f0f,%eax
mov    -0x8(%rbp),%ecx
shr    $0x4,%ecx
and    $0xf0f0f0f,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff00ff,%eax
mov    -0x8(%rbp),%ecx
shr    $0x8,%ecx
and    $0xff00ff,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xffff,%eax
mov    -0x8(%rbp),%ecx
shr    $0x10,%ecx
and    $0xffff,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_population_count>:
test   %edi,%edi
je     1156 <bit_population_count+0x56>
mov    %edi,%eax
mov    %edi,%ecx
and    $0x55555555,%ecx
shr    %eax
and    $0x55555555,%eax
add    %ecx,%eax
mov    %eax,%ecx
and    $0x33333333,%ecx
shr    $0x2,%eax
and    $0x33333333,%eax
add    %ecx,%eax
mov    %eax,%ecx
and    $0x7070707,%ecx
shr    $0x4,%eax
and    $0x7070707,%eax
add    %ecx,%eax
mov    %eax,%ecx
and    $0xf000f,%ecx
shr    $0x8,%eax
and    $0xf000f,%eax
add    %ecx,%eax
movzwl %ax,%ecx
shr    $0x10,%eax
add    %ecx,%eax
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i32 @bit_population_count(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %23, label %3
3: ; preds = %1
%4 = and i32 %0, 1431655765
%5 = lshr i32 %0, 1
%6 = and i32 %5, 1431655765
%7 = add nuw i32 %6, %4
%8 = and i32 %7, 858993459
%9 = lshr i32 %7, 2
%10 = and i32 %9, 858993459
%11 = add nuw nsw i32 %10, %8
%12 = and i32 %11, 117901063
%13 = lshr i32 %11, 4
%14 = and i32 %13, 117901063
%15 = add nuw nsw i32 %14, %12
%16 = and i32 %15, 983055
%17 = lshr i32 %15, 8
%18 = and i32 %17, 983055
%19 = add nuw nsw i32 %18, %16
%20 = and i32 %19, 31
%21 = lshr i32 %19, 16
%22 = add nuw nsw i32 %20, %21
br label %23
23: ; preds = %1, %3
%24 = phi i32 [ %22, %3 ], [ 0, %1 ]
ret i32 %24
}