#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 */
struct bits_reader {
  int dummy;
};

/* Variables and functions */
int get_bit(struct bits_reader *);
int read_bits(struct bits_reader *bits, int *b, int n) {
  int a = 0;
  for (int i = 0; i < n; ++i) {
    int b = get_bit(bits);
    if (b < 0)
      return b;
    a |= b << i;
  }
  *b = a;
  return 0;
}


x86 assembly (clang, -O0):
<read_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    117a <read_bits+0x6a>
mov    -0x10(%rbp),%rdi
call   1030 <get_bit@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    115e <read_bits+0x4e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118a <read_bits+0x7a>
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    %cl,%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <read_bits+0x21>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bits>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
xor    %r15d,%r15d
mov    $0x0,%ebp
test   %edx,%edx
jle    115a <read_bits+0x4a>
mov    %edx,%r13d
mov    %rdi,%r12
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1030 <get_bit@plt>
test   %eax,%eax
js     115f <read_bits+0x4f>
mov    %ebx,%ecx
shl    %cl,%eax
or     %eax,%ebp
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1140 <read_bits+0x30>
mov    %ebp,(%r14)
jmp    1162 <read_bits+0x52>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.bits_reader = type { i32 }
define i32 @read_bits(%struct.bits_reader* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %15
5: ; preds = %3, %10
%6 = phi i32 [ %13, %10 ], [ 0, %3 ]
%7 = phi i32 [ %12, %10 ], [ 0, %3 ]
%8 = tail call i32 @get_bit(%struct.bits_reader* %0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %5
%11 = shl i32 %8, %6
%12 = or i32 %11, %7
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, %2
br i1 %14, label %15, label %5
15: ; preds = %10, %3
%16 = phi i32 [ 0, %3 ], [ %12, %10 ]
store i32 %16, i32* %1
br label %17
17: ; preds = %5, %15
%18 = phi i32 [ 0, %15 ], [ %8, %5 ]
ret i32 %18
}