#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 /*<<< orphan*/ perror(char *);
long read(int, void *, unsigned long);
unsigned long better_read(int fd, void *p, unsigned long count) {
  while (count) {
    long r = read(fd, p, count);
    if (r == -1) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("better_read()");
      break;
    }
    if (r == 0) {
      errno = EAGAIN;
      break;
    }
    count -= r;
    p = (char *)p + r;
  }
  return count;
}


x86 assembly (clang, -O0):
<better_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11eb <better_read+0xcb>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11a7 <better_read+0x87>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
je     1191 <better_read+0x71>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <better_read+0x76>
jmp    1133 <better_read+0x13>
lea    0xe63(%rip),%rdi
call   1040 <perror@plt>
jmp    11eb <better_read+0xcb>
cmpq   $0x0,-0x20(%rbp)
jne    11cb <better_read+0xab>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1d(%rip),%rax
mov    %rcx,(%rax)
jmp    11eb <better_read+0xcb>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <better_read+0x13>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<better_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1190 <better_read+0x70>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea1(%rip),%r15
mov    0x2e92(%rip),%r13
mov    0x2e7b(%rip),%r12
jmp    1160 <better_read+0x40>
nop
test   %rax,%rax
je     1194 <better_read+0x74>
sub    %rax,%rbx
add    %rax,%r14
test   %rbx,%rbx
je     1190 <better_read+0x70>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
jne    1150 <better_read+0x30>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
je     115b <better_read+0x3b>
cmp    (%r12),%rax
je     115b <better_read+0x3b>
lea    0xe77(%rip),%rdi
call   1040 <perror@plt>
jmp    119b <better_read+0x7b>
xor    %ebx,%ebx
jmp    119b <better_read+0x7b>
mov    (%r12),%rax
mov    %rax,(%r15)
mov    %rbx,%rax
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
@.str = [14 x i8] c"better_read()\00"
define i64 @better_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %34, %26, %3
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @read(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %29
18: ; preds = %11
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22, %18
br label %8
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %41
29: ; preds = %11
%30 = load i64, i64* %7
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i64, i64* @EAGAIN
store i64 %33, i64* @errno
br label %41
34: ; preds = %29
%35 = load i64, i64* %7
%36 = load i64, i64* %6
%37 = sub i64 %36, %35
store i64 %37, i64* %6
%38 = load i8*, i8** %5
%39 = load i64, i64* %7
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %5
br label %8
41: ; preds = %32, %27, %8
%42 = load i64, i64* %6
ret i64 %42
}

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