#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, unsigned char *, int);
int fullRead(int fd, unsigned char *buf, int len) {
  int total;
  int thistime;
  for (total = 0; total < len;) {
    thistime = read(fd, buf + total, len - total);
    if (thistime < 0) {
      if (EINTR == errno || EAGAIN == errno)
        continue;
      return -1;
    } else if (thistime == 0) {
      return total;
    }
    total += thistime;
  }
  return total;
}


x86 assembly (clang, -O0):
<fullRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c8 <fullRead+0xb8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a0 <fullRead+0x90>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
je     118f <fullRead+0x7f>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <fullRead+0x84>
jmp    1129 <fullRead+0x19>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <fullRead+0xbe>
cmpl   $0x0,-0x1c(%rbp)
jne    11b5 <fullRead+0xa5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <fullRead+0xbe>
jmp    11ba <fullRead+0xaa>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <fullRead+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fullRead>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
xor    %ecx,%ecx
mov    0x2eae(%rip),%r14
mov    0x2eaf(%rip),%r15
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %ecx,%r12d
mov    %edx,%ebx
sub    %ecx,%ebx
jle    1199 <fullRead+0x89>
movslq %r12d,%rbp
add    %rsi,%rbp
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    1180 <fullRead+0x70>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1150 <fullRead+0x40>
mov    0x2e58(%rip),%rcx
cmp    %rax,(%rcx)
je     1150 <fullRead+0x40>
jmp    1193 <fullRead+0x83>
nopw   0x0(%rax,%rax,1)
lea    (%rax,%r12,1),%ecx
test   %eax,%eax
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rsi
jne    113a <fullRead+0x2a>
jmp    1199 <fullRead+0x89>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @fullRead(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %42, %34, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %14
%27 = load i64, i64* @EINTR
%28 = load i64, i64* @errno
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @EAGAIN
%32 = load i64, i64* @errno
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
br label %10
35: ; preds = %30
store i32 -1, i32* %4
br label %48
36: ; preds = %14
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %48
41: ; preds = %36
br label %42
42: ; preds = %41
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %10
46: ; preds = %10
%47 = load i32, i32* %8
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %39, %35
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @fullRead(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %20, %3
%5 = phi i32 [ %22, %20 ], [ 0, %3 ]
%6 = icmp slt i32 %5, %2
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %5
br i1 %6, label %10, label %23
10: ; preds = %4, %13
%11 = tail call i32 @read(i32 %0, i8* %8, i32 %9)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i64, i64* @EINTR
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %17, %15
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %10, label %23
20: ; preds = %10
%21 = icmp eq i32 %11, 0
%22 = add nsw i32 %11, %5
br i1 %21, label %23, label %4
23: ; preds = %20, %4, %13
%24 = phi i32 [ -1, %13 ], [ %5, %4 ], [ %5, %20 ]
ret i32 %24
}