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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ ETIME;
int POLLERR;
int /*<<< orphan*/ POLLIN;
int POLLNVAL;
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int sync_wait(int fd, int timeout) {
  struct pollfd fds = {0};
  int ret;
  fds.fd = fd;
  fds.events = POLLIN;
  do {
    ret = poll(&fds, 1, timeout);
    if (ret > 0) {
      if (fds.revents & (POLLERR | POLLNVAL)) {
        errno = EINVAL;
        return -1;
      }
      return 0;
    } else if (ret == 0) {
      errno = ETIME;
      return -1;
    }
  } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  return ret;
}


x86 assembly (clang, -O0):
<sync_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11ba <sync_wait+0x9a>
mov    -0x1c(%rbp),%eax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e5f(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11ae <sync_wait+0x8e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e21(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <sync_wait+0x12d>
movl   $0x0,-0x4(%rbp)
jmp    124d <sync_wait+0x12d>
cmpl   $0x0,-0x24(%rbp)
jne    11e4 <sync_wait+0xc4>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rcx
mov    0x2deb(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <sync_wait+0x12d>
jmp    11e9 <sync_wait+0xc9>
jmp    11ee <sync_wait+0xce>
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    123c <sync_wait+0x11c>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2da8(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x26(%rbp)
je     1236 <sync_wait+0x116>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x26(%rbp)
mov    -0x26(%rbp),%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1150 <sync_wait+0x30>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_wait>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
movq   $0x0,0x4(%rsp)
movl   $0x0,0xc(%rsp)
mov    %edi,(%rsp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%r14
mov    0x2e76(%rip),%r15
mov    0x2e67(%rip),%r12
mov    0x2e48(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1030 <poll@plt>
test   %eax,%eax
jg     1196 <sync_wait+0x86>
cmp    $0xffffffff,%eax
jne    1189 <sync_wait+0x79>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <sync_wait+0x50>
cmp    0x0(%r13),%rax
je     1160 <sync_wait+0x50>
jmp    11bb <sync_wait+0xab>
test   %eax,%eax
jne    11c0 <sync_wait+0xb0>
mov    0x2e44(%rip),%rax
jmp    11b5 <sync_wait+0xa5>
mov    0x2e4b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %ecx,0x4(%rsp)
je     11ce <sync_wait+0xbe>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %eax,%eax
jmp    11c0 <sync_wait+0xb0>

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32, i32 }
@POLLIN = global i32 0
@POLLERR = global i32 0
@POLLNVAL = global i32 0
@EINVAL = global i64 0
@errno = global i64 0
@ETIME = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @sync_wait(i32 %0, i32 %1) {
%3 = alloca %struct.pollfd
%4 = bitcast %struct.pollfd* %3 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
%6 = bitcast i32* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
store i32 %8, i32* %9
br label %10
10: ; preds = %22, %2
%11 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 %1)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = load i32, i32* @POLLERR
%17 = load i32, i32* @POLLNVAL
%18 = or i32 %17, %16
%19 = and i32 %18, %15
%20 = icmp eq i32 %19, 0
br i1 %20, label %32, label %29
21: ; preds = %10
switch i32 %11, label %32 [
i32 0, label %29
i32 -1, label %22
]
22: ; preds = %21
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %23, %26
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %10, label %32
29: ; preds = %21, %13
%30 = phi i64* [ @EINVAL, %13 ], [ @ETIME, %21 ]
%31 = load i64, i64* %30
store i64 %31, i64* @errno
br label %32
32: ; preds = %22, %21, %29, %13
%33 = phi i32 [ 0, %13 ], [ -1, %29 ], [ -1, %22 ], [ %11, %21 ]
ret i32 %33
}