#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror_wrapper(char *);
int read(int, char *, int);
int /*<<< orphan*/ set_block(int);
int /*<<< orphan*/ stderr;
int read_all(int fd, void *buf, int size) {
  int got_read = 0;
  int ret;
  while (got_read < size) {
    ret = read(fd, (char *)buf + got_read, size - got_read);
    if (ret == -1 && errno == EINTR)
      continue;
    if (ret == 0) {
      errno = 0;
      fprintf(stderr, "EOF\n");
      return 0;
    }
    if (ret < 0) {
      if (errno != EAGAIN)
        perror_wrapper("read");
      return 0;
    }
    if (got_read == 0) {
      set_block(fd);
    }
    got_read += ret;
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    123f <read_all+0xff>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11aa <read_all+0x6a>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <read_all+0x6a>
jmp    1159 <read_all+0x19>
cmpl   $0x0,-0x1c(%rbp)
jne    11e3 <read_all+0xa3>
mov    0x2e1d(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1246 <read_all+0x106>
cmpl   $0x0,-0x1c(%rbp)
jge    121f <read_all+0xdf>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <read_all+0xd3>
lea    0xdf7(%rip),%rdi
call   1030 <perror_wrapper@plt>
movl   $0x0,-0x4(%rbp)
jmp    1246 <read_all+0x106>
cmpl   $0x0,-0x18(%rbp)
jne    1231 <read_all+0xf1>
mov    -0x8(%rbp),%edi
call   1060 <set_block@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1159 <read_all+0x19>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%ebx
test   %edx,%edx
jle    1225 <read_all+0xe5>
mov    %edi,%r13d
movl   $0x0,0x8(%rsp)
mov    0x2e6b(%rip),%r15
mov    0x2e5c(%rip),%r12
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11ad <read_all+0x6d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1190 <read_all+0x50>
jmp    120b <read_all+0xcb>
mov    %eax,%r14d
test   %eax,%eax
je     11e8 <read_all+0xa8>
js     1208 <read_all+0xc8>
mov    0x8(%rsp),%ebx
test   %ebx,%ebx
jne    11c6 <read_all+0x86>
mov    %r13d,%edi
call   1060 <set_block@plt>
add    %r14d,%ebx
mov    %ebx,0x8(%rsp)
movslq %ebx,%rax
mov    0x10(%rsp),%rsi
lea    (%rsi,%rax,1),%rbp
mov    0xc(%rsp),%ebx
sub    %eax,%ebx
jg     1190 <read_all+0x50>
mov    $0x1,%ebx
jmp    1225 <read_all+0xe5>
movq   $0x0,(%r15)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1050 <fprintf@plt>
xor    %ebx,%ebx
jmp    1225 <read_all+0xe5>
mov    (%r15),%rax
mov    0x2dae(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     1225 <read_all+0xe5>
lea    0xde5(%rip),%rdi
call   1030 <perror_wrapper@plt>
mov    %ebx,%eax
add    $0x18,%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 = [5 x i8] c"EOF\0A\00"
@EAGAIN = global i64 0
@.str.1 = [5 x i8] c"read\00"
define i32 @read_all(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %53, %30, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %57
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %14
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %10
31: ; preds = %26, %14
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
store i64 0, i64* @errno
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %58
37: ; preds = %31
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @perror_wrapper(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
store i32 0, i32* %4
br label %58
47: ; preds = %37
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %5
%52 = call i32 @set_block(i32 %51)
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %9
%55 = load i32, i32* %8
%56 = add nsw i32 %55, %54
store i32 %56, i32* %8
br label %10
57: ; preds = %10
store i32 1, i32* %4
br label %58
58: ; preds = %57, %46, %34
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [5 x i8] c"EOF\0A\00"
@EAGAIN = global i64 0
@.str.1 = [5 x i8] c"read\00"
define i32 @read_all(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %38
5: ; preds = %3, %32
%6 = phi i32 [ %37, %32 ], [ %2, %3 ]
%7 = phi i8* [ %36, %32 ], [ %1, %3 ]
%8 = phi i32 [ %33, %32 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %11
%10 = tail call i32 @read(i32 %0, i8* %7, i32 %6)
switch i32 %10, label %18 [
i32 -1, label %11
i32 0, label %15
]
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %9, label %22
15: ; preds = %9
store i64 0, i64* @errno
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
18: ; preds = %9
%19 = icmp slt i32 %10, 0
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = load i64, i64* @errno
br label %22
22: ; preds = %11, %20
%23 = phi i64 [ %21, %20 ], [ %12, %11 ]
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %38, label %26
26: ; preds = %22
%27 = tail call i32 @perror_wrapper(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %38
28: ; preds = %18
%29 = icmp eq i32 %8, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = tail call i32 @set_block(i32 %0)
br label %32
32: ; preds = %30, %28
%33 = add nsw i32 %10, %8
%34 = icmp slt i32 %33, %2
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %1, i64 %35
%37 = sub nsw i32 %2, %33
br i1 %34, label %5, label %38
38: ; preds = %32, %3, %22, %26, %15
%39 = phi i32 [ 0, %15 ], [ 0, %26 ], [ 0, %22 ], [ 1, %3 ], [ 1, %32 ]
ret i32 %39
}