#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;
};

/* Variables and functions */
scalar_t__ EINTR;
int POLLIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ stderr;
int cg_wait_for(int fd) {
  int ret = -1;
  struct pollfd fds = {
      .fd = fd,
      .events = POLLIN,
  };
  while (true) {
    ret = poll(&fds, 1, 10000);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      fprintf(stderr, "Error: poll() failed\n");
      break;
    }
    if (ret > 0 && fds.revents & POLLIN) {
      ret = 0;
      break;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<cg_wait_for>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a4 <cg_wait_for+0x84>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <cg_wait_for+0x6a>
jmp    114b <cg_wait_for+0x2b>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d4 <cg_wait_for+0xb4>
cmpl   $0x0,-0x8(%rbp)
jle    11cf <cg_wait_for+0xaf>
mov    -0x10(%rbp),%eax
mov    0x2e30(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11cf <cg_wait_for+0xaf>
movl   $0x0,-0x8(%rbp)
jmp    11d4 <cg_wait_for+0xb4>
jmp    114b <cg_wait_for+0x2b>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cg_wait_for>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,0x8(%rsp)
mov    0x2eb2(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e7f(%rip),%r15
mov    0x2e70(%rip),%r12
jmp    1169 <cg_wait_for+0x49>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1191 <cg_wait_for+0x71>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
cmp    $0xffffffff,%eax
je     1160 <cg_wait_for+0x40>
test   %eax,%eax
jle    1169 <cg_wait_for+0x49>
mov    (%r14),%eax
test   %eax,0x10(%rsp)
je     1169 <cg_wait_for+0x49>
xor    %eax,%eax
jmp    11ab <cg_wait_for+0x8b>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"Error: poll() failed\0A\00"
define i32 @cg_wait_for(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
store i32 %0, i32* %2
store i32 -1, i32* %3
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
%6 = load i32, i32* %2
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%8 = load i32, i32* @POLLIN
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
store i32 0, i32* %9
br label %10
10: ; preds = %1, %18, %32
%11 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 10000)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %10
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %33
22: ; preds = %10
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
%27 = load i32, i32* %26
%28 = load i32, i32* @POLLIN
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 0, i32* %3
br label %33
32: ; preds = %25, %22
br label %10
33: ; preds = %31, %19
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"Error: poll() failed\0A\00"
define i32 @cg_wait_for(i32 %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%6 = load i32, i32* @POLLIN
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i32 0, i32* %7
br label %8
8: ; preds = %20, %1
%9 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 10000)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %26
18: ; preds = %8
%19 = icmp sgt i32 %9, 0
br i1 %19, label %21, label %20
20: ; preds = %18, %21, %11
br label %8
21: ; preds = %18
%22 = load i32, i32* %7
%23 = load i32, i32* @POLLIN
%24 = and i32 %23, %22
%25 = icmp eq i32 %24, 0
br i1 %25, label %20, label %26
26: ; preds = %21, %15
%27 = phi i32 [ -1, %15 ], [ 0, %21 ]
ret i32 %27
}