#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 floor(int /*<<< orphan*/);
int /*<<< orphan*/ log2(int);
int pow(int, int);
int bit_count(int num) {
  if (num <= 1)
    return num;
  int x = floor(log2(num));
  return x * pow(2, x - 1) + bit_count(num - pow(2, x)) + num + 1 - pow(2, x);
}


x86 assembly (clang, -O0):
<bit_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     1160 <bit_count+0x20>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <bit_count+0x97>
mov    -0x8(%rbp),%edi
call   1030 <log2@plt>
mov    %eax,%edi
call   1060 <floor@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x2,%edi
call   1050 <pow@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x2,%edi
call   1050 <pow@plt>
mov    -0x18(%rbp),%edi
sub    %eax,%edi
call   1040 <bit_count@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x2,%edi
call   1050 <pow@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
cmp    $0x1,%edi
jle    11a5 <bit_count+0x65>
mov    %ebx,%edi
call   1030 <log2@plt>
mov    %eax,%edi
call   1060 <floor@plt>
mov    %eax,%r14d
lea    -0x1(%r14),%esi
mov    $0x2,%edi
call   1050 <pow@plt>
mov    %eax,%ebp
imul   %r14d,%ebp
mov    $0x2,%edi
mov    %r14d,%esi
call   1050 <pow@plt>
mov    %ebx,%edi
sub    %eax,%edi
call   1040 <bit_count@plt>
mov    %eax,%r15d
mov    $0x2,%edi
mov    %r14d,%esi
call   1050 <pow@plt>
add    %ebx,%ebp
add    %r15d,%ebp
sub    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bit_count(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* %3
store i32 %8, i32* %2
br label %30
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @log2(i32 %10)
%12 = call i32 @floor(i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* %4
%15 = sub nsw i32 %14, 1
%16 = call i32 @pow(i32 2, i32 %15)
%17 = mul nsw i32 %13, %16
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = call i32 @pow(i32 2, i32 %19)
%21 = sub nsw i32 %18, %20
%22 = call i32 @bit_count(i32 %21)
%23 = add nsw i32 %17, %22
%24 = load i32, i32* %3
%25 = add nsw i32 %23, %24
%26 = add nsw i32 %25, 1
%27 = load i32, i32* %4
%28 = call i32 @pow(i32 2, i32 %27)
%29 = sub nsw i32 %26, %28
store i32 %29, i32* %2
br label %30
30: ; preds = %9, %7
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
define i32 @bit_count(i32 %0) {
%2 = icmp slt i32 %0, 2
br i1 %2, label %3, label %5
3: ; preds = %1, %5
%4 = phi i32 [ %18, %5 ], [ %0, %1 ]
ret i32 %4
5: ; preds = %1
%6 = tail call i32 @log2(i32 %0)
%7 = tail call i32 @floor(i32 %6)
%8 = add nsw i32 %7, -1
%9 = tail call i32 @pow(i32 2, i32 %8)
%10 = mul nsw i32 %9, %7
%11 = tail call i32 @pow(i32 2, i32 %7)
%12 = sub nsw i32 %0, %11
%13 = tail call i32 @bit_count(i32 %12)
%14 = tail call i32 @pow(i32 2, i32 %7)
%15 = add nuw i32 %0, 1
%16 = add i32 %15, %10
%17 = add i32 %16, %13
%18 = sub i32 %17, %14
br label %3
}