#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 */
scalar_t__ get_bits(unsigned char **, unsigned short *, unsigned char *, int);
int count_ones(int max, unsigned char **data, unsigned short *len,
               unsigned char *bitpos) {
  int i = 0;
  while (i < max && get_bits(data, len, bitpos, 1))
    i++;
  return i;
}


x86 assembly (clang, -O0):
<count_ones>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x4(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    115f <count_ones+0x4f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <get_bits@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    116f <count_ones+0x5f>
jmp    117d <count_ones+0x6d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    112e <count_ones+0x1e>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_ones>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edi,%edi
jle    1153 <count_ones+0x43>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1030 <get_bits@plt>
test   %rax,%rax
je     1155 <count_ones+0x45>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1130 <count_ones+0x20>
mov    %ebp,%ebx
jmp    1155 <count_ones+0x45>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_ones(i32 %0, i8** %1, i16* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i16*
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i16* %2, i16** %7
store i8* %3, i8** %8
store i32 0, i32* %9
br label %10
10: ; preds = %22, %4
%11 = load i32, i32* %9
%12 = load i32, i32* %5
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i8**, i8*** %6
%16 = load i16*, i16** %7
%17 = load i8*, i8** %8
%18 = call i64 @get_bits(i8** %15, i16* %16, i8* %17, i32 1)
%19 = icmp ne i64 %18, 0
br label %20
20: ; preds = %14, %10
%21 = phi i1 [ false, %10 ], [ %19, %14 ]
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = load i32, i32* %9
%24 = add nsw i32 %23, 1
store i32 %24, i32* %9
br label %10
25: ; preds = %20
%26 = load i32, i32* %9
ret i32 %26
}

LLVM IR (-O2):
define i32 @count_ones(i32 %0, i8** %1, i16* %2, i8* %3) {
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %13
6: ; preds = %4, %10
%7 = phi i32 [ %11, %10 ], [ 0, %4 ]
%8 = tail call i64 @get_bits(i8** %1, i16* %2, i8* %3, i32 1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = add nuw nsw i32 %7, 1
%12 = icmp eq i32 %11, %0
br i1 %12, label %13, label %6
13: ; preds = %6, %10, %4
%14 = phi i32 [ 0, %4 ], [ %0, %10 ], [ %7, %6 ]
ret i32 %14
}