#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
void write1bit(FILE *outfile, int byte) {
  static unsigned char buf, mask;
  static int inbuf;
  if (!byte)
    mask = 1 << (7 - inbuf);
  else
    mask = 0;
  buf = buf | mask;
  inbuf++;
  if (inbuf == 8) {
    fwrite(&buf, sizeof(unsigned char), 1, outfile);
    inbuf = 0;
    buf = 0;
  }
}


x86 assembly (clang, -O0):
<write1bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1146 <write1bit+0x36>
mov    $0x7,%ecx
sub    0x2efc(%rip),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %al,0x2eec(%rip)
jmp    114d <write1bit+0x3d>
movb   $0x0,0x2ee0(%rip)
movzbl 0x2ed8(%rip),%eax
movzbl 0x2ed2(%rip),%ecx
or     %ecx,%eax
mov    %al,0x2ec9(%rip)
mov    0x2ec7(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ebe(%rip)
cmpl   $0x8,0x2eb7(%rip)
jne    11a7 <write1bit+0x97>
mov    -0x8(%rbp),%rcx
lea    0x2ea2(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fwrite@plt>
movl   $0x0,0x2e90(%rip)
movb   $0x0,0x2e85(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write1bit>:
push   %rax
mov    0x2f19(%rip),%edx
mov    $0x7,%cl
sub    %dl,%cl
mov    $0x1,%eax
shl    %cl,%eax
xor    %ecx,%ecx
test   %esi,%esi
cmove  %eax,%ecx
or     0x2efd(%rip),%cl
mov    %cl,0x2ef7(%rip)
add    $0x1,%edx
mov    %edx,0x2ef2(%rip)
cmp    $0x8,%edx
jne    1162 <write1bit+0x52>
mov    %rdi,%r8
movsbl %cl,%edi
mov    %r8,%rsi
call   1030 <fputc@plt>
movl   $0x0,0x2ed5(%rip)
movb   $0x0,0x2eca(%rip)
pop    %rax
ret

LLVM IR (-O0):
@write1bit.buf = internal global i8 0
@write1bit.mask = internal global i8 0
@write1bit.inbuf = internal global i32 0
define void @write1bit(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp ne i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @write1bit.inbuf
%9 = sub nsw i32 7, %8
%10 = shl i32 1, %9
%11 = trunc i32 %10 to i8
store i8 %11, i8* @write1bit.mask
br label %13
12: ; preds = %2
store i8 0, i8* @write1bit.mask
br label %13
13: ; preds = %12, %7
%14 = load i8, i8* @write1bit.buf
%15 = zext i8 %14 to i32
%16 = load i8, i8* @write1bit.mask
%17 = zext i8 %16 to i32
%18 = or i32 %15, %17
%19 = trunc i32 %18 to i8
store i8 %19, i8* @write1bit.buf
%20 = load i32, i32* @write1bit.inbuf
%21 = add nsw i32 %20, 1
store i32 %21, i32* @write1bit.inbuf
%22 = load i32, i32* @write1bit.inbuf
%23 = icmp eq i32 %22, 8
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i32*, i32** %3
%26 = call i32 @fwrite(i8* @write1bit.buf, i32 1, i32 1, i32* %25)
store i32 0, i32* @write1bit.inbuf
store i8 0, i8* @write1bit.buf
br label %27
27: ; preds = %24, %13
ret void
}

LLVM IR (-O2):
@write1bit.buf = internal unnamed_addr global i8 0
@write1bit.inbuf = internal unnamed_addr global i32 0
define void @write1bit(i32* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = load i32, i32* @write1bit.inbuf
%5 = sub nsw i32 7, %4
%6 = shl nuw i32 1, %5
%7 = trunc i32 %6 to i8
%8 = select i1 %3, i8 %7, i8 0
%9 = load i8, i8* @write1bit.buf
%10 = or i8 %8, %9
store i8 %10, i8* @write1bit.buf
%11 = add nsw i32 %4, 1
store i32 %11, i32* @write1bit.inbuf
%12 = icmp eq i32 %11, 8
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = sext i8 %10 to i32
%15 = tail call i32 @fputc(i32 %14, i32* %0)
store i32 0, i32* @write1bit.inbuf
store i8 0, i8* @write1bit.buf
br label %16
16: ; preds = %13, %2
ret void
}