#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 dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ ETIME;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int gettimeout(struct timeval, struct timeval *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int waitfdtimed(int fd, struct timeval end) {
  fd_set readset;
  int retval;
  struct timeval timeout;
  if ((fd < 0) || (fd >= FD_SETSIZE)) {
    errno = EINVAL;
    return -1;
  }
  FD_ZERO(&readset);
  FD_SET(fd, &readset);
  if (gettimeout(end, &timeout) == -1)
    return -1;
  while (((retval = select(fd + 1, &readset, NULL, NULL, &timeout)) == -1) &&
         (errno == EINTR)) {
    if (gettimeout(end, &timeout) == -1)
      return -1;
    FD_ZERO(&readset);
    FD_SET(fd, &readset);
  }
  if (retval == 0) {
    errno = ETIME;
    return -1;
  }
  if (retval == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<waitfdtimed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %esi,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     116a <waitfdtimed+0x2a>
mov    -0xc(%rbp),%eax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%eax
jl     118a <waitfdtimed+0x4a>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e55(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <waitfdtimed+0x15e>
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <gettimeout@plt>
cmp    $0xffffffff,%eax
jne    11c0 <waitfdtimed+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <waitfdtimed+0x15e>
jmp    11c5 <waitfdtimed+0x85>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x19(%rbp)
jne    120c <waitfdtimed+0xcc>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    121c <waitfdtimed+0xdc>
jmp    1257 <waitfdtimed+0x117>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <gettimeout@plt>
cmp    $0xffffffff,%eax
jne    123d <waitfdtimed+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <waitfdtimed+0x15e>
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
jmp    11c5 <waitfdtimed+0x85>
cmpl   $0x0,-0x14(%rbp)
jne    1281 <waitfdtimed+0x141>
mov    0x2d80(%rip),%rax
mov    (%rax),%rcx
mov    0x2d5e(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <waitfdtimed+0x15e>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1297 <waitfdtimed+0x157>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <waitfdtimed+0x15e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitfdtimed>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
js     1236 <waitfdtimed+0xf6>
mov    %edi,%ebx
mov    0x2e7c(%rip),%rax
cmp    %edi,(%rax)
jle    1236 <waitfdtimed+0xf6>
mov    %esi,%r15d
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
call   1050 <gettimeout@plt>
mov    $0xffffffff,%ecx
cmp    $0xffffffff,%eax
je     124f <waitfdtimed+0x10f>
lea    0x1(%rbx),%r12d
lea    0x4(%rsp),%rsi
mov    %rsp,%r8
mov    %r12d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    0x2e1a(%rip),%r14
cmp    $0xffffffff,%eax
sete   %cl
jne    122b <waitfdtimed+0xeb>
mov    0x2e03(%rip),%rdx
mov    (%rdx),%rdx
cmp    %rdx,(%r14)
jne    122b <waitfdtimed+0xeb>
mov    %rsp,%r13
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <gettimeout@plt>
cmp    $0xffffffff,%eax
je     124a <waitfdtimed+0x10a>
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1040 <select@plt>
cmp    $0xffffffff,%eax
sete   %cl
jne    122b <waitfdtimed+0xeb>
mov    0x2da5(%rip),%rdx
mov    (%rdx),%rdx
cmp    %rdx,(%r14)
je     11e0 <waitfdtimed+0xa0>
test   %eax,%eax
je     1260 <waitfdtimed+0x120>
movzbl %cl,%ecx
neg    %ecx
jmp    124f <waitfdtimed+0x10f>
mov    0x2d83(%rip),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d81(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
jmp    124a <waitfdtimed+0x10a>

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

LLVM IR (-O2):
%struct.timeval = type { i32 }
@FD_SETSIZE = global i32 0
@EINVAL = global i64 0
@errno = global i64 0
@EINTR = global i64 0
@ETIME = global i64 0
define i32 @waitfdtimed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp sgt i32 %0, -1
%8 = load i32, i32* @FD_SETSIZE
%9 = icmp sgt i32 %8, %0
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = load i64, i64* @EINVAL
store i64 %12, i64* @errno
br label %46
13: ; preds = %2
%14 = call i32 @FD_ZERO(i32* %3)
%15 = call i32 @FD_SET(i32 %0, i32* %3)
%16 = call i32 @gettimeout(i32 %1, %struct.timeval* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %46, label %18
18: ; preds = %13
%19 = add nuw nsw i32 %0, 1
%20 = call i32 @select(i32 %19, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%21 = icmp eq i32 %20, -1
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %26, label %38
26: ; preds = %18, %29
%27 = call i32 @gettimeout(i32 %1, %struct.timeval* %4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %46, label %29
29: ; preds = %26
%30 = call i32 @FD_ZERO(i32* %3)
%31 = call i32 @FD_SET(i32 %0, i32* %3)
%32 = call i32 @select(i32 %19, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%33 = icmp eq i32 %32, -1
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
%37 = select i1 %33, i1 %36, i1 false
br i1 %37, label %26, label %38
38: ; preds = %29, %18
%39 = phi i32 [ %20, %18 ], [ %32, %29 ]
%40 = phi i1 [ %21, %18 ], [ %33, %29 ]
%41 = icmp eq i32 %39, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = load i64, i64* @ETIME
store i64 %43, i64* @errno
br label %46
44: ; preds = %38
%45 = sext i1 %40 to i32
br label %46
46: ; preds = %26, %44, %13, %42, %11
%47 = phi i32 [ -1, %11 ], [ -1, %42 ], [ -1, %13 ], [ %45, %44 ], [ -1, %26 ]
ret i32 %47
}