#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 revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ ENOTCONN;
int /*<<< orphan*/ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int /*<<< orphan*/ POLLRDNORM;
int /*<<< orphan*/ errno;
int poll(struct pollfd *, int, int);
int read(int, void *, int);
int daemon_read(int fildes, void *buf, int nbyte, int to) {
  int i;
  struct pollfd fds[1];
  if (nbyte == 0) {
    return 0;
  }
  fds[0].fd = fildes;
  fds[0].events = POLLRDNORM;
  fds[0].revents = 0;
  i = poll(fds, 1, to > 0 ? to * 1000 : -1);
  if (i < 0) {
    i = -1;
  } else if (i == 0) {
    errno = ETIMEDOUT;
  } else if (fds[0].revents & (POLLHUP | POLLERR)) {
    i = -1;
    errno = ENOTCONN;
  } else {
    i = read(fildes, buf, nbyte);
    if (i == 0) {
      i = -1;
      errno = ENOTCONN;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<daemon_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    114b <daemon_read+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1265 <daemon_read+0x145>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1185 <daemon_read+0x65>
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1192 <daemon_read+0x72>
mov    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1192 <daemon_read+0x72>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bc <daemon_read+0x9c>
movl   $0xffffffff,-0x1c(%rbp)
jmp    125f <daemon_read+0x13f>
cmpl   $0x0,-0x1c(%rbp)
jne    11dd <daemon_read+0xbd>
mov    0x2deb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dea(%rip),%rax
mov    %ecx,(%rax)
jmp    125a <daemon_read+0x13a>
mov    -0x24(%rbp),%eax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2de8(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     121b <daemon_read+0xfb>
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dac(%rip),%rax
mov    %ecx,(%rax)
jmp    1255 <daemon_read+0x135>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1250 <daemon_read+0x130>
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d72(%rip),%rax
mov    %ecx,(%rax)
jmp    1255 <daemon_read+0x135>
jmp    125a <daemon_read+0x13a>
jmp    125f <daemon_read+0x13f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
je     11b6 <daemon_read+0x96>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %edi,0x8(%rsp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movl   $0x0,0xc(%rsp)
imul   $0x3e8,%ecx,%edx
test   %ecx,%ecx
mov    $0xffffffff,%ebp
cmovle %ebp,%edx
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
test   %eax,%eax
js     11d4 <daemon_read+0xb4>
je     11ba <daemon_read+0x9a>
mov    0x2e65(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0xffffffff,%r12d
mov    0x2e35(%rip),%r13
test   %ecx,0xc(%rsp)
jne    11c4 <daemon_read+0xa4>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11d4 <daemon_read+0xb4>
jmp    11c4 <daemon_read+0xa4>
xor    %ebp,%ebp
jmp    11d4 <daemon_read+0xb4>
xor    %r12d,%r12d
mov    0x2df4(%rip),%r13
mov    0x0(%r13),%eax
mov    0x2df1(%rip),%rcx
mov    %eax,(%rcx)
mov    %r12d,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLRDNORM = global i32 0
@ETIMEDOUT = global i32 0
@errno = global i32 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
@ENOTCONN = global i32 0
define i32 @daemon_read(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1 x %struct.pollfd]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 0, i32* %5
br label %67
15: ; preds = %4
%16 = load i32, i32* %6
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %11, i64 0, i64 0
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @POLLRDNORM
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %11, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %11, i64 0, i64 0
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %22, i32 0, i32 1
store i32 0, i32* %23
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %11, i64 0, i64 0
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = load i32, i32* %9
%29 = mul nsw i32 %28, 1000
br label %31
30: ; preds = %15
br label %31
31: ; preds = %30, %27
%32 = phi i32 [ %29, %27 ], [ -1, %30 ]
%33 = call i32 @poll(%struct.pollfd* %24, i32 1, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 -1, i32* %10
br label %65
37: ; preds = %31
%38 = load i32, i32* %10
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load i32, i32* @ETIMEDOUT
store i32 %41, i32* @errno
br label %64
42: ; preds = %37
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %11, i64 0, i64 0
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32, i32* @POLLHUP
%47 = load i32, i32* @POLLERR
%48 = or i32 %46, %47
%49 = and i32 %45, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %42
store i32 -1, i32* %10
%52 = load i32, i32* @ENOTCONN
store i32 %52, i32* @errno
br label %63
53: ; preds = %42
%54 = load i32, i32* %6
%55 = load i8*, i8** %7
%56 = load i32, i32* %8
%57 = call i32 @read(i32 %54, i8* %55, i32 %56)
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
store i32 -1, i32* %10
%61 = load i32, i32* @ENOTCONN
store i32 %61, i32* @errno
br label %62
62: ; preds = %60, %53
br label %63
63: ; preds = %62, %51
br label %64
64: ; preds = %63, %40
br label %65
65: ; preds = %64, %36
%66 = load i32, i32* %10
store i32 %66, i32* %5
br label %67
67: ; preds = %65, %14
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLRDNORM = global i32 0
@ETIMEDOUT = global i32 0
@errno = global i32 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
@ENOTCONN = global i32 0
define i32 @daemon_read(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca [1 x %struct.pollfd]
%6 = bitcast [1 x %struct.pollfd]* %5 to i8*
%7 = icmp eq i32 %2, 0
br i1 %7, label %35, label %8
8: ; preds = %4
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @POLLRDNORM
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 1
store i32 0, i32* %13
%14 = icmp sgt i32 %3, 0
%15 = mul nsw i32 %3, 1000
%16 = select i1 %14, i32 %15, i32 -1
%17 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %35, label %19
19: ; preds = %8
%20 = icmp eq i32 %17, 0
br i1 %20, label %31, label %21
21: ; preds = %19
%22 = load i32, i32* %13
%23 = load i32, i32* @POLLHUP
%24 = load i32, i32* @POLLERR
%25 = or i32 %24, %23
%26 = and i32 %25, %22
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @read(i32 %0, i8* %1, i32 %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28, %21, %19
%32 = phi i32* [ @ETIMEDOUT, %19 ], [ @ENOTCONN, %21 ], [ @ENOTCONN, %28 ]
%33 = phi i32 [ 0, %19 ], [ -1, %21 ], [ -1, %28 ]
%34 = load i32, i32* %32
store i32 %34, i32* @errno
br label %35
35: ; preds = %31, %28, %8, %4
%36 = phi i32 [ 0, %4 ], [ %29, %28 ], [ -1, %8 ], [ %33, %31 ]
ret i32 %36
}