#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ error_printf(char *, char *, int /*<<< orphan*/, int);
int read(int, void *, int);
int /*<<< orphan*/ strerror(scalar_t__);
ssize_t safe_read_available(int fd, void *buf, ssize_t count, char *errstr) {
  ssize_t ret = 0;
  while ((ret = read(fd, buf, count)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      error_printf("%s (%s) (fd=%d)\r\n", errstr, strerror(errno), fd);
      break;
    }
    break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<safe_read_available>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11c7 <safe_read_available+0x97>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c2 <safe_read_available+0x92>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <safe_read_available+0x61>
jmp    114d <safe_read_available+0x1d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
mov    -0x4(%rbp),%ecx
lea    0xe48(%rip),%rdi
call   1040 <error_printf@plt>
jmp    11c7 <safe_read_available+0x97>
jmp    11c7 <safe_read_available+0x97>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read_available>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,(%rsp)
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e91(%rip),%r13
mov    0x2e82(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1030 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1198 <safe_read_available+0x68>
mov    0x0(%r13),%rdi
cmp    (%r14),%rdi
je     1160 <safe_read_available+0x30>
call   1050 <strerror@plt>
lea    0xe76(%rip),%rdi
mov    (%rsp),%rsi
mov    %eax,%edx
mov    %r15d,%ecx
call   1040 <error_printf@plt>
mov    %ebx,%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
@.str = [18 x i8] c"%s (%s) (fd=%d)\0D\0A\00"
define i32 @safe_read_available(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i32 0, i32* %9
br label %10
10: ; preds = %23, %4
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @read(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %10
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %10
24: ; preds = %19
%25 = load i8*, i8** %8
%26 = load i64, i64* @errno
%27 = call i32 @strerror(i64 %26)
%28 = load i32, i32* %5
%29 = call i32 @error_printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %27, i32 %28)
br label %31
30: ; preds = %16
br label %31
31: ; preds = %30, %24, %10
%32 = load i32, i32* %9
ret i32 %32
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"%s (%s) (fd=%d)\0D\0A\00"
define i32 @safe_read_available(i32 %0, i8* nocapture %1, i32 %2, i8* %3) {
br label %5
5: ; preds = %8, %4
%6 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %5, label %12
12: ; preds = %8
%13 = tail call i32 @strerror(i64 %9)
%14 = tail call i32 @error_printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %13, i32 %0)
br label %15
15: ; preds = %5, %12
ret i32 %6
}