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

/* Variables and functions */
int BUFSIZ;
scalar_t__ EAGAIN;
int POLLERR;
int POLLHUP;
int POLLIN;
int POLLNVAL;
scalar_t__ errno;
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int checkfd(struct pollfd *fdp, int outfd) {
  int nr, nw;
  char buf[BUFSIZ];
  if (fdp->revents & (POLLNVAL | POLLERR | POLLHUP))
    return -1;
  if ((fdp->revents & POLLIN) == 0)
    return 0;
  errno = 0;
  nr = read(fdp->fd, buf, sizeof buf);
  if (nr <= 0) {
    if (errno != EAGAIN)
      return -1;
    else
      return 0;
  } else {
    char *bc = buf;
    while (nr) {
      if ((nw = write(outfd, bc, nr)) <= 0)
        return -1;
      nr -= nw;
      bc += nw;
    }
    return 0;
  }
}


x86 assembly (clang, -O0):
<checkfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e6b(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e6a(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     119e <checkfd+0x7e>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12a9 <checkfd+0x189>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e25(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11c9 <checkfd+0xa9>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12a9 <checkfd+0x189>
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
mov    0x2de0(%rip),%rcx
movq   $0x0,(%rcx)
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%edi
mov    %eax,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     123a <checkfd+0x11a>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
je     1227 <checkfd+0x107>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12a9 <checkfd+0x189>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12a9 <checkfd+0x189>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     129b <checkfd+0x17b>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     127a <checkfd+0x15a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12a9 <checkfd+0x189>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    1242 <checkfd+0x122>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r12
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%rdi),%eax
mov    0x2e84(%rip),%rcx
mov    0x2e85(%rip),%rsi
mov    (%rsi),%esi
or     (%rcx),%esi
mov    0x2e82(%rip),%rcx
or     (%rcx),%esi
mov    $0xffffffff,%r14d
test   %eax,%esi
jne    11d7 <checkfd+0xb7>
mov    0x2e57(%rip),%rcx
test   %eax,(%rcx)
je     11ba <checkfd+0x9a>
mov    0x2e34(%rip),%r13
movq   $0x0,0x0(%r13)
mov    0x4(%rdi),%edi
mov    %r12,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    11bf <checkfd+0x9f>
mov    %eax,%ebx
nopl   (%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11d7 <checkfd+0xb7>
mov    %eax,%ecx
add    %rcx,%r12
sub    %eax,%ebx
jne    11a0 <checkfd+0x80>
xor    %r14d,%r14d
jmp    11d7 <checkfd+0xb7>
mov    0x0(%r13),%rax
mov    0x2dde(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
setne  %r14b
neg    %r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@BUFSIZ = global i32 0
@POLLNVAL = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@POLLIN = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @checkfd(%struct.pollfd* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.pollfd*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store %struct.pollfd* %0, %struct.pollfd** %4
store i32 %1, i32* %5
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load %struct.pollfd*, %struct.pollfd** %4
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @POLLNVAL
%20 = load i32, i32* @POLLERR
%21 = or i32 %19, %20
%22 = load i32, i32* @POLLHUP
%23 = or i32 %21, %22
%24 = and i32 %18, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %10
br label %69
27: ; preds = %2
%28 = load %struct.pollfd*, %struct.pollfd** %4
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLIN
%32 = and i32 %30, %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 0, i32* %3
store i32 1, i32* %10
br label %69
35: ; preds = %27
store i64 0, i64* @errno
%36 = load %struct.pollfd*, %struct.pollfd** %4
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = trunc i64 %13 to i32
%40 = call i32 @read(i32 %38, i8* %15, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %35
%44 = load i64, i64* @errno
%45 = load i64, i64* @EAGAIN
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 -1, i32* %3
store i32 1, i32* %10
br label %69
48: ; preds = %43
store i32 0, i32* %3
store i32 1, i32* %10
br label %69
49: ; preds = %35
store i8* %15, i8** %11
br label %50
50: ; preds = %60, %49
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %68
53: ; preds = %50
%54 = load i32, i32* %5
%55 = load i8*, i8** %11
%56 = load i32, i32* %6
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
store i32 %57, i32* %7
%58 = icmp sle i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %53
store i32 -1, i32* %3
store i32 1, i32* %10
br label %69
60: ; preds = %53
%61 = load i32, i32* %7
%62 = load i32, i32* %6
%63 = sub nsw i32 %62, %61
store i32 %63, i32* %6
%64 = load i32, i32* %7
%65 = load i8*, i8** %11
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %11
br label %50
68: ; preds = %50
store i32 0, i32* %3
store i32 1, i32* %10
br label %69
69: ; preds = %68, %59, %48, %47, %34, %26
%70 = load i8*, i8** %8
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@BUFSIZ = global i32 0
@POLLNVAL = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@POLLIN = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @checkfd(%struct.pollfd* nocapture %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @POLLNVAL
%9 = load i32, i32* @POLLERR
%10 = or i32 %9, %8
%11 = load i32, i32* @POLLHUP
%12 = or i32 %10, %11
%13 = and i32 %12, %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %39
15: ; preds = %2
%16 = load i32, i32* @POLLIN
%17 = and i32 %16, %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %39, label %19
19: ; preds = %15
store i64 0, i64* @errno
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @read(i32 %21, i8* %5, i32 %3)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp ne i64 %25, %26
%28 = sext i1 %27 to i32
br label %39
29: ; preds = %19, %34
%30 = phi i8* [ %37, %34 ], [ %5, %19 ]
%31 = phi i32 [ %35, %34 ], [ %22, %19 ]
%32 = call i32 @write(i32 %1, i8* %30, i32 %31)
%33 = icmp slt i32 %32, 1
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = sub nsw i32 %31, %32
%36 = zext i32 %32 to i64
%37 = getelementptr inbounds i8, i8* %30, i64 %36
%38 = icmp eq i32 %35, 0
br i1 %38, label %39, label %29
39: ; preds = %34, %29, %24, %15, %2
%40 = phi i32 [ -1, %2 ], [ 0, %15 ], [ %28, %24 ], [ 0, %34 ], [ -1, %29 ]
ret i32 %40
}