#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 FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, struct timeval *);
int waitinput_1sec(int sock) {
  fd_set set;
  struct timeval tmv;
  FD_ZERO(&set);
  while (1) {
    FD_SET(sock, &set);
    tmv.tv_sec = 1;
    tmv.tv_usec = 0;
    if (select(sock + 1, &set, 0, 0, &tmv) < 0) {
      if (errno == EINTR)
        continue;
      perror("select");
      return 0;
    }
    return FD_ISSET(sock, &set);
  }
}


x86 assembly (clang, -O0):
<waitinput_1sec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
mov    %ecx,%edx
call   1050 <select@plt>
cmp    $0x0,%rax
jge    11d7 <waitinput_1sec+0x87>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <waitinput_1sec+0x6f>
jmp    1164 <waitinput_1sec+0x14>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <waitinput_1sec+0x96>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitinput_1sec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
lea    0x1(%r14),%ebp
lea    0x8(%rsp),%r15
mov    0x2e62(%rip),%r12
mov    0x2e53(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1050 <select@plt>
test   %rax,%rax
jns    11dc <waitinput_1sec+0x8c>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1190 <waitinput_1sec+0x40>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    11e9 <waitinput_1sec+0x99>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
add    $0x18,%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
@.str = [7 x i8] c"select\00"
define i32 @waitinput_1sec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
store i32 %0, i32* %3
%6 = call i32 @FD_ZERO(i32* %4)
br label %7
7: ; preds = %1, %20
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %4)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* %3
%13 = add nsw i32 %12, 1
%14 = call i64 @select(i32 %13, i32* %4, i32 0, i32 0, %struct.timeval* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %7
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %7
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %26
23: ; preds = %7
%24 = load i32, i32* %3
%25 = call i32 @FD_ISSET(i32 %24, i32* %4)
store i32 %25, i32* %2
br label %26
26: ; preds = %23, %21
%27 = load i32, i32* %2
ret i32 %27
}

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