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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ POLLIN;
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ setnonblocking(int);
int read_timeout(int fd, int timeout) {
  setnonblocking(fd);
  int ret;
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  fds[0].revents = 0;
  do {
    ret = poll(fds, 1, timeout);
  } while (ret < 0 && errno == EINTR);
  if (ret == 0) {
    errno = ETIMEDOUT;
    return -1;
  }
  if (ret == 1)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <setnonblocking@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <poll@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x31(%rbp)
jge    118d <read_timeout+0x6d>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1150 <read_timeout+0x30>
cmpl   $0x0,-0x10(%rbp)
jne    11c2 <read_timeout+0xa2>
mov    0x2e27(%rip),%rax
mov    (%rax),%rcx
mov    0x2e25(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <read_timeout+0xbf>
cmpl   $0x1,-0x10(%rbp)
jne    11d8 <read_timeout+0xb8>
movl   $0x0,-0x4(%rbp)
jmp    11df <read_timeout+0xbf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_timeout>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
call   1040 <setnonblocking@plt>
mov    %ebp,(%rsp)
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%r14
mov    0x2e82(%rip),%r15
mov    0x2e6b(%rip),%rbp
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1030 <poll@plt>
test   %eax,%eax
jns    117c <read_timeout+0x5c>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r15)
je     1160 <read_timeout+0x40>
cmp    $0x1,%eax
je     1199 <read_timeout+0x79>
test   %eax,%eax
jne    1192 <read_timeout+0x72>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%eax
jmp    119b <read_timeout+0x7b>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
define i32 @read_timeout(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1 x %struct.pollfd]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = call i32 @setnonblocking(i32 %8)
%10 = load i32, i32* %4
%11 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %10, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
store i32 %13, i32* %15
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i64 0, i64* %17
br label %18
18: ; preds = %29, %2
%19 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%20 = load i32, i32* %5
%21 = call i32 @poll(%struct.pollfd* %19, i32 1, i32 %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %18
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %22
%30 = phi i1 [ false, %22 ], [ %28, %25 ]
br i1 %30, label %18, label %31
31: ; preds = %29
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i64, i64* @ETIMEDOUT
store i64 %35, i64* @errno
store i32 -1, i32* %3
br label %41
36: ; preds = %31
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 0, i32* %3
br label %41
40: ; preds = %36
store i32 -1, i32* %3
br label %41
41: ; preds = %40, %39, %34
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
define i32 @read_timeout(i32 %0, i32 %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = tail call i32 @setnonblocking(i32 %0)
%5 = bitcast [1 x %struct.pollfd]* %3 to i8*
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i64 0, i64* %10
br label %11
11: ; preds = %11, %2
%12 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 %1)
%13 = icmp slt i32 %12, 0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
switch i32 %12, label %21 [
i32 0, label %19
i32 1, label %22
]
19: ; preds = %18
%20 = load i64, i64* @ETIMEDOUT
store i64 %20, i64* @errno
br label %22
21: ; preds = %18
br label %22
22: ; preds = %18, %21, %19
%23 = phi i32 [ -1, %19 ], [ -1, %21 ], [ 0, %18 ]
ret i32 %23
}