#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_encode(int, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
void WriteNBits(int bits, int nBits, FILE *oFp) {
  assert(nBits >= 0 && nBits <= sizeof(int) * 8);
  while (nBits--) {
    if (((unsigned)bits >> nBits) & 0x1)
      arithmetic_encode(1, 2, 2, oFp);
    else
      arithmetic_encode(0, 1, 2, oFp);
  }
}


x86 assembly (clang, -O0):
<WriteNBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
jl     114f <WriteNBits+0x2f>
movslq -0x8(%rbp),%rax
cmp    $0x20,%rax
setbe  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     11b8 <WriteNBits+0x98>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
shr    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     119e <WriteNBits+0x7e>
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%edx
mov    %edx,%esi
call   1030 <arithmetic_encode@plt>
jmp    11b3 <WriteNBits+0x93>
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <arithmetic_encode@plt>
jmp    115c <WriteNBits+0x3c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteNBits>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
xor    %edi,%edi
cmp    $0x21,%esi
setb   %dil
call   1040 <assert@plt>
test   %ebp,%ebp
je     117a <WriteNBits+0x5a>
add    $0xffffffff,%ebp
jmp    1169 <WriteNBits+0x49>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1030 <arithmetic_encode@plt>
add    $0xffffffff,%ebp
jae    117a <WriteNBits+0x5a>
bt     %ebp,%ebx
jae    1150 <WriteNBits+0x30>
mov    $0x1,%edi
mov    $0x2,%esi
jmp    1157 <WriteNBits+0x37>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @WriteNBits(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%7 = load i32, i32* %5
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = icmp ule i64 %11, 32
br label %13
13: ; preds = %9, %3
%14 = phi i1 [ false, %3 ], [ %12, %9 ]
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
br label %17
17: ; preds = %33, %13
%18 = load i32, i32* %5
%19 = add nsw i32 %18, -1
store i32 %19, i32* %5
%20 = icmp ne i32 %18, 0
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = lshr i32 %22, %23
%25 = and i32 %24, 1
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32*, i32** %6
%29 = call i32 @arithmetic_encode(i32 1, i32 2, i32 2, i32* %28)
br label %33
30: ; preds = %21
%31 = load i32*, i32** %6
%32 = call i32 @arithmetic_encode(i32 0, i32 1, i32 2, i32* %31)
br label %33
33: ; preds = %30, %27
br label %17
34: ; preds = %17
ret void
}

LLVM IR (-O2):
define void @WriteNBits(i32 %0, i32 %1, i32* %2) {
%4 = icmp ult i32 %1, 33
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp eq i32 %1, 0
br i1 %7, label %20, label %8
8: ; preds = %3, %18
%9 = phi i32 [ %10, %18 ], [ %1, %3 ]
%10 = add nsw i32 %9, -1
%11 = shl nuw i32 1, %10
%12 = and i32 %11, %0
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @arithmetic_encode(i32 1, i32 2, i32 2, i32* %2)
br label %18
16: ; preds = %8
%17 = tail call i32 @arithmetic_encode(i32 0, i32 1, i32 2, i32* %2)
br label %18
18: ; preds = %16, %14
%19 = icmp eq i32 %10, 0
br i1 %19, label %20, label %8
20: ; preds = %18, %3
ret void
}