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

/* Variables and functions */
int vp9_rb_read_bit(struct vp9_read_bit_buffer *);
int vp9_rb_read_literal(struct vp9_read_bit_buffer *rb, int bits) {
  int value = 0, bit;
  for (bit = bits - 1; bit >= 0; bit--)
    value |= vp9_rb_read_bit(rb) << bit;
  return value;
}


x86 assembly (clang, -O0):
<vp9_rb_read_literal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     115b <vp9_rb_read_literal+0x4b>
mov    -0x8(%rbp),%rdi
call   1030 <vp9_rb_read_bit@plt>
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    112f <vp9_rb_read_literal+0x1f>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vp9_rb_read_literal>:
push   %rbp
push   %r14
push   %rbx
test   %esi,%esi
jle    1149 <vp9_rb_read_literal+0x39>
mov    %esi,%ebx
mov    %rdi,%r14
add    $0x1,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <vp9_rb_read_bit@plt>
lea    -0x2(%rbx),%ecx
shl    %cl,%eax
or     %eax,%ebp
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1130 <vp9_rb_read_literal+0x20>
jmp    114b <vp9_rb_read_literal+0x3b>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.vp9_read_bit_buffer = type { i32 }
define i32 @vp9_rb_read_literal(%struct.vp9_read_bit_buffer* %0, i32 %1) {
%3 = alloca %struct.vp9_read_bit_buffer*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store %struct.vp9_read_bit_buffer* %0, %struct.vp9_read_bit_buffer** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %6
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %6
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load %struct.vp9_read_bit_buffer*, %struct.vp9_read_bit_buffer** %3
%14 = call i32 @vp9_rb_read_bit(%struct.vp9_read_bit_buffer* %13)
%15 = load i32, i32* %6
%16 = shl i32 %14, %15
%17 = load i32, i32* %5
%18 = or i32 %17, %16
store i32 %18, i32* %5
br label %19
19: ; preds = %12
%20 = load i32, i32* %6
%21 = add nsw i32 %20, -1
store i32 %21, i32* %6
br label %9
22: ; preds = %9
%23 = load i32, i32* %5
ret i32 %23
}

LLVM IR (-O2):
%struct.vp9_read_bit_buffer = type { i32 }
define i32 @vp9_rb_read_literal(%struct.vp9_read_bit_buffer* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %12
4: ; preds = %2, %4
%5 = phi i32 [ %7, %4 ], [ %1, %2 ]
%6 = phi i32 [ %10, %4 ], [ 0, %2 ]
%7 = add nsw i32 %5, -1
%8 = tail call i32 @vp9_rb_read_bit(%struct.vp9_read_bit_buffer* %0)
%9 = shl i32 %8, %7
%10 = or i32 %9, %6
%11 = icmp ugt i32 %5, 1
br i1 %11, label %4, label %12
12: ; preds = %4, %2
%13 = phi i32 [ 0, %2 ], [ %10, %4 ]
ret i32 %13
}