#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 *);
int /*<<< orphan*/ perror_wrapper(char *);
int read(int, char *, 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) {
      perror_wrapper("read");
      return 0;
    }
    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    1203 <read_all+0xd3>
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    119a <read_all+0x6a>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <read_all+0x6a>
jmp    1149 <read_all+0x19>
cmpl   $0x0,-0x1c(%rbp)
jne    11d3 <read_all+0xa3>
mov    0x2e2d(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <read_all+0xda>
cmpl   $0x0,-0x1c(%rbp)
jge    11f5 <read_all+0xc5>
lea    0xe21(%rip),%rdi
call   1030 <perror_wrapper@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <read_all+0xda>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <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,%eax
test   %edx,%edx
jle    11dd <read_all+0xad>
mov    %edi,%r13d
xor    %r15d,%r15d
mov    0x2e80(%rip),%r14
mov    0x2e71(%rip),%r12
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
nopl   (%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    118d <read_all+0x5d>
mov    (%r14),%rax
cmp    (%r12),%rax
je     1170 <read_all+0x40>
jmp    11cf <read_all+0x9f>
test   %eax,%eax
je     11b1 <read_all+0x81>
js     11cf <read_all+0x9f>
add    %eax,%r15d
movslq %r15d,%rax
mov    0x10(%rsp),%rsi
lea    (%rsi,%rax,1),%rbp
mov    0xc(%rsp),%ebx
sub    %eax,%ebx
jg     1170 <read_all+0x40>
mov    $0x1,%eax
jmp    11dd <read_all+0xad>
movq   $0x0,(%r14)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11db <read_all+0xab>
lea    0xe2f(%rip),%rdi
call   1030 <perror_wrapper@plt>
xor    %eax,%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"
@.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 = %42, %30, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %46
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 %47
37: ; preds = %31
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror_wrapper(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %47
42: ; preds = %37
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %10
46: ; preds = %10
store i32 1, i32* %4
br label %47
47: ; preds = %46, %40, %34
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [5 x i8] c"EOF\0A\00"
@.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 %10, label %28
5: ; preds = %23
%6 = icmp slt i32 %25, %2
%7 = sext i32 %25 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %25
br i1 %6, label %10, label %28
10: ; preds = %3, %5
%11 = phi i32 [ %9, %5 ], [ %2, %3 ]
%12 = phi i8* [ %8, %5 ], [ %1, %3 ]
%13 = phi i32 [ %25, %5 ], [ 0, %3 ]
br label %14
14: ; preds = %10, %16
%15 = tail call i32 @read(i32 %0, i8* %12, i32 %11)
switch i32 %15, label %23 [
i32 -1, label %16
i32 0, label %20
]
16: ; preds = %14
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %14, label %26
20: ; preds = %14
store i64 0, i64* @errno
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
23: ; preds = %14
%24 = icmp slt i32 %15, 0
%25 = add nsw i32 %15, %13
br i1 %24, label %26, label %5
26: ; preds = %23, %16
%27 = tail call i32 @perror_wrapper(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %5, %3, %26, %20
%29 = phi i32 [ 0, %20 ], [ 0, %26 ], [ 1, %3 ], [ 1, %5 ]
ret i32 %29
}