#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*/ perror(char *);
int read(int, void *, size_t);
void 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;
      perror("read");
      break;
    }
    len -= ret;
    buf += ret;
  }
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%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    -0x4(%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    11ce <read_all+0xae>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a9 <read_all+0x89>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1198 <read_all+0x78>
jmp    1133 <read_all+0x13>
lea    0xe61(%rip),%rdi
call   1040 <perror@plt>
jmp    11ce <read_all+0xae>
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>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1179 <read_all+0x59>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
jmp    1159 <read_all+0x39>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1182 <read_all+0x62>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1150 <read_all+0x30>
test   %eax,%eax
je     1179 <read_all+0x59>
cltq
add    %rax,%r14
sub    %rax,%rbx
jne    1159 <read_all+0x39>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe77(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <perror@plt>

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

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