#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int read_bit(int /*<<< orphan*/ *);
int read_bits(int n, FILE *in) {
  int res = 0;
  for (int i = n; i >= 0; i--) {
    int c = read_bit(in);
    if (c == EOF) {
      if (i == n) {
        return EOF;
      } else {
        c = 0;
      }
    }
    res <<= 1;
    res |= c;
  }
  return res;
}


x86 assembly (clang, -O0):
<read_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     119d <read_bits+0x8d>
mov    -0x10(%rbp),%rdi
call   1030 <read_bit@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e9c(%rip),%rcx
cmp    (%rcx),%eax
jne    117d <read_bits+0x6d>
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1171 <read_bits+0x61>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <read_bits+0x93>
movl   $0x0,-0x1c(%rbp)
jmp    117d <read_bits+0x6d>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    112c <read_bits+0x1c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%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   %edi,%edi
js     116f <read_bits+0x5f>
mov    %rsi,%r14
mov    %edi,%r12d
add    $0x1,%r12d
xor    %ebx,%ebx
mov    0x2eb9(%rip),%r15
xor    %ebp,%ebp
jmp    115a <read_bits+0x4a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%ecx
add    %ebp,%ebp
or     %ecx,%ebp
lea    (%r12,%rbx,1),%ecx
add    $0xffffffff,%ecx
mov    %ebx,%eax
add    $0xffffffff,%eax
mov    %eax,%ebx
mov    %ebp,%eax
test   %ecx,%ecx
jle    1171 <read_bits+0x61>
mov    %r14,%rdi
call   1030 <read_bit@plt>
cmp    (%r15),%eax
jne    1140 <read_bits+0x30>
xor    %ecx,%ecx
test   %ebx,%ebx
jne    1142 <read_bits+0x32>
jmp    1171 <read_bits+0x61>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@EOF = global i32 0
define i32 @read_bits(i32 %0, i32* %1) {
%3 = icmp sgt i32 %0, -1
br i1 %3, label %4, label %18
4: ; preds = %2, %12
%5 = phi i32 [ %16, %12 ], [ %0, %2 ]
%6 = phi i32 [ %15, %12 ], [ 0, %2 ]
%7 = tail call i32 @read_bit(i32* %1)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = icmp eq i32 %5, %0
br i1 %11, label %18, label %12
12: ; preds = %4, %10
%13 = phi i32 [ %7, %4 ], [ 0, %10 ]
%14 = shl i32 %6, 1
%15 = or i32 %13, %14
%16 = add nsw i32 %5, -1
%17 = icmp sgt i32 %5, 0
br i1 %17, label %4, label %18
18: ; preds = %12, %10, %2
%19 = phi i32 [ 0, %2 ], [ %7, %10 ], [ %15, %12 ]
ret i32 %19
}