#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, size_t);
ssize_t read_all(int fd, void *buf, size_t count) {
  printf("reading %d from %d\n", (int)count, fd);
  while (read(fd, buf, count) == -1) {
    if (errno == EINTR) {
      continue;
    } else {
      printf("read_all FAiLED\n");
      return -1;
    }
  }
  return count;
}


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)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    119c <read_all+0x7c>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
jne    1182 <read_all+0x62>
jmp    114a <read_all+0x2a>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <read_all+0x83>
mov    -0x18(%rbp),%rax
mov    %eax,-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   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb9(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e86(%rip),%r15
mov    0x2e77(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    119d <read_all+0x6d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1170 <read_all+0x40>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"reading %d from %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [17 x i8] c"read_all FAiLED\0A\00"
define i32 @read_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = trunc i64 %8 to i32
%10 = load i32, i32* %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
br label %12
12: ; preds = %22, %3
%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)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %12
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %28
25: ; preds = %12
%26 = load i64, i64* %7
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
br label %28
28: ; preds = %25, %23
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@.str = [20 x i8] c"reading %d from %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@str = [16 x i8] c"read_all FAiLED\00"
define i32 @read_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %0)
br label %6
6: ; preds = %9, %3
%7 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
br i1 %12, label %6, label %13
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %6, %13
%16 = phi i32 [ -1, %13 ], [ %4, %6 ]
ret i32 %16
}