#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__ read_bit(unsigned long *, unsigned char const *);
unsigned read_bits(unsigned long *bitpointer, const unsigned char *bitstream,
                   unsigned long nbits) {
  unsigned result = 0, i;
  for (i = 0; i < nbits; i++)
    result |= ((unsigned)read_bit(bitpointer, bitstream)) << i;
  return result;
}


x86 assembly (clang, -O0):
<read_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1165 <read_bits+0x55>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <read_bit@plt>
mov    -0x20(%rbp),%ecx
shl    %cl,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1132 <read_bits+0x22>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bits>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     114b <read_bits+0x3b>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <read_bit@plt>
mov    %ebx,%ecx
shl    %cl,%eax
or     %eax,%ebp
add    $0x1,%ebx
cmp    %r12,%rbx
jb     1130 <read_bits+0x20>
jmp    114d <read_bits+0x3d>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_bits(i64* %0, i8* %1, i64 %2) {
%4 = alloca i64*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i64* %0, i64** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %23, %3
%10 = load i32, i32* %8
%11 = zext i32 %10 to i64
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %9
%15 = load i64*, i64** %4
%16 = load i8*, i8** %5
%17 = call i64 @read_bit(i64* %15, i8* %16)
%18 = trunc i64 %17 to i32
%19 = load i32, i32* %8
%20 = shl i32 %18, %19
%21 = load i32, i32* %7
%22 = or i32 %21, %20
store i32 %22, i32* %7
br label %23
23: ; preds = %14
%24 = load i32, i32* %8
%25 = add i32 %24, 1
store i32 %25, i32* %8
br label %9
26: ; preds = %9
%27 = load i32, i32* %7
ret i32 %27
}

LLVM IR (-O2):
define i32 @read_bits(i64* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %15, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %12, %5 ], [ 0, %3 ]
%7 = phi i32 [ %11, %5 ], [ 0, %3 ]
%8 = tail call i64 @read_bit(i64* %0, i8* %1)
%9 = trunc i64 %8 to i32
%10 = shl i32 %9, %6
%11 = or i32 %10, %7
%12 = add i32 %6, 1
%13 = zext i32 %12 to i64
%14 = icmp ult i64 %13, %2
br i1 %14, label %5, label %15
15: ; preds = %5, %3
%16 = phi i32 [ 0, %3 ], [ %11, %5 ]
ret i32 %16
}