#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 ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EIO;
int /*<<< orphan*/ EXIT_FAILURE;
int LINE_MAX;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
int read_out(int amaster) {
  char buf[LINE_MAX];
  ssize_t buf_got;
  buf_got = read(amaster, buf, sizeof buf);
  if (buf_got == 0)
    return 0;
  if (buf_got == -1 && errno == EIO)
    return 0;
  if (buf_got == -1 && errno == EAGAIN)
    return 0;
  if (buf_got < 0) {
    perror("read (amaster)");
    exit(EXIT_FAILURE);
  }
  if (write(STDOUT_FILENO, buf, buf_got) != buf_got) {
    perror("write(2)");
    exit(EXIT_FAILURE);
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    %eax,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a1 <read_out+0x61>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    1279 <read_out+0x139>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d8 <read_out+0x98>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <read_out+0x98>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    1279 <read_out+0x139>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120f <read_out+0xcf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <read_out+0xcf>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    1279 <read_out+0x139>
cmpl   $0x0,-0x1c(%rbp)
jge    1233 <read_out+0xf3>
lea    0xde0(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     126b <read_out+0x12b>
lea    0xdb7(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_out>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
je     11c4 <read_out+0x84>
cmp    $0xffffffff,%r14d
jne    11a0 <read_out+0x60>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
je     11c4 <read_out+0x84>
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <read_out+0x84>
jmp    11d0 <read_out+0x90>
test   %r14d,%r14d
js     11d0 <read_out+0x90>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
mov    $0x1,%r14d
jne    11d9 <read_out+0x99>
mov    %r14d,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
jmp    11e0 <read_out+0xa0>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@LINE_MAX = global i32 0
@errno = global i64 0
@EIO = global i64 0
@EAGAIN = global i64 0
@.str = [15 x i8] c"read (amaster)\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [9 x i8] c"write(2)\00"
define i32 @read_out(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @LINE_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i32, i32* %3
%13 = trunc i64 %9 to i32
%14 = call i32 @read(i32 %12, i8* %11, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %7
br label %52
18: ; preds = %1
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EIO
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
store i32 1, i32* %7
br label %52
26: ; preds = %21, %18
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %2
store i32 1, i32* %7
br label %52
34: ; preds = %29, %26
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34
%42 = load i32, i32* @STDOUT_FILENO
%43 = load i32, i32* %6
%44 = call i32 @write(i32 %42, i8* %11, i32 %43)
%45 = load i32, i32* %6
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %41
store i32 1, i32* %2
store i32 1, i32* %7
br label %52
52: ; preds = %51, %33, %25, %17
%53 = load i8*, i8** %4
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@LINE_MAX = global i32 0
@errno = global i64 0
@EIO = global i64 0
@EAGAIN = global i64 0
@.str = [15 x i8] c"read (amaster)\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [9 x i8] c"write(2)\00"
define i32 @read_out(i32 %0) {
%2 = load i32, i32* @LINE_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @read(i32 %0, i8* %4, i32 %2)
switch i32 %5, label %13 [
i32 0, label %27
i32 -1, label %6
]
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EIO
%9 = icmp eq i64 %7, %8
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %7, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %27, label %15
13: ; preds = %1
%14 = icmp slt i32 %5, 0
br i1 %14, label %15, label %19
15: ; preds = %6, %13
%16 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %13
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @write(i32 %20, i8* %4, i32 %5)
%22 = icmp eq i32 %21, %5
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %1, %19, %6
%28 = phi i32 [ %5, %1 ], [ 0, %6 ], [ 1, %19 ]
ret i32 %28
}