#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 */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int read(int, char *, size_t);
int /*<<< orphan*/ warnx(char *);
int read_all(int fd, char *str, size_t size) {
  while (size > 0) {
    ssize_t n = read(fd, str, size);
    if (n == -1 && errno == -EINTR)
      continue;
    if (n == -1)
      return -1;
    if (n == 0)
      break;
    assert(n <= size);
    size -= n;
    str += n;
  }
  if (size > 0) {
    warnx("read less bytes than expected");
    return -1;
  }
  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)
cmpq   $0x0,-0x18(%rbp)
jbe    11eb <read_all+0xbb>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118c <read_all+0x5c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2e5b(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    118c <read_all+0x5c>
jmp    1143 <read_all+0x13>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a2 <read_all+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <read_all+0xe5>
cmpl   $0x0,-0x1c(%rbp)
jne    11b1 <read_all+0x81>
jmp    11eb <read_all+0xbb>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
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    1143 <read_all+0x13>
cmpq   $0x0,-0x18(%rbp)
jbe    120e <read_all+0xde>
lea    0xe03(%rip),%rdi
call   1030 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <read_all+0xe5>
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    %eax,%eax
test   %rdx,%rdx
je     11b4 <read_all+0x84>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e8e(%rip),%r12
mov    0x2e7f(%rip),%r13
jmp    116f <read_all+0x3f>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%eax
add    0x0(%r13),%eax
jne    11af <read_all+0x7f>
test   %rbx,%rbx
je     119f <read_all+0x6f>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1160 <read_all+0x30>
test   %eax,%eax
je     11a3 <read_all+0x73>
movslq %eax,%r14
xor    %edi,%edi
sub    %r14,%rbx
setae  %dil
call   1050 <assert@plt>
add    %r14,%rbp
test   %rbx,%rbx
jne    116f <read_all+0x3f>
xor    %eax,%eax
jmp    11b4 <read_all+0x84>
lea    0xe56(%rip),%rdi
call   1030 <warnx@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@.str = [30 x i8] c"read less bytes than expected\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 = %33, %24, %3
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %48
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 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i32, i32* @errno
%21 = load i32, i32* @EINTR
%22 = sub nsw i32 0, %21
%23 = icmp eq i32 %20, %22
br i1 %23, label %24, label %25
24: ; preds = %19
br label %9
25: ; preds = %19, %12
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 -1, i32* %4
br label %54
29: ; preds = %25
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %48
33: ; preds = %29
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = load i64, i64* %7
%37 = icmp ule i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = load i64, i64* %7
%43 = sub i64 %42, %41
store i64 %43, i64* %7
%44 = load i32, i32* %8
%45 = load i8*, i8** %6
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8* %47, i8** %6
br label %9
48: ; preds = %32, %9
%49 = load i64, i64* %7
%50 = icmp ugt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %54
53: ; preds = %48
store i32 0, i32* %4
br label %54
54: ; preds = %53, %51, %28
%55 = load i32, i32* %4
ret i32 %55
}

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