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


x86 assembly (clang, -O0):
<ReadNBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     11be <ReadNBits+0x9e>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x2,%edi
call   1030 <arithmetic_decode_target@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    119b <ReadNBits+0x7b>
mov    -0x14(%rbp),%eax
or     $0x0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
call   1040 <arithmetic_decode@plt>
jmp    11b9 <ReadNBits+0x99>
mov    -0x14(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%edx
mov    %edx,%esi
call   1040 <arithmetic_decode@plt>
jmp    1144 <ReadNBits+0x24>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadNBits>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
je     117a <ReadNBits+0x5a>
mov    %rsi,%r14
mov    %edi,%ebx
xor    %ebp,%ebp
jmp    115e <ReadNBits+0x3e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
or     $0x1,%ebp
mov    $0x1,%edi
mov    $0x2,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1040 <arithmetic_decode@plt>
test   %ebx,%ebx
je     117c <ReadNBits+0x5c>
add    $0xffffffff,%ebx
add    %ebp,%ebp
mov    $0x2,%edi
call   1030 <arithmetic_decode_target@plt>
test   %eax,%eax
jg     1140 <ReadNBits+0x20>
xor    %edi,%edi
mov    $0x1,%esi
jmp    114d <ReadNBits+0x2d>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ReadNBits(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2 x i32]
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
store i32 1, i32* %8
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
store i32 1, i32* %9
br label %10
10: ; preds = %30, %2
%11 = load i32, i32* %3
%12 = add nsw i32 %11, -1
store i32 %12, i32* %3
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %31
14: ; preds = %10
%15 = load i32, i32* %5
%16 = shl i32 %15, 1
store i32 %16, i32* %5
%17 = call i32 @arithmetic_decode_target(i32 2)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* %5
%22 = or i32 %21, 0
store i32 %22, i32* %5
%23 = load i32*, i32** %4
%24 = call i32 @arithmetic_decode(i32 0, i32 1, i32 2, i32* %23)
br label %30
25: ; preds = %14
%26 = load i32, i32* %5
%27 = or i32 %26, 1
store i32 %27, i32* %5
%28 = load i32*, i32** %4
%29 = call i32 @arithmetic_decode(i32 1, i32 2, i32 2, i32* %28)
br label %30
30: ; preds = %25, %20
br label %10
31: ; preds = %10
%32 = load i32, i32* %5
ret i32 %32
}

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