#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int comm_wait_fd(int fd, short events, int timeout) {
  struct pollfd fds;
  fds.fd = fd;
  fds.events = events;
  fds.revents = 0;
  errno = 0;
  do {
    int r = poll(&fds, 1, timeout);
    if (r >= 0) {
      return fds.revents;
    }
  } while (errno == EINTR);
  return -1;
}


x86 assembly (clang, -O0):
<comm_wait_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %edi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xa(%rbp),%ax
mov    %ax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e9f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1171 <comm_wait_fd+0x61>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1197 <comm_wait_fd+0x87>
jmp    1176 <comm_wait_fd+0x66>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     1148 <comm_wait_fd+0x38>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comm_wait_fd>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %edi,0x8(%rsp)
mov    %si,0xc(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x2eab(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%r14
mov    0x2e90(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1030 <poll@plt>
test   %eax,%eax
jns    1173 <comm_wait_fd+0x63>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <comm_wait_fd+0x40>
mov    $0xffffffff,%eax
jmp    1177 <comm_wait_fd+0x67>
mov    0x10(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i16, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @comm_wait_fd(i32 %0, i16 signext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.pollfd
%9 = alloca i32
store i32 %0, i32* %5
store i16 %1, i16* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i16, i16* %6
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 1
store i16 %12, i16* %13
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 2
store i32 0, i32* %14
store i64 0, i64* @errno
br label %15
15: ; preds = %24, %3
%16 = load i32, i32* %7
%17 = call i32 @poll(%struct.pollfd* %8, i32 1, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 2
%22 = load i32, i32* %21
store i32 %22, i32* %4
br label %29
23: ; preds = %15
br label %24
24: ; preds = %23
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %15, label %28
28: ; preds = %24
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %20
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i16, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @comm_wait_fd(i32 %0, i16 signext %1, i32 %2) {
%4 = alloca %struct.pollfd
%5 = bitcast %struct.pollfd* %4 to i8*
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %0, i32* %6
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i16 %1, i16* %7
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
store i32 0, i32* %8
store i64 0, i64* @errno
br label %9
9: ; preds = %12, %3
%10 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 %2)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %18
16: ; preds = %9
%17 = load i32, i32* %8
br label %18
18: ; preds = %12, %16
%19 = phi i32 [ %17, %16 ], [ -1, %12 ]
ret i32 %19
}