#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 */
typedef int uint32_t;

/* Variables and functions */
int bit_count(uint32_t i) {
  int c = 0;
  unsigned int seen_one = 0;
  while (i > 0) {
    if (i & 1) {
      seen_one = 1;
      c++;
    } else {
      if (seen_one) {
        return -1;
      }
    }
    i >>= 1;
  }
  return c;
}


x86 assembly (clang, -O0):
<bit_count>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    116c <bit_count+0x6c>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1143 <bit_count+0x43>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115e <bit_count+0x5e>
cmpl   $0x0,-0x10(%rbp)
je     1159 <bit_count+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1172 <bit_count+0x72>
jmp    115e <bit_count+0x5e>
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1115 <bit_count+0x15>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_count>:
test   %edi,%edi
jle    1138 <bit_count+0x38>
xor    %ecx,%ecx
xor    %eax,%eax
jmp    1123 <bit_count+0x23>
nopw   0x0(%rax,%rax,1)
add    $0x1,%eax
mov    $0x1,%ecx
mov    %edi,%edx
shr    %edx
cmp    $0x1,%edi
mov    %edx,%edi
jbe    1137 <bit_count+0x37>
test   $0x1,%dil
jne    1110 <bit_count+0x10>
test   %ecx,%ecx
mov    $0x0,%ecx
je     1118 <bit_count+0x18>
mov    $0xffffffff,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @bit_count(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %21, %1
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %6
%10 = load i32, i32* %3
%11 = and i32 %10, 1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
store i32 1, i32* %5
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
br label %21
16: ; preds = %9
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -1, i32* %2
br label %26
20: ; preds = %16
br label %21
21: ; preds = %20, %13
%22 = load i32, i32* %3
%23 = ashr i32 %22, 1
store i32 %23, i32* %3
br label %6
24: ; preds = %6
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @bit_count(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %18
3: ; preds = %1, %13
%4 = phi i32 [ %15, %13 ], [ 0, %1 ]
%5 = phi i32 [ %14, %13 ], [ 0, %1 ]
%6 = phi i32 [ %16, %13 ], [ %0, %1 ]
%7 = and i32 %6, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = add nsw i32 %5, 1
br label %13
11: ; preds = %3
%12 = icmp eq i32 %4, 0
br i1 %12, label %13, label %18
13: ; preds = %11, %9
%14 = phi i32 [ %10, %9 ], [ %5, %11 ]
%15 = phi i32 [ 1, %9 ], [ 0, %11 ]
%16 = lshr i32 %6, 1
%17 = icmp ult i32 %6, 2
br i1 %17, label %18, label %3
18: ; preds = %11, %13, %1
%19 = phi i32 [ 0, %1 ], [ %14, %13 ], [ -1, %11 ]
ret i32 %19
}