#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 */
typedef int /*<<< orphan*/ err_string;

/* Variables and functions */
int read(int /*<<< orphan*/, char *, int);
int write(int, char const *, int);
int test2() {
  char buffer[128];
  int nread;
  nread = read(0, buffer, 128);
  if (nread == -1) {
    const char *err_string = "A read error has occurred\n";
    int len;
    { len = (sizeof(err_string) / sizeof(err_string[0])); };
    write(2, err_string, len);
  }
  if (write(1, buffer, nread) != nread) {
    const char *err_string = "A write error has occurred\n";
    int len;
    { len = (sizeof(err_string) / sizeof(err_string[0])); };
    write(2, err_string, len);
  }
  return 0;
}


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    -0x80(%rbp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0xffffffff,-0x84(%rbp)
jne    117d <test2+0x5d>
lea    0xeab(%rip),%rax
mov    %rax,-0x90(%rbp)
movl   $0x8,-0x94(%rbp)
mov    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x2,%edi
call   1030 <write@plt>
lea    -0x80(%rbp),%rsi
mov    -0x84(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x84(%rbp),%eax
je     11cc <test2+0xac>
lea    0xe77(%rip),%rax
mov    %rax,-0xa0(%rbp)
movl   $0x8,-0xa4(%rbp)
mov    -0xa0(%rbp),%rsi
mov    -0xa4(%rbp),%edx
mov    $0x2,%edi
call   1030 <write@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test2>:
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    1154 <test2+0x34>
lea    0xebb(%rip),%rsi
mov    $0x2,%edi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     117d <test2+0x5d>
lea    0xead(%rip),%rsi
mov    $0x2,%edi
mov    $0x8,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret

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 @test2() {
%1 = alloca [128 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 128)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 8, i32* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = call i32 @write(i32 2, i8* %12, i32 %13)
br label %15
15: ; preds = %11, %0
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%17 = load i32, i32* %2
%18 = call i32 @write(i32 1, i8* %16, i32 %17)
%19 = load i32, i32* %2
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %15
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i32 8, i32* %6
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = call i32 @write(i32 2, i8* %22, i32 %23)
br label %25
25: ; preds = %21, %15
ret i32 0
}

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 @test2() {
%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 8)
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 8)
br label %12
12: ; preds = %10, %7
ret i32 0
}