#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 */

/* Variables and functions */
int O_APPEND;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int read(int, char *, int);
int strlen(char *);
int write(int, char *, int);
int main(void) {
  char buffer[128];
  int fdopen1, fdopen2, bytes_read;
  char *read_err_msg = "A read error has occurred\n";
  char *write_err_msg = "A write error has occurred\n";
  char *some_line = "Some line with no new line character";
  char *inputfile = "input_file";
  fdopen1 = open(inputfile, O_RDONLY);
  bytes_read = read(fdopen1, buffer, 128);
  if (bytes_read == -1)
    write(2, read_err_msg, strlen(read_err_msg));
  else
    buffer[bytes_read] = '\0';
  if ((write(1, buffer, bytes_read)) != bytes_read)
    write(2, write_err_msg, strlen(write_err_msg));
  fdopen2 = open("readwritefile.txt", O_RDWR | O_APPEND);
  if ((write(fdopen2, buffer, strlen(buffer))) != strlen(buffer))
    write(2, "A write error has occurred on file descriptor fdopen2\n", 46);
  if ((write(fdopen2, some_line, strlen(some_line))) != strlen(some_line))
    write(2, "A write error has occurred on file descriptor fdopen2\n", 46);
  close(fdopen1);
  close(fdopen2);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xe94(%rip),%rax
mov    %rax,-0xb0(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0xb8(%rbp)
lea    0xeb9(%rip),%rax
mov    %rax,-0xc0(%rbp)
mov    -0xc0(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0xffffffff,-0x9c(%rbp)
jne    1221 <main+0xc1>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0xa8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
jmp    1230 <main+0xd0>
movslq -0x9c(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
mov    -0x9c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x9c(%rbp),%eax
je     1280 <main+0x120>
mov    -0xb0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0xb0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x2d38(%rip),%rax
or     (%rax),%esi
lea    0xdce(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x98(%rbp)
mov    -0x98(%rbp),%eax
mov    %eax,-0xe4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xd4(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0xd4(%rbp),%eax
cmp    %ecx,%eax
je     1316 <main+0x1b6>
mov    $0x2,%edi
lea    0xd6d(%rip),%rsi
mov    $0x2e,%edx
call   1030 <write@plt>
mov    -0x98(%rbp),%eax
mov    %eax,-0xf4(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,-0xf0(%rbp)
mov    -0xb8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xf4(%rbp),%edi
mov    -0xf0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xe8(%rbp)
mov    -0xb8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0xe8(%rbp),%eax
cmp    %ecx,%eax
je     1388 <main+0x228>
mov    $0x2,%edi
lea    0xcfb(%rip),%rsi
mov    $0x2e,%edx
call   1030 <write@plt>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xedf(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11bb <main+0x5b>
lea    0xe5e(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11c2 <main+0x62>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11f3 <main+0x93>
lea    0xe3f(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x2dee(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
je     1250 <main+0xf0>
lea    0xe38(%rip),%rsi
mov    $0x2,%edi
mov    $0x2e,%edx
call   1030 <write@plt>
lea    0xde0(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
je     1290 <main+0x130>
lea    0xdf8(%rip),%rsi
mov    $0x2,%edi
mov    $0x2e,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"A read error has occurred\0A\00"
@.str.1 = [28 x i8] c"A write error has occurred\0A\00"
@.str.2 = [37 x i8] c"Some line with no new line character\00"
@.str.3 = [11 x i8] c"input_file\00"
@O_RDONLY = global i32 0
@.str.4 = [18 x i8] c"readwritefile.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.5 = [55 x i8] c"A write error has occurred on file descriptor fdopen2\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 128)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %0
%19 = load i8*, i8** %6
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @write(i32 2, i8* %19, i32 %21)
br label %27
23: ; preds = %0
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %23, %18
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%29 = load i32, i32* %5
%30 = call i32 @write(i32 1, i8* %28, i32 %29)
%31 = load i32, i32* %5
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i8*, i8** %7
%35 = load i8*, i8** %7
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 2, i8* %34, i32 %36)
br label %38
38: ; preds = %33, %27
%39 = load i32, i32* @O_RDWR
%40 = load i32, i32* @O_APPEND
%41 = or i32 %39, %40
%42 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %41)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = icmp ne i32 %47, %49
br i1 %50, label %51, label %53
51: ; preds = %38
%52 = call i32 @write(i32 2, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 46)
br label %53
53: ; preds = %51, %38
%54 = load i32, i32* %4
%55 = load i8*, i8** %8
%56 = load i8*, i8** %8
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @write(i32 %54, i8* %55, i32 %57)
%59 = load i8*, i8** %8
%60 = call i32 @strlen(i8* %59)
%61 = icmp ne i32 %58, %60
br i1 %61, label %62, label %64
62: ; preds = %53
%63 = call i32 @write(i32 2, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 46)
br label %64
64: ; preds = %62, %53
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %4
%68 = call i32 @close(i32 %67)
%69 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [27 x i8] c"A read error has occurred\0A\00"
@.str.1 = [28 x i8] c"A write error has occurred\0A\00"
@.str.2 = [37 x i8] c"Some line with no new line character\00"
@.str.3 = [11 x i8] c"input_file\00"
@O_RDONLY = global i32 0
@.str.4 = [18 x i8] c"readwritefile.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.5 = [55 x i8] c"A write error has occurred on file descriptor fdopen2\0A\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%5 = call i32 @read(i32 %4, i8* %2, i32 128)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %13
10: ; preds = %0
%11 = sext i32 %5 to i64
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %10, %7
%14 = call i32 @write(i32 1, i8* %2, i32 %5)
%15 = icmp eq i32 %14, %5
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %13
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_APPEND
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = call i32 @strlen(i8* %2)
%25 = call i32 @write(i32 %23, i8* %2, i32 %24)
%26 = call i32 @strlen(i8* %2)
%27 = icmp eq i32 %25, %26
br i1 %27, label %30, label %28
28: ; preds = %19
%29 = call i32 @write(i32 2, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 46)
br label %30
30: ; preds = %28, %19
%31 = call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @write(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i32 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = call i32 @write(i32 2, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 46)
br label %37
37: ; preds = %35, %30
%38 = call i32 @close(i32 %4)
%39 = call i32 @close(i32 %23)
%40 = call i32 @exit(i32 0)
unreachable
}