#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;
  scalar_t__ revents;
  scalar_t__ events;
};

/* Variables and functions */
scalar_t__ POLLIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int poll(struct pollfd *, int, int);
bool ring_poll(int fd) {
  struct pollfd pfd;
  int ret;
  pfd.fd = fd;
  pfd.events = POLLIN;
  ret = poll(&pfd, 1, 100);
  if (ret == -1)
    error(1, errno, "poll");
  if (ret == 0)
    return false;
  if (pfd.revents != POLLIN)
    error(1, 0, "unexpected event (0x%x)", pfd.revents);
  return true;
}


x86 assembly (clang, -O0):
<ring_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   1030 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117b <ring_poll+0x5b>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe8c(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1191 <ring_poll+0x71>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <ring_poll+0xa5>
mov    -0x18(%rbp),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     11be <ring_poll+0x9e>
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe4e(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ring_poll>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,(%rsp)
mov    0x2eb7(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   1030 <poll@plt>
mov    %eax,%ebx
test   %eax,%eax
je     119b <ring_poll+0x7b>
cmp    $0xffffffff,%ebx
jne    1172 <ring_poll+0x52>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <error@plt>
mov    0x8(%rsp),%rcx
mov    $0x1,%ebx
cmp    (%r14),%rcx
je     119b <ring_poll+0x7b>
lea    0xe7d(%rip),%rdx
mov    $0x1,%ebx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <error@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i64 }
@POLLIN = global i64 0
@errno = global i32 0
@.str = [5 x i8] c"poll\00"
@.str.1 = [24 x i8] c"unexpected event (0x%x)\00"
define i32 @ring_poll(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i64, i64* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
store i64 %8, i64* %9
%10 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 100)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %1
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %2
br label %30
20: ; preds = %16
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%22 = load i64, i64* %21
%23 = load i64, i64* @POLLIN
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%27 = load i64, i64* %26
%28 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %25, %20
store i32 1, i32* %2
br label %30
30: ; preds = %29, %19
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i64 }
@POLLIN = global i64 0
@errno = global i32 0
@.str = [5 x i8] c"poll\00"
@.str.1 = [24 x i8] c"unexpected event (0x%x)\00"
define i32 @ring_poll(i32 %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %4
%5 = load i64, i64* @POLLIN
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i64 %5, i64* %6
%7 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 100)
switch i32 %7, label %11 [
i32 -1, label %8
i32 0, label %18
]
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %1
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = load i64, i64* @POLLIN
%15 = icmp eq i64 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %13)
br label %18
18: ; preds = %11, %16, %1
%19 = phi i32 [ %7, %1 ], [ 1, %16 ], [ 1, %11 ]
ret i32 %19
}