#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 bit_stream {
  int dummy;
};

/* Variables and functions */
int bit_stream_get(struct bit_stream *);
int bit_stream_shift_out(struct bit_stream *stream, unsigned *out, int n) {
  int bit, idx;
  unsigned val;
  unsigned shift = 0;
  val = *out;
  for (idx = 0; idx < n; idx++) {
    bit = bit_stream_get(stream);
    if (bit < 0)
      return bit;
    val |= (bit << shift++);
  }
  *out = val;
  return 0;
}


x86 assembly (clang, -O0):
<bit_stream_shift_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118b <bit_stream_shift_out+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <bit_stream_get@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1167 <bit_stream_shift_out+0x57>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <bit_stream_shift_out+0x8b>
mov    -0x20(%rbp),%eax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
shl    %cl,%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <bit_stream_shift_out+0x2a>
mov    -0x28(%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):
<bit_stream_shift_out>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    (%rsi),%ebp
test   %edx,%edx
jle    114c <bit_stream_shift_out+0x3c>
mov    %edx,%r12d
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <bit_stream_get@plt>
test   %eax,%eax
js     1151 <bit_stream_shift_out+0x41>
mov    %ebx,%ecx
shl    %cl,%eax
lea    0x1(%rbx),%ecx
or     %eax,%ebp
mov    %ecx,%ebx
cmp    %ecx,%r12d
jne    1130 <bit_stream_shift_out+0x20>
mov    %ebp,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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