#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint64_t;

/* Variables and functions */
unsigned long hweight32(unsigned int);
unsigned long hweight64(uint64_t w) {
  if (((int)sizeof(unsigned long) * 8) == 32)
    return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
  w -= (w >> 1) & 0x5555555555555555ull;
  w = (w & 0x3333333333333333ull) + ((w >> 2) & 0x3333333333333333ull);
  w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0full;
  return (w * 0x0101010101010101ull) >> 56;
}


x86 assembly (clang, -O0):
<hweight64>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
movslq %eax,%rcx
movabs $0x5555555555555555,%rax
55 55 55
and    %rax,%rcx
movslq -0x4(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0x3333333333333333,%rcx
33 33 33
and    %rcx,%rax
mov    -0x4(%rbp),%ecx
sar    $0x2,%ecx
movslq %ecx,%rcx
movabs $0x3333333333333333,%rdx
33 33 33
and    %rdx,%rcx
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
sar    $0x4,%ecx
add    %ecx,%eax
cltq
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    %rcx,%rax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0x101010101010101,%rcx
01 01 01
imul   %rcx,%rax
shr    $0x38,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hweight64>:
mov    %edi,%eax
shr    %eax
and    $0x55555555,%eax
sub    %eax,%edi
mov    %edi,%eax
and    $0x33333333,%eax
shr    $0x2,%edi
and    $0x33333333,%edi
add    %edi,%eax
mov    %eax,%ecx
shr    $0x4,%ecx
add    %eax,%ecx
and    $0xf0f0f0f,%ecx
movabs $0x101010101010101,%rax
01 01 01
imul   %rcx,%rax
shr    $0x38,%rax
ret

LLVM IR (-O0):
define i64 @hweight64(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = ashr i32 %3, 1
%5 = sext i32 %4 to i64
%6 = and i64 %5, 6148914691236517205
%7 = load i32, i32* %2
%8 = sext i32 %7 to i64
%9 = sub i64 %8, %6
%10 = trunc i64 %9 to i32
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = and i64 %12, 3689348814741910323
%14 = load i32, i32* %2
%15 = ashr i32 %14, 2
%16 = sext i32 %15 to i64
%17 = and i64 %16, 3689348814741910323
%18 = add i64 %13, %17
%19 = trunc i64 %18 to i32
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = load i32, i32* %2
%22 = ashr i32 %21, 4
%23 = add nsw i32 %20, %22
%24 = sext i32 %23 to i64
%25 = and i64 %24, 1085102592571150095
%26 = trunc i64 %25 to i32
store i32 %26, i32* %2
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 72340172838076673
%30 = lshr i64 %29, 56
ret i64 %30
}

LLVM IR (-O2):
define i64 @hweight64(i32 %0) {
%2 = lshr i32 %0, 1
%3 = and i32 %2, 1431655765
%4 = sub i32 %0, %3
%5 = and i32 %4, 858993459
%6 = lshr i32 %4, 2
%7 = and i32 %6, 858993459
%8 = add nuw nsw i32 %7, %5
%9 = lshr i32 %8, 4
%10 = add nuw nsw i32 %9, %8
%11 = and i32 %10, 252645135
%12 = zext i32 %11 to i64
%13 = mul i64 %12, 72340172838076673
%14 = lshr i64 %13, 56
ret i64 %14
}