#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 */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void waitpkt(int fd, int sec) {
  fd_set rs, es;
  struct timeval timeout;
  int r;
  timeout.tv_sec = sec;
  timeout.tv_usec = 0;
  FD_ZERO(&rs);
  FD_SET(fd, &rs);
  FD_ZERO(&es);
  FD_SET(fd, &es);
  r = select(fd + 1, &rs, NULL, &es, &timeout);
  if (r == -1)
    error("select() failed");
  if (r == 0)
    error("no reply, timed out");
}


x86 assembly (clang, -O0):
<waitpkt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ba <waitpkt+0x7a>
lea    0xe4b(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x0,-0x24(%rbp)
jne    11d0 <waitpkt+0x90>
lea    0xe45(%rip),%rdi
call   1050 <error@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitpkt>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %esi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
add    $0x1,%ebx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
mov    %r15,%rcx
call   1040 <select@plt>
cmp    $0xffffffff,%eax
je     11af <waitpkt+0x6f>
test   %eax,%eax
jne    11bb <waitpkt+0x7b>
lea    0xe63(%rip),%rdi
jmp    11b6 <waitpkt+0x76>
lea    0xe4a(%rip),%rdi
call   1050 <error@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"select() failed\00"
@.str.1 = [20 x i8] c"no reply, timed out\00"
define void @waitpkt(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %11
%12 = call i32 @FD_ZERO(i32* %5)
%13 = load i32, i32* %3
%14 = call i32 @FD_SET(i32 %13, i32* %5)
%15 = call i32 @FD_ZERO(i32* %6)
%16 = load i32, i32* %3
%17 = call i32 @FD_SET(i32 %16, i32* %6)
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %5, i32* null, i32* %6, %struct.timeval* %7)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"select() failed\00"
@.str.1 = [20 x i8] c"no reply, timed out\00"
define void @waitpkt(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @FD_ZERO(i32* %3)
%12 = call i32 @FD_SET(i32 %0, i32* %3)
%13 = call i32 @FD_ZERO(i32* %4)
%14 = call i32 @FD_SET(i32 %0, i32* %4)
%15 = add nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %3, i32* null, i32* %4, %struct.timeval* %5)
switch i32 %16, label %21 [
i32 -1, label %18
i32 0, label %17
]
17: ; preds = %2
br label %18
18: ; preds = %2, %17
%19 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = call i32 @error(i8* %19)
br label %21
21: ; preds = %18, %2
ret void
}