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

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int poll(struct pollfd *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void check_poll(int fd, short events, int revents) {
  struct pollfd fds[] = {
      {.fd = fd, .events = events, .revents = 0},
  };
  int ret = poll(fds, 1, 0);
  fprintf(stderr, "poll {events = 0x%x} = %d", events, ret);
  if (ret == 1)
    fprintf(stderr, "; {revents = 0x%x}", (int)fds[0].revents);
  fprintf(stderr, "\n");
  fflush(stderr);
  if (ret < 0)
    err(1, "poll");
  int expected = revents ? 1 : 0;
  if (ret != expected)
    errx(1, "expected poll to return %d\n", expected);
  if (ret == 1 && fds[0].revents != revents)
    errx(1, "expected revents to be 0x%x", revents);
}


x86 assembly (clang, -O0):
<check_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x6(%rbp),%ax
mov    %ax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <poll@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
movswl -0x6(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    11cf <check_poll+0x7f>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    120f <check_poll+0xbf>
mov    $0x1,%edi
lea    0xe23(%rip),%rsi
call   1050 <err@plt>
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     1242 <check_poll+0xf2>
mov    -0x20(%rbp),%edx
mov    $0x1,%edi
lea    0xdf5(%rip),%rsi
call   1030 <errx@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    126c <check_poll+0x11c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     126c <check_poll+0x11c>
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
call   1030 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_poll>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,0x8(%rsp)
mov    %bx,0xc(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <poll@plt>
mov    %eax,%ebp
mov    0x2e56(%rip),%r15
mov    (%r15),%edi
lea    0xe6c(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    $0x1,%ebp
jne    11b9 <check_poll+0x69>
mov    (%r15),%edi
mov    0x10(%rsp),%edx
lea    0xe68(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%edi
lea    0xe89(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%edi
call   1060 <fflush@plt>
test   %ebp,%ebp
jns    11e7 <check_poll+0x97>
lea    0xe50(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
xor    %edx,%edx
test   %r14d,%r14d
setne  %dl
cmp    %edx,%ebp
je     1204 <check_poll+0xb4>
lea    0xe38(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
cmp    $0x1,%ebp
jne    1224 <check_poll+0xd4>
cmp    %r14d,0x10(%rsp)
je     1224 <check_poll+0xd4>
lea    0xe37(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
call   1030 <errx@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i16, i32 }
@stderr = global i32 0
@.str = [26 x i8] c"poll {events = 0x%x} = %d\00"
@.str.1 = [19 x i8] c"; {revents = 0x%x}\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"poll\00"
@.str.4 = [28 x i8] c"expected poll to return %d\0A\00"
@.str.5 = [28 x i8] c"expected revents to be 0x%x\00"
define void @check_poll(i32 %0, i16 signext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i32
%7 = alloca [1 x %struct.pollfd]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
%12 = load i32, i32* %4
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
%14 = load i16, i16* %5
store i16 %14, i16* %13
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
store i32 0, i32* %15
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%17 = call i32 @poll(%struct.pollfd* %16, i32 1, i32 0)
store i32 %17, i32* %8
%18 = load i32, i32* @stderr
%19 = load i16, i16* %5
%20 = sext i16 %19 to i32
%21 = load i32, i32* %8
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %31
25: ; preds = %3
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %25, %3
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 @fflush(i32 %34)
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %31
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 0
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 1, i32 0
store i32 %44, i32* %9
%45 = load i32, i32* %8
%46 = load i32, i32* %9
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* %9
%50 = call i32 @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %40
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 1
br i1 %53, label %54, label %63
54: ; preds = %51
%55 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 2
%57 = load i32, i32* %56
%58 = load i32, i32* %6
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = load i32, i32* %6
%62 = call i32 @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60, %54, %51
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i16, i32 }
@stderr = global i32 0
@.str = [26 x i8] c"poll {events = 0x%x} = %d\00"
@.str.1 = [19 x i8] c"; {revents = 0x%x}\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"poll\00"
@.str.4 = [28 x i8] c"expected poll to return %d\0A\00"
@.str.5 = [28 x i8] c"expected revents to be 0x%x\00"
define void @check_poll(i32 %0, i16 signext %1, i32 %2) {
%4 = alloca [1 x %struct.pollfd]
%5 = bitcast [1 x %struct.pollfd]* %4 to i8*
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %0, i32* %7
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i16 %1, i16* %8
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
store i32 0, i32* %9
%10 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 0)
%11 = load i32, i32* @stderr
%12 = sext i16 %1 to i32
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %10)
%14 = icmp eq i32 %10, 1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i32, i32* %9
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %3
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = call i32 @fflush(i32 %22)
%24 = icmp slt i32 %10, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = icmp ne i32 %2, 0
%29 = zext i1 %28 to i32
%30 = icmp eq i32 %10, %29
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %33
33: ; preds = %31, %27
%34 = xor i1 %14, true
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, %2
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %2)
br label %40
40: ; preds = %38, %33
ret void
}