#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 events;
  int revents;
};
typedef int ssize_t;

/* Variables and functions */
int POLLIN;
int POLLPRI;
int poll(struct pollfd *, int, int);
int read(int, void *, size_t);
ssize_t eci_impl_fd_read(int fd, void *buf, size_t count, int timeout) {
  int nfds = 1;
  struct pollfd ufds;
  ssize_t rescount = 0;
  int ret;
  ufds.fd = fd;
  ufds.events = POLLIN | POLLPRI;
  ufds.revents = 0;
  ret = poll(&ufds, nfds, timeout);
  if (ret > 0) {
    if (ufds.revents & POLLIN || ufds.revents & POLLPRI) {
      rescount = read(fd, buf, count);
    }
  } else if (ret == 0) {
    rescount = -1;
  }
  return rescount;
}


x86 assembly (clang, -O0):
<eci_impl_fd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    0x2e96(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1040 <poll@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jle    11c4 <eci_impl_fd_read+0xa4>
mov    -0x28(%rbp),%eax
mov    0x2e54(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11ac <eci_impl_fd_read+0x8c>
mov    -0x28(%rbp),%eax
mov    0x2e4f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11bf <eci_impl_fd_read+0x9f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x34(%rbp)
jmp    11da <eci_impl_fd_read+0xba>
cmpl   $0x0,-0x38(%rbp)
jne    11d5 <eci_impl_fd_read+0xb5>
movl   $0xffffffff,-0x34(%rbp)
jmp    11da <eci_impl_fd_read+0xba>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eci_impl_fd_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%ebp
mov    %edi,(%rsp)
mov    0x2ea2(%rip),%rbx
mov    0x2eab(%rip),%r15
mov    (%r15),%eax
or     (%rbx),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ecx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jle    1185 <eci_impl_fd_read+0x65>
mov    0x8(%rsp),%eax
test   %eax,(%rbx)
jne    1176 <eci_impl_fd_read+0x56>
test   %eax,(%r15)
je     118c <eci_impl_fd_read+0x6c>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
jmp    118e <eci_impl_fd_read+0x6e>
cmp    $0x1,%eax
sbb    %eax,%eax
jmp    118e <eci_impl_fd_read+0x6e>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
define i32 @eci_impl_fd_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.pollfd
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 1, i32* %9
store i32 0, i32* %11
%13 = load i32, i32* %5
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @POLLIN
%16 = load i32, i32* @POLLPRI
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
store i32 0, i32* %19
%20 = load i32, i32* %9
%21 = load i32, i32* %8
%22 = call i32 @poll(%struct.pollfd* %10, i32 %20, i32 %21)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %4
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%27 = load i32, i32* %26
%28 = load i32, i32* @POLLIN
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%33 = load i32, i32* %32
%34 = load i32, i32* @POLLPRI
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %31, %25
%38 = load i32, i32* %5
%39 = load i8*, i8** %6
%40 = load i64, i64* %7
%41 = call i32 @read(i32 %38, i8* %39, i64 %40)
store i32 %41, i32* %11
br label %42
42: ; preds = %37, %31
br label %48
43: ; preds = %4
%44 = load i32, i32* %12
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %11
br label %47
47: ; preds = %46, %43
br label %48
48: ; preds = %47, %42
%49 = load i32, i32* %11
ret i32 %49
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
define i32 @eci_impl_fd_read(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @POLLIN
%9 = load i32, i32* @POLLPRI
%10 = or i32 %9, %8
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
store i32 0, i32* %12
%13 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %3)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %4
%16 = load i32, i32* %12
%17 = load i32, i32* @POLLIN
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @POLLPRI
%22 = and i32 %21, %16
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20, %15
%25 = call i32 @read(i32 %0, i8* %1, i64 %2)
br label %29
26: ; preds = %4
%27 = icmp eq i32 %13, 0
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %26, %20, %24
%30 = phi i32 [ %25, %24 ], [ 0, %20 ], [ %28, %26 ]
ret i32 %30
}