#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 uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
void bit_writer(FILE *f, uint8_t bit, uint8_t flush) {
  static uint8_t buffer;
  static uint8_t buffer_index = 0;
  if (flush) {
    for (; buffer_index < 8; ++buffer_index)
      buffer <<= 1;
  }
  if (buffer_index == 8) {
    fputc(buffer, f);
    buffer_index = 0;
  }
  buffer <<= 1;
  buffer |= bit ? 1 : 0;
  buffer_index++;
}


x86 assembly (clang, -O0):
<bit_writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1166 <bit_writer+0x56>
jmp    1131 <bit_writer+0x21>
cmpl   $0x8,0x2ef8(%rip)
jge    1161 <bit_writer+0x51>
mov    0x2ee8(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2edf(%rip)
mov    0x2edd(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ed4(%rip)
jmp    1131 <bit_writer+0x21>
jmp    1166 <bit_writer+0x56>
cmpl   $0x8,0x2ec3(%rip)
jne    118c <bit_writer+0x7c>
mov    0x2eb3(%rip),%edi
mov    -0x8(%rbp),%rsi
call   1030 <fputc@plt>
movl   $0x0,0x2ea4(%rip)
mov    0x2e9a(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2e91(%rip)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
or     0x2e7b(%rip),%eax
mov    %eax,0x2e75(%rip)
mov    0x2e73(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e6a(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_writer>:
push   %rbx
mov    %esi,%ebx
mov    %rdi,%rsi
mov    0x2f14(%rip),%eax
test   %edx,%edx
je     114f <bit_writer+0x3f>
cmp    $0x7,%eax
jg     114f <bit_writer+0x3f>
mov    0x2f01(%rip),%edi
mov    %eax,%ecx
neg    %ecx
and    $0x7,%ecx
je     115c <bit_writer+0x4c>
neg    %ecx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
add    %edi,%edi
add    $0xffffffff,%edx
cmp    %edx,%ecx
jne    1140 <bit_writer+0x30>
mov    %eax,%ecx
sub    %edx,%ecx
jmp    115e <bit_writer+0x4e>
cmp    $0x8,%eax
jne    118f <bit_writer+0x7f>
mov    0x2ed2(%rip),%edi
jmp    1188 <bit_writer+0x78>
mov    %eax,%ecx
add    $0xffffffff,%eax
cmp    $0x7,%eax
jb     1178 <bit_writer+0x68>
add    $0xfffffff8,%ecx
nopl   0x0(%rax)
shl    $0x8,%edi
add    $0x8,%ecx
jne    1170 <bit_writer+0x60>
mov    %edi,0x2eae(%rip)
movl   $0x8,0x2ea8(%rip)
call   1030 <fputc@plt>
xor    %eax,%eax
mov    0x2e97(%rip),%ecx
xor    %edx,%edx
test   %ebx,%ebx
setne  %dl
lea    (%rdx,%rcx,2),%ecx
mov    %ecx,0x2e87(%rip)
add    $0x1,%eax
mov    %eax,0x2e82(%rip)
pop    %rbx
ret

LLVM IR (-O0):
@bit_writer.buffer = internal global i32 0
@bit_writer.buffer_index = internal global i32 0
define void @bit_writer(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* %6
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %3
br label %10
10: ; preds = %16, %9
%11 = load i32, i32* @bit_writer.buffer_index
%12 = icmp slt i32 %11, 8
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* @bit_writer.buffer
%15 = shl i32 %14, 1
store i32 %15, i32* @bit_writer.buffer
br label %16
16: ; preds = %13
%17 = load i32, i32* @bit_writer.buffer_index
%18 = add nsw i32 %17, 1
store i32 %18, i32* @bit_writer.buffer_index
br label %10
19: ; preds = %10
br label %20
20: ; preds = %19, %3
%21 = load i32, i32* @bit_writer.buffer_index
%22 = icmp eq i32 %21, 8
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @bit_writer.buffer
%25 = load i32*, i32** %4
%26 = call i32 @fputc(i32 %24, i32* %25)
store i32 0, i32* @bit_writer.buffer_index
br label %27
27: ; preds = %23, %20
%28 = load i32, i32* @bit_writer.buffer
%29 = shl i32 %28, 1
store i32 %29, i32* @bit_writer.buffer
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 1, i32 0
%34 = load i32, i32* @bit_writer.buffer
%35 = or i32 %34, %33
store i32 %35, i32* @bit_writer.buffer
%36 = load i32, i32* @bit_writer.buffer_index
%37 = add nsw i32 %36, 1
store i32 %37, i32* @bit_writer.buffer_index
ret void
}

LLVM IR (-O2):
@bit_writer.buffer = internal unnamed_addr global i32 0
@bit_writer.buffer_index = internal unnamed_addr global i32 0
define void @bit_writer(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp ne i32 %2, 0
%5 = load i32, i32* @bit_writer.buffer_index
%6 = icmp slt i32 %5, 8
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %8, label %33
8: ; preds = %3
%9 = load i32, i32* @bit_writer.buffer
%10 = sub i32 0, %5
%11 = and i32 %10, 7
%12 = icmp eq i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %8, %13
%14 = phi i32 [ %17, %13 ], [ %9, %8 ]
%15 = phi i32 [ %18, %13 ], [ %5, %8 ]
%16 = phi i32 [ %19, %13 ], [ 0, %8 ]
%17 = shl i32 %14, 1
%18 = add i32 %15, 1
%19 = add i32 %16, 1
%20 = icmp eq i32 %19, %11
br i1 %20, label %21, label %13
21: ; preds = %13, %8
%22 = phi i32 [ undef, %8 ], [ %17, %13 ]
%23 = phi i32 [ %9, %8 ], [ %17, %13 ]
%24 = phi i32 [ %5, %8 ], [ %18, %13 ]
%25 = add i32 %5, -1
%26 = icmp ult i32 %25, 7
br i1 %26, label %37, label %27
27: ; preds = %21, %27
%28 = phi i32 [ %30, %27 ], [ %23, %21 ]
%29 = phi i32 [ %31, %27 ], [ %24, %21 ]
%30 = shl i32 %28, 8
%31 = add i32 %29, 8
%32 = icmp eq i32 %29, 0
br i1 %32, label %37, label %27
33: ; preds = %3
%34 = icmp eq i32 %5, 8
br i1 %34, label %35, label %42
35: ; preds = %33
%36 = load i32, i32* @bit_writer.buffer
br label %39
37: ; preds = %27, %21
%38 = phi i32 [ %22, %21 ], [ %30, %27 ]
store i32 %38, i32* @bit_writer.buffer
store i32 8, i32* @bit_writer.buffer_index
br label %39
39: ; preds = %35, %37
%40 = phi i32 [ %36, %35 ], [ %38, %37 ]
%41 = tail call i32 @fputc(i32 %40, i32* %0)
br label %42
42: ; preds = %39, %33
%43 = phi i32 [ 0, %39 ], [ %5, %33 ]
%44 = load i32, i32* @bit_writer.buffer
%45 = shl i32 %44, 1
%46 = icmp ne i32 %1, 0
%47 = zext i1 %46 to i32
%48 = or i32 %45, %47
store i32 %48, i32* @bit_writer.buffer
%49 = add nsw i32 %43, 1
store i32 %49, i32* @bit_writer.buffer_index
ret void
}