#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 */
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
#define EAGAIN 128
int /*<<< orphan*/ POLLIN;
int errno;
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, size_t);
int do_read(int fd, void *buf, size_t count) {
  size_t bytes_read = 0;
  struct pollfd pfd;
  int ret;
  pfd.fd = fd;
  pfd.events = POLLIN;
  do {
    ret = poll(&pfd, 1, 500);
    if (ret == 0 || ret == -1)
      return -1;
    ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
    if (ret == -1) {
      switch (errno) {
      case EAGAIN:
        continue;
      default:
        return -1;
      }
    } else if (ret == 0) {
      printf("Unexpected end of file\n");
      return -1;
    }
    bytes_read += ret;
  } while (bytes_read < count);
  return 0;
}


x86 assembly (clang, -O0):
<do_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)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1f4,%edx
call   1050 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1187 <do_read+0x57>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1193 <do_read+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <do_read+0xfa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e2 <do_read+0xb2>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    11d6 <do_read+0xa6>
jmp    11d1 <do_read+0xa1>
jmp    1215 <do_read+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <do_read+0xfa>
cmpl   $0x0,-0x2c(%rbp)
jne    1204 <do_read+0xd4>
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <do_read+0xfa>
jmp    1209 <do_read+0xd9>
movslq -0x2c(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     115d <do_read+0x2d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%r12d
mov    %edi,(%rsp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %ebx,%ebx
mov    %rsp,%r15
mov    $0xffffffff,%ebp
jmp    1174 <do_read+0x44>
mov    0x2e71(%rip),%rax
cmpl   $0x80,(%rax)
jne    11c3 <do_read+0x93>
cmp    %r13,%rbx
jae    11b3 <do_read+0x83>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1f4,%edx
call   1050 <poll@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jb     11c3 <do_read+0x93>
lea    (%r14,%rbx,1),%rsi
mov    %r13,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1160 <do_read+0x30>
test   %eax,%eax
je     11b7 <do_read+0x87>
cltq
add    %rax,%rbx
cmp    %r13,%rbx
jb     1174 <do_read+0x44>
xor    %ebp,%ebp
jmp    11c3 <do_read+0x93>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@errno = global i32 0
@.str = [24 x i8] c"Unexpected end of file\0A\00"
define i32 @do_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.pollfd
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
%11 = load i32, i32* %5
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %13, i32* %14
br label %15
15: ; preds = %49, %3
%16 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 500)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %19, %15
store i32 -1, i32* %4
br label %54
23: ; preds = %19
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %7
%29 = load i64, i64* %8
%30 = sub i64 %28, %29
%31 = call i32 @read(i32 %24, i8* %27, i64 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = load i32, i32* @errno
switch i32 %35, label %37 [
i32 128, label %36
]
36: ; preds = %34
br label %49
37: ; preds = %34
store i32 -1, i32* %4
br label %54
38: ; preds = %23
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %54
43: ; preds = %38
br label %44
44: ; preds = %43
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = load i64, i64* %8
%48 = add i64 %47, %46
store i64 %48, i64* %8
br label %49
49: ; preds = %44, %36
%50 = load i64, i64* %8
%51 = load i64, i64* %7
%52 = icmp ult i64 %50, %51
br i1 %52, label %15, label %53
53: ; preds = %49
store i32 0, i32* %4
br label %54
54: ; preds = %53, %41, %37, %22
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@errno = global i32 0
@str = [23 x i8] c"Unexpected end of file\00"
define i32 @do_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca %struct.pollfd
%5 = bitcast %struct.pollfd* %4 to i8*
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %7, i32* %8
br label %9
9: ; preds = %26, %3
%10 = phi i64 [ 0, %3 ], [ %27, %26 ]
%11 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 500)
%12 = add i32 %11, 1
%13 = icmp ult i32 %12, 2
br i1 %13, label %29, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %1, i64 %10
%16 = sub i64 %2, %10
%17 = call i32 @read(i32 %0, i8* %15, i64 %16)
switch i32 %17, label %23 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = icmp eq i32 %19, 128
br i1 %20, label %26, label %29
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %29
23: ; preds = %14
%24 = sext i32 %17 to i64
%25 = add i64 %10, %24
br label %26
26: ; preds = %18, %23
%27 = phi i64 [ %10, %18 ], [ %25, %23 ]
%28 = icmp ult i64 %27, %2
br i1 %28, label %9, label %29
29: ; preds = %26, %18, %9, %21
%30 = phi i32 [ -1, %21 ], [ 0, %26 ], [ -1, %18 ], [ -1, %9 ]
ret i32 %30
}