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

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ ETIMEDOUT;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int pdemo_read(int, char *, unsigned int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int pdemo_read_to(int fd, char *buf, unsigned int l, unsigned int timeout) {
  int rc;
  fd_set fdset;
  struct timeval to;
one_more_time:
  FD_ZERO(&fdset);
  FD_SET(fd, &fdset);
  to.tv_sec = (time_t)timeout;
  to.tv_usec = 0;
  rc = select(fd + 1, &fdset, NULL, NULL, &to);
  switch (rc) {
  case -1: {
    if (EINTR == errno) {
      goto one_more_time;
    }
    return -1;
  } break;
  case 0: {
    errno = ETIMEDOUT;
    return -1;
  } break;
  default: {
    assert(FD_ISSET(fd, &fdset));
    return pdemo_read(fd, buf, l);
  } break;
  }
  return rc;
}


x86 assembly (clang, -O0):
<pdemo_read_to>:
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    -0x20(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1050 <FD_ZERO@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1080 <FD_SET@plt>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%eax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     11de <pdemo_read_to+0x7e>
jmp    11ce <pdemo_read_to+0x6e>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     1207 <pdemo_read_to+0xa7>
jmp    1225 <pdemo_read_to+0xc5>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%eax
jne    11fb <pdemo_read_to+0x9b>
jmp    1175 <pdemo_read_to+0x15>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <pdemo_read_to+0xea>
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc9(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <pdemo_read_to+0xea>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <FD_ISSET@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <pdemo_read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pdemo_read_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    %edi,%r12d
mov    %ecx,%r14d
lea    0x1(%r12),%ebx
lea    0xc(%rsp),%rbp
lea    0x18(%rsp),%r13
mov    0x2e3d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r14,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11ec <pdemo_read_to+0x8c>
mov    (%r15),%eax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%eax
je     11a0 <pdemo_read_to+0x40>
mov    $0xffffffff,%eax
jmp    122e <pdemo_read_to+0xce>
test   %eax,%eax
jne    1209 <pdemo_read_to+0xa9>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2de0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    122e <pdemo_read_to+0xce>
lea    0xc(%rsp),%rsi
mov    %r12d,%edi
call   1030 <FD_ISSET@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    %r12d,%edi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
call   1070 <pdemo_read@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@EINTR = global i32 0
@errno = global i32 0
@ETIMEDOUT = global i32 0
define i32 @pdemo_read_to(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 i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
br label %13
13: ; preds = %29, %4
%14 = call i32 @FD_ZERO(i32* %11)
%15 = load i32, i32* %6
%16 = call i32 @FD_SET(i32 %15, i32* %11)
%17 = load i32, i32* %9
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 %18, i64* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i64 0, i64* %20
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = call i32 @select(i32 %22, i32* %11, i32* null, i32* null, %struct.timeval* %12)
store i32 %23, i32* %10
%24 = load i32, i32* %10
switch i32 %24, label %33 [
i32 -1, label %25
i32 0, label %31
]
25: ; preds = %13
%26 = load i32, i32* @EINTR
%27 = load i32, i32* @errno
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %13
30: ; preds = %25
store i32 -1, i32* %5
br label %41
31: ; preds = %13
%32 = load i32, i32* @ETIMEDOUT
store i32 %32, i32* @errno
store i32 -1, i32* %5
br label %41
33: ; preds = %13
%34 = load i32, i32* %6
%35 = call i32 @FD_ISSET(i32 %34, i32* %11)
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* %6
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = call i32 @pdemo_read(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %5
br label %41
41: ; preds = %33, %31, %30
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@EINTR = global i32 0
@errno = global i32 0
@ETIMEDOUT = global i32 0
define i32 @pdemo_read_to(i32 %0, i8* %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 = zext i32 %3 to i64
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%12 = add nsw i32 %0, 1
br label %13
13: ; preds = %17, %4
%14 = call i32 @FD_ZERO(i32* %5)
%15 = call i32 @FD_SET(i32 %0, i32* %5)
store i64 %9, i64* %10
store i64 0, i64* %11
%16 = call i32 @select(i32 %12, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %16, label %23 [
i32 -1, label %17
i32 0, label %21
]
17: ; preds = %13
%18 = load i32, i32* @EINTR
%19 = load i32, i32* @errno
%20 = icmp eq i32 %18, %19
br i1 %20, label %13, label %27
21: ; preds = %13
%22 = load i32, i32* @ETIMEDOUT
store i32 %22, i32* @errno
br label %27
23: ; preds = %13
%24 = call i32 @FD_ISSET(i32 %0, i32* %5)
%25 = call i32 @assert(i32 %24)
%26 = call i32 @pdemo_read(i32 %0, i8* %1, i32 %2)
br label %27
27: ; preds = %17, %23, %21
%28 = phi i32 [ %26, %23 ], [ -1, %21 ], [ -1, %17 ]
ret i32 %28
}