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

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int poll(struct pollfd *, int, int);
void poll_one(int fd) {
  struct pollfd pfd = {0};
  int ret;
  pfd.fd = fd;
  pfd.events = POLLIN;
  ret = poll(&pfd, 1, 1000);
  if (ret == -1)
    error(1, errno, "poll");
  if (ret == 0)
    error(1, 0, "poll: timeout");
  if (!(pfd.revents & POLLIN))
    error(1, 0, "poll: unexpected event(s) 0x%x\n", pfd.revents);
}


x86 assembly (clang, -O0):
<poll_one>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1040 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1199 <poll_one+0x69>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe6e(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11b8 <poll_one+0x88>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe54(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x10(%rbp),%eax
mov    0x2e26(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e5 <poll_one+0xb5>
mov    -0x10(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe35(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_one>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
mov    %edi,(%rsp)
mov    0x2eb0(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1030 <poll@plt>
test   %eax,%eax
je     1170 <poll_one+0x50>
cmp    $0xffffffff,%eax
jne    1185 <poll_one+0x65>
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdx
mov    $0x1,%edi
jmp    117e <poll_one+0x5e>
lea    0xe8e(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <error@plt>
mov    0x8(%rsp),%ecx
test   %ecx,(%rbx)
jne    11a2 <poll_one+0x82>
lea    0xe7f(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <error@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32, i32 }
@POLLIN = global i32 0
@errno = global i32 0
@.str = [5 x i8] c"poll\00"
@.str.1 = [14 x i8] c"poll: timeout\00"
@.str.2 = [32 x i8] c"poll: unexpected event(s) 0x%x\0A\00"
define void @poll_one(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = alloca i32
store i32 %0, i32* %2
%5 = bitcast %struct.pollfd* %3 to i8*
%6 = load i32, i32* %2
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 1000)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%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* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
%23 = load i32, i32* %22
%24 = load i32, i32* @POLLIN
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %21
ret void
}

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