#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 */
void *readbits(unsigned char **, int *, int);
void readmanybits(unsigned char **ptr, int *bitptr, int nbits,
                  unsigned char *buffer) {
  while (nbits >= 8) {
    *buffer++ = readbits(ptr, bitptr, 8);
    nbits -= 8;
  }
  if (nbits) {
    *buffer++ = readbits(ptr, bitptr, nbits);
  }
}


x86 assembly (clang, -O0):
<readmanybits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x8,-0x14(%rbp)
jl     1164 <readmanybits+0x54>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <readbits@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <readmanybits+0x17>
cmpl   $0x0,-0x14(%rbp)
je     1191 <readmanybits+0x81>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <readbits@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readmanybits>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
cmp    $0x8,%edx
jl     1171 <readmanybits+0x61>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <readbits@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
lea    -0x8(%r12),%edx
cmp    $0xf,%r12d
mov    %edx,%r12d
ja     1130 <readmanybits+0x20>
test   %edx,%edx
je     1165 <readmanybits+0x55>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <readbits@plt>
mov    %al,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12d,%edx
test   %edx,%edx
jne    1158 <readmanybits+0x48>
jmp    1165 <readmanybits+0x55>

LLVM IR (-O0):
define void @readmanybits(i8** %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
br label %9
9: ; preds = %12, %4
%10 = load i32, i32* %7
%11 = icmp sge i32 %10, 8
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = load i8**, i8*** %5
%14 = load i32*, i32** %6
%15 = call i8* @readbits(i8** %13, i32* %14, i32 8)
%16 = ptrtoint i8* %15 to i8
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %8
store i8 %16, i8* %17
%19 = load i32, i32* %7
%20 = sub nsw i32 %19, 8
store i32 %20, i32* %7
br label %9
21: ; preds = %9
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = load i32*, i32** %6
%27 = load i32, i32* %7
%28 = call i8* @readbits(i8** %25, i32* %26, i32 %27)
%29 = ptrtoint i8* %28 to i8
%30 = load i8*, i8** %8
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %8
store i8 %29, i8* %30
br label %32
32: ; preds = %24, %21
ret void
}

LLVM IR (-O2):
define void @readmanybits(i8** %0, i32* %1, i32 %2, i8* nocapture writeonly %3) {
%5 = icmp sgt i32 %2, 7
br i1 %5, label %6, label %15
6: ; preds = %4, %6
%7 = phi i8* [ %12, %6 ], [ %3, %4 ]
%8 = phi i32 [ %13, %6 ], [ %2, %4 ]
%9 = tail call i8* @readbits(i8** %0, i32* %1, i32 8)
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i8
%12 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %11, i8* %7
%13 = add nsw i32 %8, -8
%14 = icmp ugt i32 %8, 15
br i1 %14, label %6, label %15
15: ; preds = %6, %4
%16 = phi i32 [ %2, %4 ], [ %13, %6 ]
%17 = phi i8* [ %3, %4 ], [ %12, %6 ]
%18 = icmp eq i32 %16, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = tail call i8* @readbits(i8** %0, i32* %1, i32 %16)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i8
store i8 %22, i8* %17
br label %23
23: ; preds = %19, %15
ret void
}