#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 GetSymbol(int *, int *, int *, int, int);
int /*<<< orphan*/ arithmetic_decode(int, int, int, int /*<<< orphan*/ *);
int arithmetic_decode_target(int);
int /*<<< orphan*/ assert(int);
int ReadNBits(int nBits, FILE *iFp) {
  int bits = 0, target, low, high, count[2] = {1, 1};
  assert(nBits >= 0 && nBits <= sizeof(int) * 8);
  while (nBits--) {
    bits <<= 1;
    target = arithmetic_decode_target(2);
    bits |= GetSymbol(&low, &high, count, target, 2);
    arithmetic_decode(low, high, 2, iFp);
  }
  return bits;
}


x86 assembly (clang, -O0):
<ReadNBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xea3(%rip),%rax
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0x29(%rbp)
jl     117e <ReadNBits+0x3e>
movslq -0x4(%rbp),%rax
cmp    $0x20,%rax
setbe  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     11ee <ReadNBits+0xae>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x2,%edi
call   1040 <arithmetic_decode_target@plt>
mov    %eax,-0x18(%rbp)
lea    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x2,%r8d
call   1050 <GetSymbol@plt>
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x2,%edx
call   1060 <arithmetic_decode@plt>
jmp    118b <ReadNBits+0x4b>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadNBits>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movabs $0x100000001,%rax
mov    %rax,0x10(%rsp)
xor    %edi,%edi
cmp    $0x21,%ebp
setb   %dil
call   1030 <assert@plt>
xor    %ebx,%ebx
test   %ebp,%ebp
je     11ce <ReadNBits+0x8e>
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebx,%ebx
mov    $0x2,%edi
call   1040 <arithmetic_decode_target@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %eax,%ecx
mov    $0x2,%r8d
call   1050 <GetSymbol@plt>
or     %eax,%ebx
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1060 <arithmetic_decode@plt>
add    $0xffffffff,%ebp
jne    1190 <ReadNBits+0x50>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.ReadNBits.count = [2 x i32] [i32 1, i32 1]
define i32 @ReadNBits(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %5
%10 = bitcast [2 x i32]* %9 to i8*
%11 = load i32, i32* %3
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = icmp ule i64 %15, 32
br label %17
17: ; preds = %13, %2
%18 = phi i1 [ false, %2 ], [ %16, %13 ]
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
br label %21
21: ; preds = %25, %17
%22 = load i32, i32* %3
%23 = add nsw i32 %22, -1
store i32 %23, i32* %3
%24 = icmp ne i32 %22, 0
br i1 %24, label %25, label %38
25: ; preds = %21
%26 = load i32, i32* %5
%27 = shl i32 %26, 1
store i32 %27, i32* %5
%28 = call i32 @arithmetic_decode_target(i32 2)
store i32 %28, i32* %6
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%30 = load i32, i32* %6
%31 = call i32 @GetSymbol(i32* %7, i32* %8, i32* %29, i32 %30, i32 2)
%32 = load i32, i32* %5
%33 = or i32 %32, %31
store i32 %33, i32* %5
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = load i32*, i32** %4
%37 = call i32 @arithmetic_decode(i32 %34, i32 %35, i32 2, i32* %36)
br label %21
38: ; preds = %21
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
define i32 @ReadNBits(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i64* %5 to i8*
store i64 4294967297, i64* %5
%9 = icmp ult i32 %0, 33
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp eq i32 %0, 0
br i1 %12, label %27, label %13
13: ; preds = %2
%14 = bitcast i64* %5 to i32*
br label %15
15: ; preds = %13, %15
%16 = phi i32 [ %0, %13 ], [ %18, %15 ]
%17 = phi i32 [ 0, %13 ], [ %22, %15 ]
%18 = add nsw i32 %16, -1
%19 = shl i32 %17, 1
%20 = call i32 @arithmetic_decode_target(i32 2)
%21 = call i32 @GetSymbol(i32* %3, i32* %4, i32* %14, i32 %20, i32 2)
%22 = or i32 %21, %19
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = call i32 @arithmetic_decode(i32 %23, i32 %24, i32 2, i32* %1)
%26 = icmp eq i32 %18, 0
br i1 %26, label %27, label %15
27: ; preds = %15, %2
%28 = phi i32 [ 0, %2 ], [ %22, %15 ]
ret i32 %28
}