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

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int);
int poll_and_read(FILE *f, char *data, int size) {
  int fd = fileno(f);
  struct pollfd pfd;
  int poll_time = 2000;
  pfd.events = POLLIN;
  pfd.fd = fd;
  int status = 1;
  status = poll(&pfd, 1, poll_time);
  if (status == -1 || status == 0) {
    printf("Couldn't poll returned : %d\n", status);
    return 0;
  } else {
    if (fgets(data, size, f) == NULL) {
      return 0;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<poll_and_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x20(%rbp)
movl   $0x7d0,-0x2c(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x2c(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1060 <poll@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     11a7 <poll_and_read+0x67>
cmpl   $0x0,-0x30(%rbp)
jne    11c2 <poll_and_read+0x82>
mov    -0x30(%rbp),%esi
lea    0xe4f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f3 <poll_and_read+0xb3>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11e8 <poll_and_read+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    11f3 <poll_and_read+0xb3>
jmp    11ed <poll_and_read+0xad>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_and_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <fileno@plt>
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rsp)
mov    %eax,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%eax
cmp    $0x1,%eax
ja     1193 <poll_and_read+0x53>
lea    0xe78(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %ebp,%ebp
jmp    11a9 <poll_and_read+0x69>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
xor    %ecx,%ecx
test   %rax,%rax
cmove  %ecx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [29 x i8] c"Couldn't poll returned : %d\0A\00"
define i32 @poll_and_read(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32*, i32** %5
%13 = call i32 @fileno(i32* %12)
store i32 %13, i32* %8
store i32 2000, i32* %10
%14 = load i32, i32* @POLLIN
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* %8
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %16, i32* %17
store i32 1, i32* %11
%18 = load i32, i32* %10
%19 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %3
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22, %3
%26 = load i32, i32* %11
%27 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 0, i32* %4
br label %38
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = load i32*, i32** %5
%32 = call i32* @fgets(i8* %29, i32 %30, i32* %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 0, i32* %4
br label %38
35: ; preds = %28
br label %36
36: ; preds = %35
%37 = load i32, i32* %11
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %34, %25
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [29 x i8] c"Couldn't poll returned : %d\0A\00"
define i32 @poll_and_read(i32* nocapture %0, i8* %1, i32 %2) {
%4 = alloca %struct.pollfd
%5 = tail call i32 @fileno(i32* %0)
%6 = bitcast %struct.pollfd* %4 to i8*
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %5, i32* %9
%10 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 2000)
%11 = add i32 %10, 1
%12 = icmp ult i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %19
15: ; preds = %3
%16 = call i32* @fgets(i8* %1, i32 %2, i32* %0)
%17 = icmp eq i32* %16, null
%18 = select i1 %17, i32 0, i32 %10
br label %19
19: ; preds = %15, %13
%20 = phi i32 [ 0, %13 ], [ %18, %15 ]
ret i32 %20
}