#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 */
struct bitio {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int bitio_close(struct bitio *);
struct bitio *bitio_open(char *, char);
int bitio_write(struct bitio *, int);
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int main() {
  int fd;
  int i;
  int res, bit_res;
  int bit;
  unsigned char buff;
  struct bitio *f;
  fd = open("test", O_RDONLY);
  if (fd < 0) {
    return 1;
  }
  f = bitio_open("B", 'w');
  if (!f) {
    perror("Error bitio_open()\n");
    return 1;
  }
  do {
    res = read(fd, &buff, 1);
    if (res < 0) {
      perror("Error read()\n");
      return 1;
    }
    if (res == 0) {
      break;
    }
    for (i = 0; i < 8; i++) {
      bit = (int)(buff & ((unsigned char)1 << i));
      bit_res = bitio_write(f, bit);
      if (bit_res < 0) {
        perror("Error bitio_write()\n");
      }
    }
  } while (res != 0);
  res = bitio_close(f);
  if (res < 0) {
    perror("Error bitio_close()\n");
    return 1;
  }
  res = close(fd);
  if (res < 0) {
    perror("Error close()\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ad <main+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
lea    0xe51(%rip),%rdi
mov    $0x77,%esi
call   1090 <bitio_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <main+0x75>
lea    0xe33(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
jmp    11ea <main+0x7a>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1220 <main+0xb0>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
cmpl   $0x0,-0x10(%rbp)
jne    122f <main+0xbf>
jmp    129c <main+0x12c>
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    128d <main+0x11d>
movzbl -0x19(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <bitio_write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    127a <main+0x10a>
lea    0xdb4(%rip),%rdi
call   1080 <perror@plt>
jmp    127f <main+0x10f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1236 <main+0xc6>
jmp    1292 <main+0x122>
cmpl   $0x0,-0x10(%rbp)
jne    11ea <main+0x7a>
mov    -0x28(%rbp),%rdi
call   1030 <bitio_close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12ca <main+0x15a>
lea    0xd85(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12f7 <main+0x187>
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r13d
mov    $0x1,%eax
test   %r13d,%r13d
js     1351 <main+0x1e1>
lea    0xe5d(%rip),%rdi
mov    $0x77,%esi
call   1090 <bitio_open@plt>
test   %rax,%rax
je     1334 <main+0x1c4>
mov    %rax,%rbx
lea    0x7(%rsp),%rsi
mov    %r13d,%edi
mov    $0x1,%edx
call   1050 <read@plt>
lea    0xe44(%rip),%r14
test   %eax,%eax
js     1344 <main+0x1d4>
lea    0xe43(%rip),%r15
lea    0x7(%rsp),%r12
jmp    1208 <main+0x98>
nopl   (%rax)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1344 <main+0x1d4>
test   %eax,%eax
je     130f <main+0x19f>
movzbl 0x7(%rsp),%ebp
mov    %ebp,%esi
and    $0x1,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12ba <main+0x14a>
mov    %ebp,%esi
and    $0x2,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12c7 <main+0x157>
mov    %ebp,%esi
and    $0x4,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12d4 <main+0x164>
mov    %ebp,%esi
and    $0x8,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12e1 <main+0x171>
mov    %ebp,%esi
and    $0x10,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12ee <main+0x17e>
mov    %ebp,%esi
and    $0x20,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     12fb <main+0x18b>
mov    %ebp,%esi
and    $0x40,%esi
mov    %rbx,%rdi
call   1060 <bitio_write@plt>
test   %eax,%eax
js     1305 <main+0x195>
and    $0xffffff80,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <bitio_write@plt>
test   %eax,%eax
jns    11f0 <main+0x80>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    11f0 <main+0x80>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    122a <main+0xba>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    123f <main+0xcf>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1254 <main+0xe4>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1265 <main+0xf5>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1276 <main+0x106>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1287 <main+0x117>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1298 <main+0x128>
mov    %rbx,%rdi
call   1030 <bitio_close@plt>
test   %eax,%eax
js     133d <main+0x1cd>
mov    %r13d,%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    1351 <main+0x1e1>
lea    0xd21(%rip),%r14
jmp    1344 <main+0x1d4>
lea    0xccc(%rip),%r14
jmp    1344 <main+0x1d4>
lea    0xcfa(%rip),%r14
mov    %r14,%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitio = type { i32 }
@.str = [5 x i8] c"test\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"B\00"
@.str.2 = [20 x i8] c"Error bitio_open()\0A\00"
@.str.3 = [14 x i8] c"Error read()\0A\00"
@.str.4 = [21 x i8] c"Error bitio_write()\0A\00"
@.str.5 = [21 x i8] c"Error bitio_close()\0A\00"
@.str.6 = [15 x i8] c"Error close()\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca %struct.bitio*
store i32 0, i32* %1
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 1, i32* %1
br label %72
14: ; preds = %0
%15 = call %struct.bitio* @bitio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
store %struct.bitio* %15, %struct.bitio** %8
%16 = load %struct.bitio*, %struct.bitio** %8
%17 = icmp ne %struct.bitio* %16, null
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %72
20: ; preds = %14
br label %21
21: ; preds = %54, %20
%22 = load i32, i32* %2
%23 = call i32 @read(i32 %22, i8* %7, i32 1)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %72
28: ; preds = %21
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %57
32: ; preds = %28
store i32 0, i32* %3
br label %33
33: ; preds = %50, %32
%34 = load i32, i32* %3
%35 = icmp slt i32 %34, 8
br i1 %35, label %36, label %53
36: ; preds = %33
%37 = load i8, i8* %7
%38 = zext i8 %37 to i32
%39 = load i32, i32* %3
%40 = shl i32 1, %39
%41 = and i32 %38, %40
store i32 %41, i32* %6
%42 = load %struct.bitio*, %struct.bitio** %8
%43 = load i32, i32* %6
%44 = call i32 @bitio_write(%struct.bitio* %42, i32 %43)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %36
%48 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %36
br label %50
50: ; preds = %49
%51 = load i32, i32* %3
%52 = add nsw i32 %51, 1
store i32 %52, i32* %3
br label %33
53: ; preds = %33
br label %54
54: ; preds = %53
%55 = load i32, i32* %4
%56 = icmp ne i32 %55, 0
br i1 %56, label %21, label %57
57: ; preds = %54, %31
%58 = load %struct.bitio*, %struct.bitio** %8
%59 = call i32 @bitio_close(%struct.bitio* %58)
store i32 %59, i32* %4
%60 = load i32, i32* %4
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %72
64: ; preds = %57
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
store i32 %66, i32* %4
%67 = load i32, i32* %4
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %72
71: ; preds = %64
store i32 0, i32* %1
br label %72
72: ; preds = %71, %69, %62, %26, %18, %13
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
%struct.bitio = type { i32 }
@.str = [5 x i8] c"test\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"B\00"
@.str.2 = [20 x i8] c"Error bitio_open()\0A\00"
@.str.3 = [14 x i8] c"Error read()\0A\00"
@.str.4 = [21 x i8] c"Error bitio_write()\0A\00"
@.str.5 = [21 x i8] c"Error bitio_close()\0A\00"
@.str.6 = [15 x i8] c"Error close()\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %76, label %5
5: ; preds = %0
%6 = tail call %struct.bitio* @bitio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
%7 = icmp eq %struct.bitio* %6, null
br i1 %7, label %73, label %8
8: ; preds = %5
%9 = call i32 @read(i32 %3, i8* %1, i32 1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %73, label %11
11: ; preds = %8, %64
%12 = phi i32 [ %65, %64 ], [ %9, %8 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %67, label %14
14: ; preds = %11
%15 = load i8, i8* %1
%16 = zext i8 %15 to i32
%17 = and i32 %16, 1
%18 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %14, %20
%23 = and i32 %16, 2
%24 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = and i32 %16, 4
%30 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = and i32 %16, 8
%36 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = and i32 %16, 16
%42 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = and i32 %16, 32
%48 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %47)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = and i32 %16, 64
%54 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %53)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = and i32 %16, 128
%60 = tail call i32 @bitio_write(%struct.bitio* %6, i32 %59)
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
%65 = call i32 @read(i32 %3, i8* %1, i32 1)
%66 = icmp slt i32 %65, 0
br i1 %66, label %73, label %11
67: ; preds = %11
%68 = tail call i32 @bitio_close(%struct.bitio* %6)
%69 = icmp slt i32 %68, 0
br i1 %69, label %73, label %70
70: ; preds = %67
%71 = tail call i32 @close(i32 %3)
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %64, %70, %67, %8, %5
%74 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %5 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %8 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), %67 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), %70 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %64 ]
%75 = tail call i32 @perror(i8* %74)
br label %76
76: ; preds = %73, %70, %0
%77 = phi i32 [ 1, %0 ], [ 0, %70 ], [ 1, %73 ]
ret i32 %77
}