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

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
int bot_fileutils_read_timeout(int fd, void *buf, int maxlen, int msTimeout) {
  struct pollfd pfd;
  int len;
  int res;
  pfd.fd = fd;
  pfd.events = POLLIN;
  res = poll(&pfd, 1, msTimeout);
  if (res < 0) {
    perror("poll");
    return -1;
  }
  if (res == 0) {
    errno = ETIMEDOUT;
    return 0;
  }
  len = read(fd, buf, maxlen);
  if (len < 0) {
    perror("read");
    return -1;
  }
  if (len == 0) {
    fprintf(stderr, "end of file\n");
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<bot_fileutils_read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    119d <bot_fileutils_read_timeout+0x5d>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <bot_fileutils_read_timeout+0xea>
cmpl   $0x0,-0x28(%rbp)
jne    11c5 <bot_fileutils_read_timeout+0x85>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e19(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    122a <bot_fileutils_read_timeout+0xea>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f9 <bot_fileutils_read_timeout+0xb9>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <bot_fileutils_read_timeout+0xea>
cmpl   $0x0,-0x24(%rbp)
jne    1224 <bot_fileutils_read_timeout+0xe4>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <bot_fileutils_read_timeout+0xea>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_fileutils_read_timeout>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edi,0x8(%rsp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %ecx,%edx
call   1050 <poll@plt>
test   %eax,%eax
js     11b8 <bot_fileutils_read_timeout+0x78>
je     11a2 <bot_fileutils_read_timeout+0x62>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11c1 <bot_fileutils_read_timeout+0x81>
jne    11d2 <bot_fileutils_read_timeout+0x92>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11cd <bot_fileutils_read_timeout+0x8d>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    0x2e1e(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    11d2 <bot_fileutils_read_timeout+0x92>
lea    0xe41(%rip),%rdi
jmp    11c8 <bot_fileutils_read_timeout+0x88>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@ETIMEDOUT = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.2 = [13 x i8] c"end of file\0A\00"
define i32 @bot_fileutils_read_timeout(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.pollfd
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @POLLIN
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
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* %12
%19 = load i32, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
23: ; preds = %4
%24 = load i32, i32* %12
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* @ETIMEDOUT
store i32 %27, i32* @errno
store i32 0, i32* %5
br label %45
28: ; preds = %23
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i32 @read(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
37: ; preds = %28
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
43: ; preds = %37
%44 = load i32, i32* %11
store i32 %44, i32* %5
br label %45
45: ; preds = %43, %40, %35, %26, %21
%46 = load i32, i32* %5
ret i32 %46
}

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