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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int inpselect(int, int /*<<< orphan*/ *, struct timeval *);
int waitinput(int fd, unsigned ms) {
  fd_set set;
  struct timeval tmv;
  int ret;
  if (fd < 0)
    return 0;
  do {
    FD_ZERO(&set);
    FD_SET(fd, &set);
    tmv.tv_sec = 0;
    tmv.tv_usec = 1000 * ms;
    ret = inpselect(fd + 1, &set, &tmv);
  } while (ret == -1);
  return ret;
}


x86 assembly (clang, -O0):
<waitinput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1154 <waitinput+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <waitinput+0x76>
jmp    1159 <waitinput+0x29>
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
imul   $0x3e8,-0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <inpselect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     1159 <waitinput+0x29>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitinput>:
test   %edi,%edi
js     11a0 <waitinput+0x70>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
imul   $0x3e8,%esi,%r12d
lea    0x1(%r14),%ebp
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
mov    %r12d,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <inpselect@plt>
cmp    $0xffffffff,%eax
je     1160 <waitinput+0x30>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @waitinput(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* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %29
12: ; preds = %2
br label %13
13: ; preds = %24, %12
%14 = call i32 @FD_ZERO(i32* %6)
%15 = load i32, i32* %4
%16 = call i32 @FD_SET(i32 %15, i32* %6)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %5
%19 = mul i32 1000, %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = call i32 @inpselect(i32 %22, i32* %6, %struct.timeval* %7)
store i32 %23, i32* %8
br label %24
24: ; preds = %13
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %13, label %27
27: ; preds = %24
%28 = load i32, i32* %8
store i32 %28, i32* %3
br label %29
29: ; preds = %27, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @waitinput(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 slt i32 %0, 0
br i1 %7, label %18, label %8
8: ; preds = %2
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%10 = mul i32 %1, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%12 = add nuw nsw i32 %0, 1
br label %13
13: ; preds = %8, %13
%14 = call i32 @FD_ZERO(i32* %3)
%15 = call i32 @FD_SET(i32 %0, i32* %3)
store i64 0, i64* %9
store i32 %10, i32* %11
%16 = call i32 @inpselect(i32 %12, i32* %3, %struct.timeval* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %13, label %18
18: ; preds = %13, %2
%19 = phi i32 [ 0, %2 ], [ %16, %13 ]
ret i32 %19
}