int bitcount(unsigned int n) {
  n = ((n) & (((unsigned int)(-1)) / ((0x1u << ((0x1u << (1)))) + 1u))) +
      (((n) >> ((0x1u << (1)))) &
       (((unsigned int)(-1)) / ((0x1u << ((0x1u << (1)))) + 1u)));
  n = ((n) & (((unsigned int)(-1)) / ((0x1u << ((0x1u << (1)))) + 1u))) +
      (((n) >> ((0x1u << (1)))) &
       (((unsigned int)(-1)) / ((0x1u << ((0x1u << (1)))) + 1u)));
  n = ((n) & (((unsigned int)(-1)) / ((0x1u << ((0x1u << (2)))) + 1u))) +
      (((n) >> ((0x1u << (2)))) &
       (((unsigned int)(-1)) / ((0x1u << ((0x1u << (2)))) + 1u)));
  n = ((n) & (((unsigned int)(-1)) / ((0x1u << ((0x1u << (3)))) + 1u))) +
      (((n) >> ((0x1u << (3)))) &
       (((unsigned int)(-1)) / ((0x1u << ((0x1u << (3)))) + 1u)));
  n = ((n) & (((unsigned int)(-1)) / ((0x1u << ((0x1u << (4)))) + 1u))) +
      (((n) >> ((0x1u << (4)))) &
       (((unsigned int)(-1)) / ((0x1u << ((0x1u << (4)))) + 1u)));
  return n;
}


x86 assembly (clang, -O0):
<bitcount>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x33333333,%eax
mov    -0x4(%rbp),%ecx
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x33333333,%eax
mov    -0x4(%rbp),%ecx
shr    $0x2,%ecx
and    $0x33333333,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf0f0f0f,%eax
mov    -0x4(%rbp),%ecx
shr    $0x4,%ecx
and    $0xf0f0f0f,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xff00ff,%eax
mov    -0x4(%rbp),%ecx
shr    $0x8,%ecx
and    $0xff00ff,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffff,%eax
mov    -0x4(%rbp),%ecx
shr    $0x10,%ecx
and    $0xffff,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitcount>:
mov    %edi,%eax
and    $0x33333333,%eax
shr    $0x2,%edi
and    $0x33333333,%edi
add    %edi,%eax
mov    %eax,%ecx
and    $0x33333333,%ecx
shr    $0x2,%eax
and    $0x11111111,%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

LLVM IR (-O0):
define i32 @bitcount(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = and i32 %3, 858993459
%5 = load i32, i32* %2
%6 = lshr i32 %5, 2
%7 = and i32 %6, 858993459
%8 = add i32 %4, %7
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = and i32 %9, 858993459
%11 = load i32, i32* %2
%12 = lshr i32 %11, 2
%13 = and i32 %12, 858993459
%14 = add i32 %10, %13
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = and i32 %15, 252645135
%17 = load i32, i32* %2
%18 = lshr i32 %17, 4
%19 = and i32 %18, 252645135
%20 = add i32 %16, %19
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = and i32 %21, 16711935
%23 = load i32, i32* %2
%24 = lshr i32 %23, 8
%25 = and i32 %24, 16711935
%26 = add i32 %22, %25
store i32 %26, i32* %2
%27 = load i32, i32* %2
%28 = and i32 %27, 65535
%29 = load i32, i32* %2
%30 = lshr i32 %29, 16
%31 = and i32 %30, 65535
%32 = add i32 %28, %31
store i32 %32, i32* %2
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
define i32 @bitcount(i32 %0) {
%2 = and i32 %0, 858993459
%3 = lshr i32 %0, 2
%4 = and i32 %3, 858993459
%5 = add nuw nsw i32 %4, %2
%6 = and i32 %5, 858993459
%7 = lshr i32 %5, 2
%8 = and i32 %7, 286331153
%9 = add nuw nsw i32 %8, %6
%10 = and i32 %9, 117901063
%11 = lshr i32 %9, 4
%12 = and i32 %11, 117901063
%13 = add nuw nsw i32 %12, %10
%14 = and i32 %13, 983055
%15 = lshr i32 %13, 8
%16 = and i32 %15, 983055
%17 = add nuw nsw i32 %16, %14
%18 = and i32 %17, 31
%19 = lshr i32 %17, 16
%20 = add nuw nsw i32 %18, %19
ret i32 %20
}