#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*/ *);
void WriteNBits(int bits, int nBits, FILE *oFp) {
  while (nBits--) {
    if ((bits >> nBits) & 0x0001)
      arithmetic_encode(1, 2, 2, oFp);
    else
      arithmetic_encode(0, 1, 2, oFp);
  }
}


x86 assembly (clang, -O0):
<WriteNBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     117e <WriteNBits+0x6e>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1164 <WriteNBits+0x54>
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
mov    $0x2,%edx
mov    %edx,%esi
call   1030 <arithmetic_encode@plt>
jmp    1179 <WriteNBits+0x69>
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <arithmetic_encode@plt>
jmp    1122 <WriteNBits+0x12>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteNBits>:
push   %rbp
push   %r14
push   %rbx
test   %esi,%esi
je     115a <WriteNBits+0x4a>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
add    $0xffffffff,%ebp
jmp    1149 <WriteNBits+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1030 <arithmetic_encode@plt>
add    $0xffffffff,%ebp
jae    115a <WriteNBits+0x4a>
bt     %ebp,%ebx
jae    1130 <WriteNBits+0x20>
mov    $0x1,%edi
mov    $0x2,%esi
jmp    1137 <WriteNBits+0x27>
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
br label %7
7: ; preds = %23, %3
%8 = load i32, i32* %5
%9 = add nsw i32 %8, -1
store i32 %9, i32* %5
%10 = icmp ne i32 %8, 0
br i1 %10, label %11, label %24
11: ; preds = %7
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = ashr i32 %12, %13
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32*, i32** %6
%19 = call i32 @arithmetic_encode(i32 1, i32 2, i32 2, i32* %18)
br label %23
20: ; preds = %11
%21 = load i32*, i32** %6
%22 = call i32 @arithmetic_encode(i32 0, i32 1, i32 2, i32* %21)
br label %23
23: ; preds = %20, %17
br label %7
24: ; preds = %7
ret void
}

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