#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, void *, size_t);
int read_all(int fd, void *buf, size_t len) {
  ssize_t ret;
  while (len != 0 && (ret = read(fd, buf, len)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN)
        perror("read");
      return -1;
      perror("read");
      break;
    }
    len -= ret;
    buf += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
je     115f <read_all+0x3f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    116f <read_all+0x4f>
jmp    11ef <read_all+0xcf>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ca <read_all+0xaa>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1198 <read_all+0x78>
jmp    1133 <read_all+0x13>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <read_all+0x9e>
lea    0xe47(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <read_all+0xd6>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <read_all+0x13>
movl   $0x0,-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
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     1196 <read_all+0x76>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e9d(%rip),%r12
mov    0x2e8e(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    116f <read_all+0x4f>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1150 <read_all+0x30>
jmp    1184 <read_all+0x64>
test   %eax,%eax
je     117f <read_all+0x5f>
cltq
add    %rax,%rbp
sub    %rax,%rbx
jne    1150 <read_all+0x30>
jmp    1196 <read_all+0x76>
mov    %eax,%r14d
jmp    1196 <read_all+0x76>
mov    0x2e3d(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
je     11a8 <read_all+0x88>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe51(%rip),%rdi
call   1040 <perror@plt>
jmp    1196 <read_all+0x76>

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

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