#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 O_CREAT;
int O_WRONLY;
int bitio_close(struct bitio *);
struct bitio *bitio_open(char *, char);
int bitio_read(struct bitio *);
int close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int main() {
  int fd;
  int i;
  int res, bit_res;
  unsigned char buf;
  struct bitio *f;
  fd = open("C", O_WRONLY | O_CREAT, 0666);
  if (fd < 0) {
    perror("Error open()\n");
    return 1;
  }
  f = bitio_open("B", 'r');
  if (!f) {
    perror("Error bitio_open()\n");
    return 1;
  }
  do {
    buf = 0;
    for (i = 0; i < 8; i++) {
      bit_res = bitio_read(f);
      if (bit_res < 0) {
        break;
      }
      buf |= (unsigned char)(bit_res << i);
    }
    if (bit_res < 0) {
      break;
    }
    res = write(fd, &buf, 1);
    if (res < 0) {
      perror("Error write()\n");
      return 1;
    }
  } while (bit_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    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c7 <main+0x57>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <main+0x19c>
lea    0xe42(%rip),%rdi
mov    $0x72,%esi
call   1090 <bitio_open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ff <main+0x8f>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <main+0x19c>
jmp    1204 <main+0x94>
movb   $0x0,-0x15(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    1256 <main+0xe6>
mov    -0x20(%rbp),%rdi
call   1030 <bitio_read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1234 <main+0xc4>
jmp    1256 <main+0xe6>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    %cl,%eax
movzbl %al,%ecx
movzbl -0x15(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x15(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    120f <main+0x9f>
cmpl   $0x0,-0x14(%rbp)
jge    1265 <main+0xf5>
jmp    12aa <main+0x13a>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    129b <main+0x12b>
lea    0xd9c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <main+0x19c>
jmp    12a0 <main+0x130>
cmpl   $0x0,-0x14(%rbp)
jge    1204 <main+0x94>
mov    -0x20(%rbp),%rdi
call   1040 <bitio_close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12d8 <main+0x168>
lea    0xd6e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <main+0x19c>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1305 <main+0x195>
lea    0xd56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <main+0x19c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e4b(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe69(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     12d4 <main+0x164>
lea    0xe5c(%rip),%rdi
mov    $0x72,%esi
call   1090 <bitio_open@plt>
test   %rax,%rax
je     12dd <main+0x16d>
mov    %rax,%r15
mov    %r15,%rdi
call   1030 <bitio_read@plt>
mov    %eax,0x14(%rsp)
test   %eax,%eax
js     12ae <main+0x13e>
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
mov    %eax,%r14d
mov    %r15,%rdi
call   1030 <bitio_read@plt>
mov    %eax,0x10(%rsp)
test   %eax,%eax
js     12ae <main+0x13e>
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
mov    %eax,%r12d
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
mov    %eax,%r13d
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <bitio_read@plt>
test   %eax,%eax
js     12ae <main+0x13e>
add    %r14b,%r14b
mov    0x14(%rsp),%ecx
or     %r14b,%cl
mov    0x10(%rsp),%edx
shl    $0x2,%dl
shl    $0x3,%bl
or     %dl,%bl
or     %cl,%bl
shl    $0x4,%r12b
shl    $0x5,%r13b
or     %r12b,%r13b
shl    $0x6,%bpl
or     %r13b,%bpl
or     %bl,%bpl
shl    $0x7,%al
or     %bpl,%al
mov    %al,0xb(%rsp)
mov    0xc(%rsp),%edi
lea    0xb(%rsp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
test   %eax,%eax
jns    11ca <main+0x5a>
lea    0xd7a(%rip),%rdi
jmp    12ed <main+0x17d>
mov    %r15,%rdi
call   1040 <bitio_close@plt>
test   %eax,%eax
js     12e6 <main+0x176>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    12f7 <main+0x187>
lea    0xd78(%rip),%rdi
jmp    12ed <main+0x17d>
lea    0xd27(%rip),%rdi
jmp    12ed <main+0x17d>
lea    0xd2e(%rip),%rdi
jmp    12ed <main+0x17d>
lea    0xd48(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.bitio = type { i32 }
@.str = [2 x i8] c"C\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [14 x i8] c"Error open()\0A\00"
@.str.2 = [2 x i8] c"B\00"
@.str.3 = [20 x i8] c"Error bitio_open()\0A\00"
@.str.4 = [15 x i8] c"Error 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_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %65, label %7
7: ; preds = %0
%8 = tail call %struct.bitio* @bitio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 signext 114)
%9 = icmp eq %struct.bitio* %8, null
br i1 %9, label %65, label %10
10: ; preds = %7, %34
%11 = tail call i32 @bitio_read(%struct.bitio* %8)
%12 = icmp slt i32 %11, 0
br i1 %12, label %59, label %13
13: ; preds = %10
%14 = tail call i32 @bitio_read(%struct.bitio* %8)
%15 = icmp slt i32 %14, 0
br i1 %15, label %59, label %16
16: ; preds = %13
%17 = tail call i32 @bitio_read(%struct.bitio* %8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %59, label %19
19: ; preds = %16
%20 = tail call i32 @bitio_read(%struct.bitio* %8)
%21 = icmp slt i32 %20, 0
br i1 %21, label %59, label %22
22: ; preds = %19
%23 = tail call i32 @bitio_read(%struct.bitio* %8)
%24 = icmp slt i32 %23, 0
br i1 %24, label %59, label %25
25: ; preds = %22
%26 = tail call i32 @bitio_read(%struct.bitio* %8)
%27 = icmp slt i32 %26, 0
br i1 %27, label %59, label %28
28: ; preds = %25
%29 = tail call i32 @bitio_read(%struct.bitio* %8)
%30 = icmp slt i32 %29, 0
br i1 %30, label %59, label %31
31: ; preds = %28
%32 = tail call i32 @bitio_read(%struct.bitio* %8)
%33 = icmp slt i32 %32, 0
br i1 %33, label %59, label %34
34: ; preds = %31
%35 = trunc i32 %14 to i8
%36 = shl i8 %35, 1
%37 = trunc i32 %11 to i8
%38 = or i8 %36, %37
%39 = trunc i32 %17 to i8
%40 = shl i8 %39, 2
%41 = or i8 %38, %40
%42 = trunc i32 %20 to i8
%43 = shl i8 %42, 3
%44 = or i8 %41, %43
%45 = trunc i32 %23 to i8
%46 = shl i8 %45, 4
%47 = or i8 %44, %46
%48 = trunc i32 %26 to i8
%49 = shl i8 %48, 5
%50 = or i8 %47, %49
%51 = trunc i32 %29 to i8
%52 = shl i8 %51, 6
%53 = or i8 %50, %52
%54 = trunc i32 %32 to i8
%55 = shl i8 %54, 7
%56 = or i8 %53, %55
store i8 %56, i8* %1
%57 = call i32 @write(i32 %5, i8* %1, i32 1)
%58 = icmp slt i32 %57, 0
br i1 %58, label %65, label %10
59: ; preds = %31, %28, %25, %22, %19, %16, %13, %10
%60 = tail call i32 @bitio_close(%struct.bitio* %8)
%61 = icmp slt i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = tail call i32 @close(i32 %5)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %34, %62, %59, %7, %0
%66 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %7 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), %59 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), %62 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), %34 ]
%67 = tail call i32 @perror(i8* %66)
br label %68
68: ; preds = %65, %62
%69 = phi i32 [ 0, %62 ], [ 1, %65 ]
ret i32 %69
}