#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int read(int /*<<< orphan*/, char *, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  char buffer[128];
  int nread;
  char *read_err_msg = "A read error has occurred\n";
  char *write_err_msg = "A write error has occurred\n";
  nread = read(0, buffer, 128);
  if (nread == -1)
    write(2, read_err_msg, strlen(read_err_msg));
  if ((write(1, buffer, nread)) != nread)
    write(2, write_err_msg, strlen(write_err_msg));
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    0xeb4(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x90(%rbp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    11c1 <main+0x81>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x94(%rbp),%eax
je     1211 <main+0xd1>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xa8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    117e <main+0x3e>
lea    0xe99(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11af <main+0x6f>
lea    0xe83(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1060 <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"
define i32 @main() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = call i32 @read(i32 0, i8* %6, i32 128)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i8*, i8** %4
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @write(i32 2, i8* %11, i32 %13)
br label %15
15: ; preds = %10, %0
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @write(i32 1, i8* %16, i32 %17)
%19 = load i32, i32* %3
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @write(i32 2, i8* %22, i32 %24)
br label %26
26: ; preds = %21, %15
%27 = 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"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = call i32 @read(i32 0, i8* %2, i32 128)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %5, %0
%9 = call i32 @write(i32 1, i8* %2, i32 %3)
%10 = icmp eq i32 %9, %3
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %8
%15 = tail call i32 @exit(i32 0)
unreachable
}