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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x90(%rbp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    117e <main+0x4e>
mov    $0x2,%edi
lea    0xe8c(%rip),%rsi
mov    $0x1a,%edx
call   1030 <write@plt>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x94(%rbp),%eax
je     11b7 <main+0x87>
mov    $0x2,%edi
lea    0xe6e(%rip),%rsi
mov    $0x1b,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x80,%rsp
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1164 <main+0x34>
lea    0xeab(%rip),%rsi
mov    $0x2,%edi
mov    $0x1a,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     118d <main+0x5d>
lea    0xe9d(%rip),%rsi
mov    $0x2,%edi
mov    $0x1b,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1050 <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
store i32 0, i32* %1
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = call i32 @read(i32 0, i8* %4, i32 128)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @write(i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 26)
br label %10
10: ; preds = %8, %0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @write(i32 1, i8* %11, i32 %12)
%14 = load i32, i32* %3
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @write(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 27)
br label %18
18: ; preds = %16, %10
%19 = 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 %7
5: ; preds = %0
%6 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 26)
br label %7
7: ; preds = %5, %0
%8 = call i32 @write(i32 1, i8* %2, i32 %3)
%9 = icmp eq i32 %8, %3
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 27)
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @exit(i32 0)
unreachable
}