#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 __WORDSIZE;
int __bitmap_weight(unsigned long const *, int);
int hweight_long(unsigned long const);
int bitmap_weight(const unsigned long *src, int nbits) {
  if (nbits <= __WORDSIZE)
    return hweight_long(
        *src &
        (((nbits) % __WORDSIZE) ? (1UL << ((nbits) % __WORDSIZE)) - 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)
mov    -0x14(%rbp),%eax
mov    0x2eaf(%rip),%rcx
cmp    (%rcx),%eax
jg     11b0 <bitmap_weight+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e92(%rip),%rcx
cltd
idivl  (%rcx)
cmp    $0x0,%edx
je     1188 <bitmap_weight+0x68>
mov    -0x14(%rbp),%eax
mov    0x2e7c(%rip),%rcx
cltd
idivl  (%rcx)
mov    %edx,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1198 <bitmap_weight+0x78>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1198 <bitmap_weight+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
and    %rax,%rdi
call   1030 <hweight_long@plt>
mov    %eax,-0x4(%rbp)
jmp    11bf <bitmap_weight+0x9f>
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>:
mov    0x2ec1(%rip),%rax
mov    (%rax),%ecx
cmp    %esi,%ecx
jge    1132 <bitmap_weight+0x12>
jmp    1040 <__bitmap_weight@plt>
mov    %esi,%eax
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0xffffffffffffffff,%rdx
shl    %cl,%rdx
cmp    $0x1,%ecx
sbb    %rax,%rax
not    %rdx
or     %rdx,%rax
and    (%rdi),%rax
mov    %rax,%rdi
jmp    1030 <hweight_long@plt>

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

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