int bitcnt_f(unsigned int a) {
  int count = sizeof(a) * 8;
  int loop = 0;
  printf("%s:  Value: 0x%x  Complement with Negation: 0x%x\n", __FUNCTION__, a,
         ~a);
  a = ~a;
  while (a) {
    loop++;
    count--;
    a &= (a - 1);
  }
  printf("%s:   Loop count: %d\n", __FUNCTION__, loop);
  return count;
}


x86 assembly (clang, -O0):
<bitcnt_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x20,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edx
mov    -0x4(%rbp),%ecx
xor    $0xffffffff,%ecx
lea    0xec7(%rip),%rdi
lea    0xef2(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     117d <bitcnt_f+0x6d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
and    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1150 <bitcnt_f+0x40>
mov    -0xc(%rbp),%edx
lea    0xeb4(%rip),%rdi
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitcnt_f>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    %edi,%ebx
not    %ebx
lea    0xede(%rip),%rdi
lea    0xf09(%rip),%rsi
xor    %ebp,%ebp
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0xffffffff,%r14d
je     1155 <bitcnt_f+0x45>
xor    %ebp,%ebp
nop
add    $0x1,%ebp
lea    -0x1(%rbx),%eax
and    %ebx,%eax
mov    %eax,%ebx
jne    1140 <bitcnt_f+0x30>
mov    $0x20,%ebx
sub    %ebp,%ebx
jmp    115a <bitcnt_f+0x4a>
mov    $0x20,%ebx
lea    0xeda(%rip),%rdi
lea    0xeca(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"%s: Value: 0x%x Complement with Negation: 0x%x\0A\00"
@__FUNCTION__.bitcnt_f = [9 x i8] c"bitcnt_f\00"
@.str.1 = [22 x i8] c"%s: Loop count: %d\0A\00"
define i32 @bitcnt_f(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 32, i32* %3
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = load i32, i32* %2
%7 = xor i32 %6, -1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.bitcnt_f, i64 0, i64 0), i32 %5, i32 %7)
%9 = load i32, i32* %2
%10 = xor i32 %9, -1
store i32 %10, i32* %2
br label %11
11: ; preds = %14, %1
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
%17 = load i32, i32* %3
%18 = add nsw i32 %17, -1
store i32 %18, i32* %3
%19 = load i32, i32* %2
%20 = sub i32 %19, 1
%21 = load i32, i32* %2
%22 = and i32 %21, %20
store i32 %22, i32* %2
br label %11
23: ; preds = %11
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.bitcnt_f, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [50 x i8] c"%s: Value: 0x%x Complement with Negation: 0x%x\0A\00"
@__FUNCTION__.bitcnt_f = [9 x i8] c"bitcnt_f\00"
@.str.1 = [22 x i8] c"%s: Loop count: %d\0A\00"
define i32 @bitcnt_f(i32 %0) {
%2 = xor i32 %0, -1
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.bitcnt_f, i64 0, i64 0), i32 %0, i32 %2)
%4 = icmp eq i32 %0, -1
br i1 %4, label %14, label %5
5: ; preds = %1, %5
%6 = phi i32 [ %9, %5 ], [ 0, %1 ]
%7 = phi i32 [ %10, %5 ], [ 32, %1 ]
%8 = phi i32 [ %12, %5 ], [ %2, %1 ]
%9 = add nuw nsw i32 %6, 1
%10 = add nsw i32 %7, -1
%11 = add i32 %8, -1
%12 = and i32 %11, %8
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %5
14: ; preds = %5, %1
%15 = phi i32 [ 32, %1 ], [ %10, %5 ]
%16 = phi i32 [ 0, %1 ], [ %9, %5 ]
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.bitcnt_f, i64 0, i64 0), i32 %16)
ret i32 %15
}