#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 */

/* Variables and functions */
int __bitmap_weight(unsigned long const *, int);
int hweight_long(unsigned long const);
int bitmap_weight(const unsigned long *src, int nbits) {
  if (nbits <= ((int)sizeof(unsigned long) * 8))
    return hweight_long(
        *src & (((nbits) % ((int)sizeof(unsigned long) * 8))
                    ? (1UL << ((nbits) % ((int)sizeof(unsigned long) * 8))) - 1
                    : ~0UL));
  return __bitmap_weight(src, nbits);
}


x86 assembly (clang, -O0):
<bitmap_weight>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x40,-0x14(%rbp)
jg     11a4 <bitmap_weight+0x84>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     117c <bitmap_weight+0x5c>
mov    -0x14(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <bitmap_weight+0x6c>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <bitmap_weight+0x6c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
and    %rax,%rdi
call   1030 <hweight_long@plt>
mov    %eax,-0x4(%rbp)
jmp    11b3 <bitmap_weight+0x93>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <__bitmap_weight@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmap_weight>:
cmp    $0x40,%esi
jg     1155 <bitmap_weight+0x35>
lea    0x3f(%rsi),%eax
test   %esi,%esi
cmovns %esi,%eax
and    $0xffffffc0,%eax
sub    %eax,%esi
mov    $0xffffffffffffffff,%rdx
mov    %esi,%ecx
shl    %cl,%rdx
cmp    $0x1,%esi
sbb    %rax,%rax
not    %rdx
or     %rdx,%rax
and    (%rdi),%rax
mov    %rax,%rdi
jmp    1030 <hweight_long@plt>
jmp    1040 <__bitmap_weight@plt>

LLVM IR (-O0):
define i32 @bitmap_weight(i64* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32
store i64* %0, i64** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp sle i32 %6, 64
br i1 %7, label %8, label %25
8: ; preds = %2
%9 = load i64*, i64** %4
%10 = load i64, i64* %9
%11 = load i32, i32* %5
%12 = srem i32 %11, 64
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i32, i32* %5
%16 = srem i32 %15, 64
%17 = zext i32 %16 to i64
%18 = shl i64 1, %17
%19 = sub i64 %18, 1
br label %21
20: ; preds = %8
br label %21
21: ; preds = %20, %14
%22 = phi i64 [ %19, %14 ], [ -1, %20 ]
%23 = and i64 %10, %22
%24 = call i32 @hweight_long(i64 %23)
store i32 %24, i32* %3
br label %29
25: ; preds = %2
%26 = load i64*, i64** %4
%27 = load i32, i32* %5
%28 = call i32 @__bitmap_weight(i64* %26, i32 %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %25, %21
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @bitmap_weight(i64* %0, i32 %1) {
%3 = icmp slt i32 %1, 65
br i1 %3, label %4, label %14
4: ; preds = %2
%5 = load i64, i64* %0
%6 = srem i32 %1, 64
%7 = icmp eq i32 %6, 0
%8 = zext i32 %6 to i64
%9 = shl nsw i64 -1, %8
%10 = xor i64 %9, -1
%11 = select i1 %7, i64 -1, i64 %10
%12 = and i64 %5, %11
%13 = tail call i32 @hweight_long(i64 %12)
br label %16
14: ; preds = %2
%15 = tail call i32 @__bitmap_weight(i64* %0, i32 %1)
br label %16
16: ; preds = %14, %4
%17 = phi i32 [ %13, %4 ], [ %15, %14 ]
ret i32 %17
}