#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 EINTR;
int EPIPE;
int errno;
int read(int, void *, size_t);
int full_read(int fd, void *buf, size_t count) {
  size_t off = 0;
  ssize_t rc = 0;
  while (off < count) {
    rc = read(fd, buf + off, count - off);
    if (rc == 0)
      return -EPIPE;
    if (rc == -1) {
      rc = -errno;
      if (rc == -EINTR)
        continue;
      break;
    }
    off += rc;
    rc = 0;
  }
  return rc;
}


x86 assembly (clang, -O0):
<full_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c8 <full_read+0xb8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1178 <full_read+0x68>
xor    %eax,%eax
mov    0x2e72(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <full_read+0xbe>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b0 <full_read+0xa0>
xor    %eax,%eax
mov    0x2e4d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e34(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11ab <full_read+0x9b>
jmp    1132 <full_read+0x22>
jmp    11c8 <full_read+0xb8>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    1132 <full_read+0x22>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     118c <full_read+0x7c>
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2ea8(%rip),%r14
mov    0x2e99(%rip),%r15
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
lea    (%rsi,%r13,1),%rbx
mov    %rdx,%rbp
sub    %r13,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1170 <full_read+0x60>
mov    (%r14),%eax
cmp    %eax,(%r15)
je     1150 <full_read+0x40>
jmp    119d <full_read+0x8d>
nopl   (%rax)
test   %eax,%eax
je     1190 <full_read+0x80>
cltq
add    %rax,%r13
xor    %eax,%eax
mov    0x10(%rsp),%rdx
cmp    %rdx,%r13
mov    0x8(%rsp),%rsi
jb     1141 <full_read+0x31>
jmp    119f <full_read+0x8f>
xor    %eax,%eax
jmp    119f <full_read+0x8f>
mov    0x2e49(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    119f <full_read+0x8f>
neg    %eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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