#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ die(char *);
scalar_t__ errno;
int read(int, unsigned char *, scalar_t__);
void xreadfully(int fd, unsigned char *buf, ssize_t len) {
  int tl = 0;
  if (len <= 0)
    return;
  while (true) {
    int l = read(fd, buf, len);
    if (l == len)
      return;
    if (l <= 0) {
      if (errno == EINTR)
        continue;
      else
        die("read() failed:");
    }
    tl += l;
    buf += l;
    len -= l;
  }
}


x86 assembly (clang, -O0):
<xreadfully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     114a <xreadfully+0x2a>
jmp    11dd <xreadfully+0xbd>
jmp    114f <xreadfully+0x2f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1175 <xreadfully+0x55>
jmp    11dd <xreadfully+0xbd>
cmpl   $0x0,-0x20(%rbp)
jg     11af <xreadfully+0x8f>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    119e <xreadfully+0x7e>
jmp    114f <xreadfully+0x2f>
lea    0xe5b(%rip),%rdi
call   1040 <die@plt>
jmp    11af <xreadfully+0x8f>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114f <xreadfully+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xreadfully>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
jle    1145 <xreadfully+0x25>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <read@plt>
movslq %eax,%r15
cmp    %rbx,%r15
jne    1154 <xreadfully+0x34>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e85(%rip),%r12
mov    0x2e76(%rip),%r13
jmp    118b <xreadfully+0x6b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %r15,%r14
sub    %r15,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
movslq %eax,%r15
cmp    %r15,%rbx
je     1145 <xreadfully+0x25>
test   %eax,%eax
jg     1170 <xreadfully+0x50>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1176 <xreadfully+0x56>
lea    0xe60(%rip),%rdi
call   1040 <die@plt>
jmp    1170 <xreadfully+0x50>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"read() failed:\00"
define void @xreadfully(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
%9 = load i64, i64* %6
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
br label %46
12: ; preds = %3
br label %13
13: ; preds = %12, %30, %34
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%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 = sext i32 %18 to i64
%20 = load i64, i64* %6
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %13
br label %46
23: ; preds = %13
%24 = load i32, i32* %8
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %13
31: ; preds = %26
%32 = call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31
br label %34
34: ; preds = %33, %23
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = add nsw i32 %36, %35
store i32 %37, i32* %7
%38 = load i32, i32* %8
%39 = load i8*, i8** %5
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %5
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = load i64, i64* %6
%45 = sub nsw i64 %44, %43
store i64 %45, i64* %6
br label %13
46: ; preds = %22, %11
ret void
}

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