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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int Wait_for_input(int fd, int microsec, int flag) {
  struct timeval wt;
  fd_set rds, wts, exs;
  int ready;
  FD_ZERO(&rds);
  FD_ZERO(&wts);
  FD_ZERO(&exs);
  FD_SET(fd, &rds);
  FD_SET(fd, &exs);
  wt.tv_sec = microsec / 1000000;
  wt.tv_usec = microsec % 1000000;
  ready = select(fd + 1, &rds, &wts, &exs, &wt);
  if (ready <= 0)
    return (0);
  if (FD_ISSET(fd, &exs))
    return (-1);
  if (FD_ISSET(fd, &rds))
    return (1);
  return (0);
}


x86 assembly (clang, -O0):
<Wait_for_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     11d4 <Wait_for_input+0x94>
movl   $0x0,-0x4(%rbp)
jmp    121f <Wait_for_input+0xdf>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11f6 <Wait_for_input+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <Wait_for_input+0xdf>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1218 <Wait_for_input+0xd8>
movl   $0x1,-0x4(%rbp)
jmp    121f <Wait_for_input+0xdf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Wait_for_input>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
lea    0x1c(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movslq %ebp,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,0x10(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x14(%rsp)
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1050 <select@plt>
test   %eax,%eax
jle    11fe <Wait_for_input+0xbe>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1200 <Wait_for_input+0xc0>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    1200 <Wait_for_input+0xc0>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @Wait_for_input(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = call i32 @FD_ZERO(i32* %9)
%14 = call i32 @FD_ZERO(i32* %10)
%15 = call i32 @FD_ZERO(i32* %11)
%16 = load i32, i32* %5
%17 = call i32 @FD_SET(i32 %16, i32* %9)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %11)
%20 = load i32, i32* %6
%21 = sdiv i32 %20, 1000000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = srem i32 %23, 1000000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
%28 = call i32 @select(i32 %27, i32* %9, i32* %10, i32* %11, %struct.timeval* %8)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %3
store i32 0, i32* %4
br label %43
32: ; preds = %3
%33 = load i32, i32* %5
%34 = call i64 @FD_ISSET(i32 %33, i32* %11)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 -1, i32* %4
br label %43
37: ; preds = %32
%38 = load i32, i32* %5
%39 = call i64 @FD_ISSET(i32 %38, i32* %9)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 1, i32* %4
br label %43
42: ; preds = %37
store i32 0, i32* %4
br label %43
43: ; preds = %42, %41, %36, %31
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @Wait_for_input(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.timeval* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = call i32 @FD_ZERO(i32* %5)
%13 = call i32 @FD_ZERO(i32* %6)
%14 = call i32 @FD_ZERO(i32* %7)
%15 = call i32 @FD_SET(i32 %0, i32* %5)
%16 = call i32 @FD_SET(i32 %0, i32* %7)
%17 = sdiv i32 %1, 1000000
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %17, i32* %18
%19 = srem i32 %1, 1000000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = add nsw i32 %0, 1
%22 = call i32 @select(i32 %21, i32* %5, i32* %6, i32* %7, %struct.timeval* %4)
%23 = icmp slt i32 %22, 1
br i1 %23, label %31, label %24
24: ; preds = %3
%25 = call i64 @FD_ISSET(i32 %0, i32* %7)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i64 @FD_ISSET(i32 %0, i32* %5)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %27, %24, %3
%32 = phi i32 [ 0, %3 ], [ -1, %24 ], [ %30, %27 ]
ret i32 %32
}