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

/* Variables and functions */
int POLLERR;
int POLLIN;
int /*<<< orphan*/ memset(struct pollfd *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int /*<<< orphan*/ *);
int wait_for_read(int fd) {
  struct pollfd fds[1];
  memset(fds, 0, sizeof(fds));
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  do {
    int rv = poll(fds, 1, NULL);
    if (rv == -1) {
      perror("poll failure");
      return -1;
    }
    if (fds[0].revents & POLLIN) {
      return 1;
    }
    if (fds[0].revents & POLLERR) {
      return -2;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<wait_for_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1194 <wait_for_read+0x64>
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <wait_for_read+0xba>
mov    -0x10(%rbp),%eax
mov    0x2e42(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b5 <wait_for_read+0x85>
movl   $0x1,-0x4(%rbp)
jmp    11ea <wait_for_read+0xba>
mov    -0x10(%rbp),%eax
mov    0x2e29(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d6 <wait_for_read+0xa6>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ea <wait_for_read+0xba>
jmp    11db <wait_for_read+0xab>
mov    $0x1,%al
test   $0x1,%al
jne    115d <wait_for_read+0x2d>
jmp    11ea <wait_for_read+0xba>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    %ebp,(%rsp)
mov    0x2e8a(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    0x2e84(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
cmp    $0xffffffff,%eax
je     11a6 <wait_for_read+0x76>
mov    0x8(%rsp),%eax
test   %eax,0x0(%rbp)
jne    1198 <wait_for_read+0x68>
test   %eax,(%rbx)
je     1170 <wait_for_read+0x40>
mov    $0xfffffffe,%eax
jmp    119d <wait_for_read+0x6d>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    119d <wait_for_read+0x6d>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [13 x i8] c"poll failure\00"
@POLLERR = global i32 0
define i32 @wait_for_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x %struct.pollfd]
%5 = alloca i32
store i32 %0, i32* %3
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%7 = call i32 @memset(%struct.pollfd* %6, i32 0, i32 12)
%8 = load i32, i32* %3
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @POLLIN
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 1
store i32 %11, i32* %13
br label %14
14: ; preds = %38, %1
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%16 = call i32 @poll(%struct.pollfd* %15, i32 1, i32* null)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
21: ; preds = %14
%22 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %22, i32 0, i32 2
%24 = load i32, i32* %23
%25 = load i32, i32* @POLLIN
%26 = and i32 %24, %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 1, i32* %2
br label %39
29: ; preds = %21
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %30, i32 0, i32 2
%32 = load i32, i32* %31
%33 = load i32, i32* @POLLERR
%34 = and i32 %32, %33
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 -2, i32* %2
br label %39
37: ; preds = %29
br label %38
38: ; preds = %37
br i1 true, label %14, label %39
39: ; preds = %19, %28, %36, %38
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [13 x i8] c"poll failure\00"
@POLLERR = global i32 0
define i32 @wait_for_read(i32 %0) {
%2 = alloca [1 x %struct.pollfd]
%3 = bitcast [1 x %struct.pollfd]* %2 to i8*
%4 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%5 = call i32 @memset(%struct.pollfd* %4, i32 0, i32 12)
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
br label %10
10: ; preds = %20, %1
%11 = call i32 @poll(%struct.pollfd* %4, i32 1, i32* null)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %24
15: ; preds = %10
%16 = load i32, i32* %9
%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* @POLLERR
%22 = and i32 %21, %16
%23 = icmp eq i32 %22, 0
br i1 %23, label %10, label %24
24: ; preds = %15, %20, %13
%25 = phi i32 [ -1, %13 ], [ 1, %15 ], [ -2, %20 ]
ret i32 %25
}