#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ logperror(int /*<<< orphan*/, char *);
int /*<<< orphan*/ logprintf(int /*<<< orphan*/, char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_timeout(int fd, char *buf, int len, int timeout) {
  fd_set fds;
  struct timeval tv;
  int ret, n;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  do {
    ret = select(fd + 1, &fds, NULL, NULL, &tv);
  } while (ret == -1 && errno == EINTR);
  if (ret == -1) {
    logprintf(LOG_ERR, "select() failed");
    logperror(LOG_ERR, "");
    return (-1);
  } else if (ret == 0)
    return (0);
  n = read(fd, buf, len);
  if (n == -1) {
    logprintf(LOG_ERR, "read() failed");
    logperror(LOG_ERR, "");
    return (-1);
  }
  return (n);
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x34(%rbp)
mov    %al,-0x39(%rbp)
jne    11de <read_timeout+0x7e>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1198 <read_timeout+0x38>
cmpl   $0xffffffff,-0x34(%rbp)
jne    1229 <read_timeout+0xc9>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1080 <logprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <read_timeout+0x13c>
cmpl   $0x0,-0x34(%rbp)
jne    123f <read_timeout+0xdf>
movl   $0x0,-0x4(%rbp)
jmp    129c <read_timeout+0x13c>
jmp    1244 <read_timeout+0xe4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1296 <read_timeout+0x136>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1080 <logprintf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <read_timeout+0x13c>
mov    -0x38(%rbp),%eax
mov    %eax,-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   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %edx,0x4(%rsp)
mov    %rsi,0x8(%rsp)
mov    %edi,%r12d
lea    0x14(%rsp),%r13
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
mov    %ebx,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x1(%r12),%ebp
lea    0x18(%rsp),%rbx
mov    0x2e2e(%rip),%r14
mov    0x2e1f(%rip),%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11de <read_timeout+0x7e>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     11c0 <read_timeout+0x60>
cmp    $0xffffffff,%eax
je     1206 <read_timeout+0xa6>
test   %eax,%eax
je     1230 <read_timeout+0xd0>
mov    %r12d,%edi
mov    0x8(%rsp),%rsi
mov    0x4(%rsp),%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1232 <read_timeout+0xd2>
lea    0xe0c(%rip),%rsi
jmp    120d <read_timeout+0xad>
lea    0xdf3(%rip),%rsi
mov    0x2db4(%rip),%rbx
mov    (%rbx),%edi
call   1080 <logprintf@plt>
mov    (%rbx),%edi
lea    0xdf9(%rip),%rsi
call   1030 <logperror@plt>
mov    $0xffffffff,%eax
jmp    1232 <read_timeout+0xd2>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
@.str = [16 x i8] c"select() failed\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [14 x i8] c"read() failed\00"
define i32 @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 i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = call i32 @FD_ZERO(i32* %10)
%15 = load i32, i32* %6
%16 = call i32 @FD_SET(i32 %15, i32* %10)
%17 = load i32, i32* %9
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %19
br label %20
20: ; preds = %31, %4
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = call i32 @select(i32 %22, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %23, i32* %12
br label %24
24: ; preds = %20
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br label %31
31: ; preds = %27, %24
%32 = phi i1 [ false, %24 ], [ %30, %27 ]
br i1 %32, label %20, label %33
33: ; preds = %31
%34 = load i32, i32* %12
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i32, i32* @LOG_ERR
%38 = call i32 @logprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* @LOG_ERR
%40 = call i32 @logperror(i32 %39, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %60
41: ; preds = %33
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 0, i32* %5
br label %60
45: ; preds = %41
br label %46
46: ; preds = %45
%47 = load i32, i32* %6
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %13
%51 = load i32, i32* %13
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %46
%54 = load i32, i32* @LOG_ERR
%55 = call i32 @logprintf(i32 %54, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%56 = load i32, i32* @LOG_ERR
%57 = call i32 @logperror(i32 %56, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %60
58: ; preds = %46
%59 = load i32, i32* %13
store i32 %59, i32* %5
br label %60
60: ; preds = %58, %53, %44, %36
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
@.str = [16 x i8] c"select() failed\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [14 x i8] c"read() failed\00"
define i32 @read_timeout(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = call i32 @FD_SET(i32 %0, i32* %5)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %12
%13 = add nsw i32 %0, 1
br label %14
14: ; preds = %14, %4
%15 = call i32 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%16 = icmp eq i32 %15, -1
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %14, label %21
21: ; preds = %14
br i1 %16, label %27, label %22
22: ; preds = %21
%23 = icmp eq i32 %15, 0
br i1 %23, label %33, label %24
24: ; preds = %22
%25 = call i32 @read(i32 %0, i8* %1, i32 %2)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %24, %21
%28 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %21 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %24 ]
%29 = load i32, i32* @LOG_ERR
%30 = call i32 @logprintf(i32 %29, i8* %28)
%31 = load i32, i32* @LOG_ERR
%32 = call i32 @logperror(i32 %31, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %27, %24, %22
%34 = phi i32 [ 0, %22 ], [ %25, %24 ], [ -1, %27 ]
ret i32 %34
}