#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 getc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int getbits(int numbits) {
  static int cb = 8, c;
  int ret;
  if (numbits < 0) {
    cb = 8;
    return (0);
  }
  ret = 0;
  for (; numbits > 0; numbits--) {
    if (cb >= 8) {
      c = getc(stdin);
      cb = 0;
    }
    ret <<= 1;
    if (c & (1 << (cb++)))
      ret |= 1;
  }
  return (ret);
}


x86 assembly (clang, -O0):
<getbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    113b <getbits+0x2b>
movl   $0x8,0x2ef9(%rip)
movl   $0x0,-0x4(%rbp)
jmp    11cd <getbits+0xbd>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11c7 <getbits+0xb7>
cmpl   $0x8,0x2ed5(%rip)
jl     1177 <getbits+0x67>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
call   1030 <getc@plt>
mov    %eax,0x2ec3(%rip)
movl   $0x0,0x2eb1(%rip)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2eaa(%rip),%eax
mov    0x2e9c(%rip),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,0x2e91(%rip)
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11b4 <getbits+0xa4>
mov    -0xc(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b9 <getbits+0xa9>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1142 <getbits+0x32>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbits>:
push   %r15
push   %r14
push   %rbx
test   %edi,%edi
js     1179 <getbits+0x69>
je     1183 <getbits+0x73>
mov    %edi,%ebx
mov    0x2f05(%rip),%ecx
add    $0x1,%ebx
xor    %r14d,%r14d
mov    0x2eb8(%rip),%r15
jmp    116c <getbits+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%edi
call   1030 <getc@plt>
mov    %eax,0x2ee2(%rip)
xor    %ecx,%ecx
lea    0x1(%rcx),%edx
mov    %edx,0x2ecf(%rip)
shr    %cl,%eax
and    $0x1,%eax
lea    (%rax,%r14,2),%r14d
add    $0xffffffff,%ebx
mov    %edx,%ecx
cmp    $0x1,%ebx
jle    1186 <getbits+0x76>
cmp    $0x7,%ecx
jg     1140 <getbits+0x30>
mov    0x2eb9(%rip),%eax
jmp    1150 <getbits+0x40>
movl   $0x8,0x2ea5(%rip)
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@getbits.cb = internal global i32 8
@getbits.c = internal global i32 0
@stdin = global i32 0
define i32 @getbits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 8, i32* @getbits.cb
store i32 0, i32* %2
br label %36
8: ; preds = %1
store i32 0, i32* %4
br label %9
9: ; preds = %31, %8
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i32, i32* @getbits.cb
%14 = icmp sge i32 %13, 8
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stdin
%17 = call i32 @getc(i32 %16)
store i32 %17, i32* @getbits.c
store i32 0, i32* @getbits.cb
br label %18
18: ; preds = %15, %12
%19 = load i32, i32* %4
%20 = shl i32 %19, 1
store i32 %20, i32* %4
%21 = load i32, i32* @getbits.c
%22 = load i32, i32* @getbits.cb
%23 = add nsw i32 %22, 1
store i32 %23, i32* @getbits.cb
%24 = shl i32 1, %22
%25 = and i32 %21, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = load i32, i32* %4
%29 = or i32 %28, 1
store i32 %29, i32* %4
br label %30
30: ; preds = %27, %18
br label %31
31: ; preds = %30
%32 = load i32, i32* %3
%33 = add nsw i32 %32, -1
store i32 %33, i32* %3
br label %9
34: ; preds = %9
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %7
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@getbits.cb = internal unnamed_addr global i32 8
@getbits.c = internal unnamed_addr global i32 0
@stdin = global i32 0
define i32 @getbits(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %7, label %3
3: ; preds = %1
%4 = icmp eq i32 %0, 0
br i1 %4, label %28, label %5
5: ; preds = %3
%6 = load i32, i32* @getbits.cb
br label %8
7: ; preds = %1
store i32 8, i32* @getbits.cb
br label %28
8: ; preds = %5, %18
%9 = phi i32 [ %22, %18 ], [ %6, %5 ]
%10 = phi i32 [ %25, %18 ], [ 0, %5 ]
%11 = phi i32 [ %26, %18 ], [ %0, %5 ]
%12 = icmp sgt i32 %9, 7
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = load i32, i32* @getbits.c
br label %18
15: ; preds = %8
%16 = load i32, i32* @stdin
%17 = tail call i32 @getc(i32 %16)
store i32 %17, i32* @getbits.c
br label %18
18: ; preds = %13, %15
%19 = phi i32 [ %9, %13 ], [ 0, %15 ]
%20 = phi i32 [ %14, %13 ], [ %17, %15 ]
%21 = shl i32 %10, 1
%22 = add nsw i32 %19, 1
store i32 %22, i32* @getbits.cb
%23 = lshr i32 %20, %19
%24 = and i32 %23, 1
%25 = or i32 %24, %21
%26 = add nsw i32 %11, -1
%27 = icmp sgt i32 %11, 1
br i1 %27, label %8, label %28
28: ; preds = %18, %3, %7
%29 = phi i32 [ 0, %7 ], [ 0, %3 ], [ %25, %18 ]
ret i32 %29
}