#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 /*<<< orphan*/ dec_bin(unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int, ...);
int bitcount2(unsigned int x) {
  int b = 0;
  int rez = 0;
  while (x != 0) {
    printf("x = %10d; and in bin = %13lu;\n", x, dec_bin(x));
    b = (x & (x - 1));
    printf("b = %10d; and in bin = %13lu;\n", b, dec_bin(b));
    printf("x ^ b = %6d; and in bin = %13lu;\n", (x ^ b), dec_bin(x ^ b));
    if ((x ^ b) == 1)
      ++rez;
    x >>= 1;
    printf("x >>= 1 = %32lu;\n\n", dec_bin(x));
  }
  return rez;
}


x86 assembly (clang, -O0):
<bitcount2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11f8 <bitcount2+0xd8>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <dec_bin@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <dec_bin@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
xor    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
xor    -0x8(%rbp),%edi
call   1040 <dec_bin@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
xor    -0x8(%rbp),%eax
cmp    $0x1,%eax
jne    11d2 <bitcount2+0xb2>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <dec_bin@plt>
mov    %eax,%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1139 <bitcount2+0x19>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitcount2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     11d1 <bitcount2+0xb1>
mov    %edi,%ebx
lea    0xf02(%rip),%r12
lea    0xf1d(%rip),%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <dec_bin@plt>
lea    0xea2(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
lea    -0x1(%rbx),%r14d
and    %ebx,%r14d
mov    %r14d,%edi
call   1040 <dec_bin@plt>
lea    0xea0(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %ebx,%r14d
mov    %r14d,%edi
call   1040 <dec_bin@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
cmp    $0x1,%r14d
sete   %al
add    %eax,%r15d
mov    %ebx,%ebp
shr    %ebp
mov    %ebp,%edi
call   1040 <dec_bin@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x1,%ebx
mov    %ebp,%ebx
ja     1150 <bitcount2+0x30>
jmp    11d4 <bitcount2+0xb4>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"x = %10d; and in bin = %13lu;\0A\00"
@.str.1 = [31 x i8] c"b = %10d; and in bin = %13lu;\0A\00"
@.str.2 = [34 x i8] c"x ^ b = %6d; and in bin = %13lu;\0A\00"
@.str.3 = [19 x i8] c"x >>= 1 = %32lu;\0A\0A\00"
define i32 @bitcount2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %36, %1
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %42
8: ; preds = %5
%9 = load i32, i32* %2
%10 = load i32, i32* %2
%11 = call i32 @dec_bin(i32 %10)
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %11)
%13 = load i32, i32* %2
%14 = load i32, i32* %2
%15 = sub i32 %14, 1
%16 = and i32 %13, %15
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = load i32, i32* %3
%19 = call i32 @dec_bin(i32 %18)
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %19)
%21 = load i32, i32* %2
%22 = load i32, i32* %3
%23 = xor i32 %21, %22
%24 = load i32, i32* %2
%25 = load i32, i32* %3
%26 = xor i32 %24, %25
%27 = call i32 @dec_bin(i32 %26)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %27)
%29 = load i32, i32* %2
%30 = load i32, i32* %3
%31 = xor i32 %29, %30
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %8
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %36
36: ; preds = %33, %8
%37 = load i32, i32* %2
%38 = lshr i32 %37, 1
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = call i32 @dec_bin(i32 %39)
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %5
42: ; preds = %5
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [31 x i8] c"x = %10d; and in bin = %13lu;\0A\00"
@.str.1 = [31 x i8] c"b = %10d; and in bin = %13lu;\0A\00"
@.str.2 = [34 x i8] c"x ^ b = %6d; and in bin = %13lu;\0A\00"
@.str.3 = [19 x i8] c"x >>= 1 = %32lu;\0A\0A\00"
define i32 @bitcount2(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %22, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %17, %3 ], [ 0, %1 ]
%5 = phi i32 [ %18, %3 ], [ %0, %1 ]
%6 = tail call i32 @dec_bin(i32 %5)
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = add i32 %5, -1
%9 = and i32 %8, %5
%10 = tail call i32 @dec_bin(i32 %9)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10)
%12 = xor i32 %9, %5
%13 = tail call i32 @dec_bin(i32 %12)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
%15 = icmp eq i32 %12, 1
%16 = zext i1 %15 to i32
%17 = add nuw nsw i32 %4, %16
%18 = lshr i32 %5, 1
%19 = tail call i32 @dec_bin(i32 %18)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = icmp ult i32 %5, 2
br i1 %21, label %22, label %3
22: ; preds = %3, %1
%23 = phi i32 [ 0, %1 ], [ %17, %3 ]
ret i32 %23
}