#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int read(int, char *, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ verbose(int /*<<< orphan*/, char *, ...);
void read_all(int fd, char *buf, size_t buflen) {
  int tmp;
  while (buflen) {
    tmp = read(fd, buf, buflen);
    switch (tmp) {
    case -1:
      verbose(0, "read_all: %s\n", strerror(errno));
      exit(1);
    case 0:
      verbose(0, "read_all: EOF\n");
      exit(1);
    }
    buf += tmp;
    buflen -= tmp;
  }
}


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)
cmpq   $0x0,-0x18(%rbp)
je     11fe <read_all+0xbe>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1195 <read_all+0x55>
jmp    1185 <read_all+0x45>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11bf <read_all+0x7f>
jmp    11d9 <read_all+0x99>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edx
xor    %edi,%edi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <verbose@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %edi,%edi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1040 <verbose@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1153 <read_all+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     1180 <read_all+0x40>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
test   %eax,%eax
je     1185 <read_all+0x45>
cmp    $0xffffffff,%eax
je     119f <read_all+0x5f>
cltq
add    %rax,%rbx
sub    %rax,%r14
jne    1160 <read_all+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe82(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1040 <verbose@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe4c(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <verbose@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"read_all: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"read_all: EOF\0A\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 = %25, %3
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %34
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 = load i32, i32* %7
switch i32 %16, label %25 [
i32 -1, label %17
i32 0, label %22
]
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @verbose(i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = call i32 (i32, i8*, ...) @verbose(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %11
%26 = load i32, i32* %7
%27 = load i8*, i8** %5
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %5
%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
br label %8
34: ; preds = %8
ret void
}

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