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

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int poll(struct pollfd *, int, int);
int fgets_t(char *s, int size, FILE *stream, int timeout) {
  struct pollfd pollfd;
  int rc;
  pollfd.fd = fileno(stream);
  pollfd.events = POLLIN;
  rc = poll(&pollfd, 1, timeout);
  if (rc > 0) {
    return fgets(s, size, stream);
  } else {
    return rc;
  }
}


x86 assembly (clang, -O0):
<fgets_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edx
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1194 <fgets_t+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1030 <fgets@plt>
mov    %eax,-0x4(%rbp)
jmp    119a <fgets_t+0x6a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_t>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <fileno@plt>
mov    %eax,0x4(%rsp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1050 <poll@plt>
test   %eax,%eax
jle    117b <fgets_t+0x4b>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1030 <fgets@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
define i32 @fgets_t(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.pollfd
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%12 = load i32*, i32** %8
%13 = call i32 @fileno(i32* %12)
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @POLLIN
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %9
%18 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = load i32*, i32** %8
%25 = call i32 @fgets(i8* %22, i32 %23, i32* %24)
store i32 %25, i32* %5
br label %28
26: ; preds = %4
%27 = load i32, i32* %11
store i32 %27, i32* %5
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
define i32 @fgets_t(i8* %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = tail call i32 @fileno(i32* %2)
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @POLLIN
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %9, i32* %10
%11 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %3)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = call i32 @fgets(i8* %0, i32 %1, i32* %2)
br label %15
15: ; preds = %4, %13
%16 = phi i32 [ %14, %13 ], [ %11, %4 ]
ret i32 %16
}