#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int read(int, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int safe_read(int fd, void *data, size_t size) {
  int ret;
  unsigned char *bytes = (unsigned char *)data;
  while (size) {
    ret = read(fd, bytes, size);
    if (ret <= 0 && errno != EINTR) {
      fprintf(stderr, "%s failed: %s\n", "read", strerror(errno));
      return -1;
    }
    size -= ret;
    bytes += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f2 <safe_read+0xc2>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11cd <safe_read+0x9d>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     11cd <safe_read+0x9d>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
lea    0xe53(%rip),%rdx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <safe_read+0xc9>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    114b <safe_read+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     11b2 <safe_read+0x82>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e85(%rip),%r12
mov    0x2e76(%rip),%r13
jmp    116a <safe_read+0x3a>
nopl   0x0(%rax)
cltq
add    %rax,%rbp
sub    %rax,%rbx
je     11b2 <safe_read+0x82>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %eax,%eax
jg     1160 <safe_read+0x30>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     1160 <safe_read+0x30>
mov    0x2e53(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe65(%rip),%rsi
lea    0xe6d(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"%s failed: %s\0A\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @safe_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
store i8* %10, i8** %9
br label %11
11: ; preds = %30, %3
%12 = load i64, i64* %7
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %9
%17 = load i64, i64* %7
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %14
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %40
30: ; preds = %21, %14
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = load i64, i64* %7
%34 = sub i64 %33, %32
store i64 %34, i64* %7
%35 = load i32, i32* %8
%36 = load i8*, i8** %9
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %9
br label %11
39: ; preds = %11
store i32 0, i32* %4
br label %40
40: ; preds = %39, %25
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"%s failed: %s\0A\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @safe_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %18
%6 = phi i8* [ %21, %18 ], [ %1, %3 ]
%7 = phi i64 [ %20, %18 ], [ %2, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %18
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i8* @strerror(i64 %11)
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %23
18: ; preds = %10, %5
%19 = sext i32 %8 to i64
%20 = sub i64 %7, %19
%21 = getelementptr inbounds i8, i8* %6, i64 %19
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %5
23: ; preds = %18, %3, %14
%24 = phi i32 [ -1, %14 ], [ 0, %3 ], [ 0, %18 ]
ret i32 %24
}