#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 /*<<< orphan*/ pfd;

/* Variables and functions */
int POLLERR;
int POLLHUP;
int POLLIN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ stderr;
int pollwait(int fd) {
  struct pollfd pfd[] = {
      {.fd = fd, .events = POLLIN},
      {.fd = 0},
      {.fd = 1},
  };
  const int ev_err = POLLERR | POLLHUP;
  int ret;
  do {
    ret = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), 10 * 1000);
    if (ret < 0) {
      perror("vznnc"
             ": poll()");
      return -1;
    }
  } while (ret == 0);
  if (pfd[0].revents & POLLIN)
    return 0;
  if (pfd[1].revents & ev_err || pfd[2].revents & ev_err) {
    fprintf(stderr, "vznnc"
                    ": broken pipe!");
    return 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<pollwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x3,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11cb <pollwait+0x9b>
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <pollwait+0x111>
jmp    11d0 <pollwait+0xa0>
cmpl   $0x0,-0x38(%rbp)
je     1193 <pollwait+0x63>
mov    -0x28(%rbp),%eax
mov    0x2df4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11fb <pollwait+0xcb>
movl   $0x0,-0x4(%rbp)
jmp    1241 <pollwait+0x111>
mov    -0x1c(%rbp),%eax
and    -0x34(%rbp),%eax
cmp    $0x0,%eax
jne    1219 <pollwait+0xe9>
mov    -0x10(%rbp),%eax
and    -0x34(%rbp),%eax
cmp    $0x0,%eax
je     123a <pollwait+0x10a>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <pollwait+0x111>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pollwait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,(%rsp)
mov    0x2e96(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movq   $0x1,0x18(%rsp)
movl   $0x0,0x20(%rsp)
mov    0x2e77(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
test   %eax,%eax
js     11cb <pollwait+0x9b>
je     1180 <pollwait+0x50>
mov    (%r14),%ecx
xor    %eax,%eax
test   %ecx,0x8(%rsp)
jne    11dc <pollwait+0xac>
test   %ebp,0x14(%rsp)
jne    11af <pollwait+0x7f>
test   %ebp,0x20(%rsp)
je     11d7 <pollwait+0xa7>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%eax
jmp    11dc <pollwait+0xac>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@.str = [14 x i8] c"vznnc: poll()\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"vznnc: broken pipe!\00"
define i32 @pollwait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x %struct.pollfd]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %4, i64 0, i64 0
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%9 = load i32, i32* %3
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
%11 = load i32, i32* @POLLIN
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 1
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 0
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 1
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 2
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i64 1
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 1
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 2
store i32 0, i32* %20
%21 = load i32, i32* @POLLERR
%22 = load i32, i32* @POLLHUP
%23 = or i32 %21, %22
store i32 %23, i32* %5
br label %24
24: ; preds = %32, %1
%25 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %4, i64 0, i64 0
%26 = call i32 @poll(%struct.pollfd* %25, i32 3, i32 10000)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
31: ; preds = %24
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %24, label %35
35: ; preds = %32
%36 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %4, i64 0, i64 0
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load i32, i32* @POLLIN
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 0, i32* %2
br label %61
43: ; preds = %35
%44 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %4, i64 0, i64 1
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load i32, i32* %5
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %57, label %50
50: ; preds = %43
%51 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %4, i64 0, i64 2
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = load i32, i32* %5
%55 = and i32 %53, %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %50, %43
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %61
60: ; preds = %50
store i32 -1, i32* %2
br label %61
61: ; preds = %60, %57, %42, %29
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@.str = [14 x i8] c"vznnc: poll()\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"vznnc: broken pipe!\00"
define i32 @pollwait(i32 %0) {
%2 = alloca [3 x %struct.pollfd]
%3 = bitcast [3 x %struct.pollfd]* %2 to i8*
%4 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 0
%5 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
%7 = load i32, i32* @POLLIN
store i32 %7, i32* %6
%8 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
%9 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 1, i32 2
%10 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 2, i32 0
%11 = bitcast i32* %8 to i8*
store i32 1, i32* %10
%12 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 2, i32 1
store i32 0, i32* %12
%13 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %2, i64 0, i64 2, i32 2
store i32 0, i32* %13
%14 = load i32, i32* @POLLERR
%15 = load i32, i32* @POLLHUP
%16 = or i32 %15, %14
br label %17
17: ; preds = %22, %1
%18 = call i32 @poll(%struct.pollfd* %4, i32 3, i32 10000)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %40
22: ; preds = %17
%23 = icmp eq i32 %18, 0
br i1 %23, label %17, label %24
24: ; preds = %22
%25 = load i32, i32* %8
%26 = load i32, i32* @POLLIN
%27 = and i32 %26, %25
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = load i32, i32* %9
%31 = and i32 %30, %16
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %13
%35 = and i32 %34, %16
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33, %29
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %33, %24, %37, %20
%41 = phi i32 [ -1, %20 ], [ 1, %37 ], [ 0, %24 ], [ -1, %33 ]
ret i32 %41
}