#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, size_t);
ssize_t read_in_full(int fd, void *buf, size_t count) {
  char *p = buf;
  ssize_t ret;
  ssize_t bytes = 0;
  while (count > 0) {
    ret = read(fd, p + bytes, count);
    if (ret < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return bytes ? bytes : -1;
    } else if (ret == 0) {
      return bytes;
    }
    count -= ret;
    bytes += ret;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<read_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11fe <read_in_full+0xee>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c7 <read_in_full+0xb7>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
je     1195 <read_in_full+0x85>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <read_in_full+0x8a>
jmp    1132 <read_in_full+0x22>
cmpl   $0x0,-0x28(%rbp)
je     11af <read_in_full+0x9f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11bc <read_in_full+0xac>
mov    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bc <read_in_full+0xac>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <read_in_full+0xf4>
cmpl   $0x0,-0x24(%rbp)
jne    11dc <read_in_full+0xcc>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <read_in_full+0xf4>
jmp    11e1 <read_in_full+0xd1>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1132 <read_in_full+0x22>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     118c <read_in_full+0x7c>
mov    %rdx,%rbx
mov    %edi,%r12d
xor    %r15d,%r15d
mov    0x2eb0(%rip),%r13
mov    0x2ea1(%rip),%r14
mov    %rsi,%rbp
mov    %rsi,(%rsp)
xchg   %ax,%ax
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %eax,%eax
jns    1169 <read_in_full+0x59>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1140 <read_in_full+0x30>
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
je     1140 <read_in_full+0x30>
jmp    1181 <read_in_full+0x71>
je     118f <read_in_full+0x7f>
mov    %eax,%ecx
add    %eax,%r15d
movslq %r15d,%rbp
mov    (%rsp),%rsi
add    %rsi,%rbp
sub    %rcx,%rbx
jne    1140 <read_in_full+0x30>
jmp    118f <read_in_full+0x7f>
cmp    $0x1,%r15d
sbb    %eax,%eax
or     %eax,%r15d
jmp    118f <read_in_full+0x7f>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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