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

/* Variables and functions */
scalar_t__ EINTR;
int POLLERR;
int POLLHUP;
int POLLIN;
int POLLNVAL;
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int SockAPI_PollEx(int sd, int timeout_ms, int *haserror) {
  int ret = -1;
  struct pollfd pfds = {sd, (POLLIN | POLLHUP | POLLERR), 0};
  do {
    ret = poll(&pfds, 1, timeout_ms);
  } while (ret == -1 && (errno == EINTR));
  if (ret == 1 && (((pfds.revents) & POLLIN) &&
                   (!((pfds.revents) & (POLLERR | POLLHUP | POLLNVAL))))) {
    *haserror = 0;
    return 0;
  }
  *haserror = (ret != 0);
  return -1;
}


x86 assembly (clang, -O0):
<SockAPI_PollEx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    0x2ea9(%rip),%rcx
or     (%rcx),%eax
mov    0x2e98(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x31(%rbp)
jne    1198 <SockAPI_PollEx+0x88>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    115b <SockAPI_PollEx+0x4b>
cmpl   $0x1,-0x1c(%rbp)
jne    1201 <SockAPI_PollEx+0xf1>
mov    -0x28(%rbp),%eax
mov    0x2e19(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1201 <SockAPI_PollEx+0xf1>
mov    -0x28(%rbp),%eax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e13(%rip),%rdx
or     (%rdx),%ecx
mov    0x2dfa(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1201 <SockAPI_PollEx+0xf1>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    121a <SockAPI_PollEx+0x10a>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SockAPI_PollEx>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,0x8(%rsp)
mov    0x2ea2(%rip),%r13
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
or     0x0(%r13),%eax
mov    0x2e9e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0xc(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <poll@plt>
cmp    $0xffffffff,%eax
jne    118d <SockAPI_PollEx+0x7d>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1170 <SockAPI_PollEx+0x60>
cmp    $0x1,%eax
jne    11bb <SockAPI_PollEx+0xab>
mov    0x10(%rsp),%ecx
test   %ecx,0x0(%r13)
je     11bb <SockAPI_PollEx+0xab>
mov    0x2e45(%rip),%rdx
mov    (%rdx),%edx
mov    0x2e34(%rip),%rsi
or     (%rsi),%edx
mov    0x2e23(%rip),%rsi
or     (%rsi),%edx
test   %ecx,%edx
je     11d9 <SockAPI_PollEx+0xc9>
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
mov    $0xffffffff,%eax
mov    %ecx,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ecx,%ecx
xor    %eax,%eax
jmp    11c7 <SockAPI_PollEx+0xb7>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32, i32 }
@POLLIN = global i32 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@POLLNVAL = global i32 0
define i32 @SockAPI_PollEx(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.pollfd
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 -1, i32* %8
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
%11 = load i32, i32* %5
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
%13 = load i32, i32* @POLLIN
%14 = load i32, i32* @POLLHUP
%15 = or i32 %13, %14
%16 = load i32, i32* @POLLERR
%17 = or i32 %15, %16
store i32 %17, i32* %12
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
store i32 0, i32* %18
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 3
store i32 0, i32* %19
br label %20
20: ; preds = %30, %3
%21 = load i32, i32* %6
%22 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %21)
store i32 %22, i32* %8
br label %23
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br label %30
30: ; preds = %26, %23
%31 = phi i1 [ false, %23 ], [ %29, %26 ]
br i1 %31, label %20, label %32
32: ; preds = %30
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %53
35: ; preds = %32
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%37 = load i32, i32* %36
%38 = load i32, i32* @POLLIN
%39 = and i32 %37, %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %35
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%43 = load i32, i32* %42
%44 = load i32, i32* @POLLERR
%45 = load i32, i32* @POLLHUP
%46 = or i32 %44, %45
%47 = load i32, i32* @POLLNVAL
%48 = or i32 %46, %47
%49 = and i32 %43, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %41
%52 = load i32*, i32** %7
store i32 0, i32* %52
store i32 0, i32* %4
br label %58
53: ; preds = %41, %35, %32
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 0
%56 = zext i1 %55 to i32
%57 = load i32*, i32** %7
store i32 %56, i32* %57
store i32 -1, i32* %4
br label %58
58: ; preds = %53, %51
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32, i32 }
@POLLIN = global i32 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@POLLNVAL = global i32 0
define i32 @SockAPI_PollEx(i32 %0, i32 %1, i32* nocapture writeonly %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
%8 = load i32, i32* @POLLIN
%9 = load i32, i32* @POLLHUP
%10 = or i32 %9, %8
%11 = load i32, i32* @POLLERR
%12 = or i32 %10, %11
store i32 %12, i32* %7
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 3
store i32 0, i32* %14
br label %15
15: ; preds = %15, %3
%16 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 %1)
%17 = icmp eq i32 %16, -1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %15, label %22
22: ; preds = %15
%23 = icmp eq i32 %16, 1
br i1 %23, label %24, label %37
24: ; preds = %22
%25 = load i32, i32* %13
%26 = load i32, i32* @POLLIN
%27 = and i32 %26, %25
%28 = icmp eq i32 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %24
%30 = load i32, i32* @POLLERR
%31 = load i32, i32* @POLLHUP
%32 = or i32 %31, %30
%33 = load i32, i32* @POLLNVAL
%34 = or i32 %32, %33
%35 = and i32 %34, %25
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %29, %24, %22
%38 = icmp ne i32 %16, 0
%39 = zext i1 %38 to i32
br label %40
40: ; preds = %29, %37
%41 = phi i32 [ %39, %37 ], [ 0, %29 ]
%42 = phi i32 [ -1, %37 ], [ 0, %29 ]
store i32 %41, i32* %2
ret i32 %42
}