#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*/ errno;
int /*<<< orphan*/ puterr(char *, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ write(int, char *, int);
int checkTimeout(int fd, int seconds) {
  struct timeval timeout;
  int n, ret = -1;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  timeout.tv_sec = seconds;
  timeout.tv_usec = 0;
  n = select(fd + 1, &rfds, NULL, NULL, &timeout);
  if (n == 0) {
    write(2, "socket timeout\n", 15);
    goto out;
  } else if (n < 0) {
    puterr("socket select", errno);
    goto out;
  }
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<checkTimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ca <checkTimeout+0x7a>
mov    $0x2,%edi
lea    0xe45(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
jmp    11fa <checkTimeout+0xaa>
cmpl   $0x0,-0x1c(%rbp)
jge    11ee <checkTimeout+0x9e>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe2c(%rip),%rdi
call   1040 <puterr@plt>
jmp    11fa <checkTimeout+0xaa>
jmp    11f3 <checkTimeout+0xa3>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkTimeout>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
mov    %ebx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
add    $0x1,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11b5 <checkTimeout+0x65>
jns    11d0 <checkTimeout+0x80>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1040 <puterr@plt>
jmp    11cb <checkTimeout+0x7b>
lea    0xe44(%rip),%rsi
mov    $0x2,%edi
mov    $0xf,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"socket timeout\0A\00"
@.str.1 = [14 x i8] c"socket select\00"
@errno = global i32 0
define i32 @checkTimeout(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %4)
%8 = call i32 @FD_SET(i32 %0, i32* %4)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = add nsw i32 %0, 1
%12 = call i32 @select(i32 %11, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 15)
br label %21
16: ; preds = %2
%17 = icmp slt i32 %12, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = load i32, i32* @errno
%20 = call i32 @puterr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %16, %18, %14
%22 = phi i32 [ -1, %14 ], [ -1, %18 ], [ 0, %16 ]
ret i32 %22
}