#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;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *);
int read(int, unsigned char *, size_t);
void readall(int fd, void *buf, size_t len) {
  unsigned char *p;
  ssize_t n;
  p = buf;
  while (len > 0) {
    n = read(fd, p, len);
    if (n == -1) {
      if (errno != EINTR)
        err(1, "read");
      continue;
    } else if (n == 0) {
      errx(1, "read: end of file");
    }
    len -= n;
    p += n;
  }
}


x86 assembly (clang, -O0):
<readall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11e8 <readall+0xb8>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a3 <readall+0x73>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
je     119e <readall+0x6e>
mov    $0x1,%edi
lea    0xe67(%rip),%rsi
call   1050 <err@plt>
jmp    114b <readall+0x1b>
cmpl   $0x0,-0x24(%rbp)
jne    11be <readall+0x8e>
mov    $0x1,%edi
lea    0xe4c(%rip),%rsi
call   1030 <errx@plt>
jmp    11c3 <readall+0x93>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    114b <readall+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11b8 <readall+0x88>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r13d
mov    0x2e90(%rip),%r12
mov    0x2e81(%rip),%r15
jmp    1170 <readall+0x40>
movslq %ebp,%rax
add    %rax,%rbx
sub    %rax,%r14
je     11b8 <readall+0x88>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    119f <readall+0x6f>
mov    (%r12),%rax
cmp    (%r15),%rax
je     1170 <readall+0x40>
mov    $0x1,%edi
lea    0xe68(%rip),%rsi
call   1050 <err@plt>
jmp    1170 <readall+0x40>
mov    %eax,%ebp
test   %eax,%eax
jne    1159 <readall+0x29>
mov    $0x1,%edi
lea    0xe54(%rip),%rsi
call   1030 <errx@plt>
jmp    1159 <readall+0x29>
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 = [5 x i8] c"read\00"
@.str.1 = [18 x i8] c"read: end of file\00"
define void @readall(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
br label %10
10: ; preds = %33, %26, %3
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %7
%16 = load i64, i64* %6
%17 = call i32 @read(i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
br label %10
27: ; preds = %13
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = load i64, i64* %6
%37 = sub i64 %36, %35
store i64 %37, i64* %6
%38 = load i32, i32* %8
%39 = load i8*, i8** %7
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %7
br label %10
42: ; preds = %10
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [18 x i8] c"read: end of file\00"
define void @readall(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 = %16, %5
%9 = tail call i32 @read(i32 %0, i8* %7, i64 %6)
switch i32 %9, label %19 [
i32 -1, label %10
i32 0, label %17
]
10: ; preds = %8
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
br label %8
17: ; preds = %8
%18 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %8, %17
%20 = sext i32 %9 to i64
%21 = sub i64 %6, %20
%22 = getelementptr inbounds i8, i8* %7, i64 %20
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %5
24: ; preds = %19, %3
ret void
}