#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 EIO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t read(int, void *, size_t);
int /*<<< orphan*/ stderr;
int read_all(void *buf, int fd, size_t len) {
  size_t left = len;
  size_t part;
  while (left > 0) {
    part = read(fd, buf, len);
    if (part < 0) {
      fprintf(stderr, "Short read"
                      "\n");
      return -EIO;
    }
    left -= part;
  }
  return len;
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11a1 <read_all+0x81>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    118d <read_all+0x6d>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e5d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a8 <read_all+0x88>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    113b <read_all+0x1b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
test   %rdx,%rdx
je     1143 <read_all+0x33>
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rbp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
sub    %rax,%rbx
jne    1130 <read_all+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Short read\0A\00"
@EIO = global i32 0
define i32 @read_all(i8* %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
store i64 %10, i64* %8
br label %11
11: ; preds = %26, %3
%12 = load i64, i64* %8
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = call i64 @read(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp ult i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EIO
%25 = sub nsw i32 0, %24
store i32 %25, i32* %4
br label %33
26: ; preds = %14
%27 = load i64, i64* %9
%28 = load i64, i64* %8
%29 = sub i64 %28, %27
store i64 %29, i64* %8
br label %11
30: ; preds = %11
%31 = load i64, i64* %7
%32 = trunc i64 %31 to i32
store i32 %32, i32* %4
br label %33
33: ; preds = %30, %21
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@EIO = global i32 0
define i32 @read_all(i8* nocapture %0, i32 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %10, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %8, %5 ], [ %2, %3 ]
%7 = tail call i64 @read(i32 %1, i8* %0, i64 %2)
%8 = sub i64 %6, %7
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %5
10: ; preds = %5, %3
%11 = trunc i64 %2 to i32
ret i32 %11
}