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

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int errno;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int /*<<< orphan*/, char *, int);
int poll1() {
  struct pollfd fds[1];
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;
  while (1) {
    int ret = poll(fds, 1, 100);
    if (ret == -1) {
      perror("poll");
      return errno;
    }
    if (fds[0].revents & POLLIN) {
      char buf[32];
      bzero(buf, sizeof(buf));
      ret = read(fds[0].fd, buf, sizeof(buf));
      if (ret == -1) {
        perror("read");
        return errno;
      }
      printf("read from stdin ==> %s\n", buf);
    }
  }
}


x86 assembly (clang, -O0):
<poll1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   1050 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ad <poll1+0x5d>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <poll1+0xd0>
mov    -0xc(%rbp),%eax
mov    0x2e31(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     121b <poll1+0xcb>
lea    -0x40(%rbp),%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120b <poll1+0xbb>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <poll1+0xd0>
lea    -0x40(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
jmp    1170 <poll1+0x20>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll1>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e79(%rip),%r12
mov    (%r12),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
jne    1196 <poll1+0x46>
lea    0xe6c(%rip),%rdi
jmp    1208 <poll1+0xb8>
mov    %rsp,%r14
lea    0x10(%rsp),%rbx
lea    0xe65(%rip),%r15
jmp    11c7 <poll1+0x77>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
je     118d <poll1+0x3d>
mov    (%r12),%eax
test   %eax,0x4(%rsp)
je     11b0 <poll1+0x60>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1201 <poll1+0xb1>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
jmp    11b0 <poll1+0x60>
lea    0xdfd(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [24 x i8] c"read from stdin ==> %s\0A\00"
define i32 @poll1() {
%1 = alloca i32
%2 = alloca [1 x %struct.pollfd]
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
store i32 %5, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %8, i32* %10
br label %11
11: ; preds = %0, %42
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%13 = call i32 @poll(%struct.pollfd* %12, i32 1, i32 100)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @errno
store i32 %18, i32* %1
br label %43
19: ; preds = %11
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @POLLIN
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %19
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%28 = call i32 @bzero(i8* %27, i32 32)
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 32)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @errno
store i32 %38, i32* %1
br label %43
39: ; preds = %26
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %19
br label %11
43: ; preds = %36, %16
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [24 x i8] c"read from stdin ==> %s\0A\00"
define i32 @poll1() {
%1 = alloca [1 x %struct.pollfd]
%2 = alloca [32 x i8]
%3 = bitcast [1 x %struct.pollfd]* %1 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 2
store i32 %4, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 0
store i32 %7, i32* %8
%9 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 100)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 1
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
br label %17
14: ; preds = %32, %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @errno
br label %35
17: ; preds = %11, %32
%18 = load i32, i32* %12
%19 = load i32, i32* @POLLIN
%20 = and i32 %19, %18
%21 = icmp eq i32 %20, 0
br i1 %21, label %32, label %22
22: ; preds = %17
%23 = call i32 @bzero(i8* %13, i32 32)
%24 = load i32, i32* %6
%25 = call i32 @read(i32 %24, i8* %13, i32 32)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @errno
br label %35
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %32
32: ; preds = %17, %30
%33 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 100)
%34 = icmp eq i32 %33, -1
br i1 %34, label %14, label %17
35: ; preds = %14, %27
%36 = phi i32 [ %29, %27 ], [ %16, %14 ]
ret i32 %36
}