#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
scalar_t__ write(int, char *, int);
int read_data(int fd) {
  char buf[1024];
  int len;
  do {
    len = read(fd, buf, 1024);
    if (0 >= len) {
      if (0 == len) {
        printf("connection closed by peer\n");
      } else {
        if (EAGAIN == errno)
          return 0;
        perror("read");
      }
      return -1;
    }
    if (0 > write(1, buf, len)) {
      perror("write");
    }
  } while (1);
  return 0;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
xor    %eax,%eax
cmp    -0x414(%rbp),%eax
jl     11d3 <read_data+0x93>
xor    %eax,%eax
cmp    -0x414(%rbp),%eax
jne    1195 <read_data+0x55>
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
jmp    11c7 <read_data+0x87>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <read_data+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    121f <read_data+0xdf>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <read_data+0xdf>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1204 <read_data+0xc4>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
jmp    1209 <read_data+0xc9>
mov    $0x1,%al
test   $0x1,%al
jne    114e <read_data+0xe>
jmp    1218 <read_data+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11a1 <read_data+0x61>
mov    %rsp,%rbx
lea    0xe9d(%rip),%r14
jmp    1183 <read_data+0x43>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11a1 <read_data+0x61>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    1170 <read_data+0x30>
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    1170 <read_data+0x30>
test   %eax,%eax
je     11cb <read_data+0x8b>
mov    0x2e24(%rip),%rax
mov    (%rax),%rcx
mov    0x2e2a(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     11dc <read_data+0x9c>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
jmp    11d7 <read_data+0x97>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"connection closed by peer\0A\00"
@EAGAIN = global i64 0
@errno = global i64 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @read_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %33, %1
%7 = load i32, i32* %3
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1024)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp sge i32 0, %10
br i1 %11, label %12, label %25
12: ; preds = %6
%13 = load i32, i32* %5
%14 = icmp eq i32 0, %13
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %24
17: ; preds = %12
%18 = load i64, i64* @EAGAIN
%19 = load i64, i64* @errno
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 0, i32* %2
br label %35
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
store i32 -1, i32* %2
br label %35
25: ; preds = %6
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = load i32, i32* %5
%28 = call i64 @write(i32 1, i8* %26, i32 %27)
%29 = icmp sgt i64 0, %28
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
br label %33
33: ; preds = %32
br i1 true, label %6, label %34
34: ; preds = %33
store i32 0, i32* %2
br label %35
35: ; preds = %34, %24, %21
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@EAGAIN = global i64 0
@errno = global i64 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
@str = [26 x i8] c"connection closed by peer\00"
define i32 @read_data(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 1024)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %17
6: ; preds = %23, %1
%7 = phi i32 [ %4, %1 ], [ %24, %23 ]
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %26
11: ; preds = %6
%12 = load i64, i64* @EAGAIN
%13 = load i64, i64* @errno
%14 = icmp eq i64 %12, %13
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
17: ; preds = %1, %23
%18 = phi i32 [ %24, %23 ], [ %4, %1 ]
%19 = call i64 @write(i32 1, i8* %3, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %17, %21
%24 = call i32 @read(i32 %0, i8* %3, i32 1024)
%25 = icmp slt i32 %24, 1
br i1 %25, label %6, label %17
26: ; preds = %9, %15, %11
%27 = phi i32 [ 0, %11 ], [ -1, %15 ], [ -1, %9 ]
ret i32 %27
}