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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int read(int /*<<< orphan*/, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sel(void) {
  struct timeval t;
  fd_set fd[1];
  int ret;
  unsigned char c;
  FD_SET(0, fd);
  t.tv_sec = 0;
  t.tv_usec = 0;
  ret = select(1, fd, NULL, NULL, &t);
  switch (ret) {
  case 0:
    return -1;
  case 1:
    ret = read(0, &c, 1);
    return ret == 1 ? c : -1;
  default:
    return -2;
  }
}


x86 assembly (clang, -O0):
<sel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
xor    %edi,%edi
mov    %edi,-0x2c(%rbp)
lea    -0x1c(%rbp),%rsi
mov    %rsi,-0x38(%rbp)
call   1050 <FD_SET@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%eax
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    %eax,%ecx
mov    $0x1,%edi
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     119b <sel+0x6b>
jmp    118a <sel+0x5a>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
je     11a7 <sel+0x77>
jmp    11e8 <sel+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <sel+0xbf>
xor    %edi,%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    11d0 <sel+0xa0>
movzbl -0x21(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11dd <sel+0xad>
mov    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    11dd <sel+0xad>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ef <sel+0xbf>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sel>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %eax,%eax
je     118e <sel+0x5e>
cmp    $0x1,%eax
jne    1195 <sel+0x65>
lea    0xb(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
movzbl 0xb(%rsp),%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    119a <sel+0x6a>
mov    $0xffffffff,%eax
jmp    119a <sel+0x6a>
mov    $0xfffffffe,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @sel() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca [1 x i32]
%4 = alloca i32
%5 = alloca i8
%6 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
%7 = call i32 @FD_SET(i32 0, i32* %6)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i64 0, i64* %9
%10 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
%11 = call i32 @select(i32 1, i32* %10, i32* null, i32* null, %struct.timeval* %2)
store i32 %11, i32* %4
%12 = load i32, i32* %4
switch i32 %12, label %24 [
i32 0, label %13
i32 1, label %14
]
13: ; preds = %0
store i32 -1, i32* %1
br label %25
14: ; preds = %0
%15 = call i32 @read(i32 0, i8* %5, i32 1)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i8, i8* %5
%20 = zext i8 %19 to i32
br label %22
21: ; preds = %14
br label %22
22: ; preds = %21, %18
%23 = phi i32 [ %20, %18 ], [ -1, %21 ]
store i32 %23, i32* %1
br label %25
24: ; preds = %0
store i32 -2, i32* %1
br label %25
25: ; preds = %24, %22, %13
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @sel() {
%1 = alloca %struct.timeval
%2 = alloca [1 x i32]
%3 = alloca i8
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast [1 x i32]* %2 to i8*
%6 = getelementptr inbounds [1 x i32], [1 x i32]* %2, i64 0, i64 0
%7 = call i32 @FD_SET(i32 0, i32* %6)
%8 = bitcast %struct.timeval* %1 to i8*
%9 = call i32 @select(i32 1, i32* %6, i32* null, i32* null, %struct.timeval* %1)
switch i32 %9, label %16 [
i32 0, label %17
i32 1, label %10
]
10: ; preds = %0
%11 = call i32 @read(i32 0, i8* %3, i32 1)
%12 = icmp eq i32 %11, 1
%13 = load i8, i8* %3
%14 = zext i8 %13 to i32
%15 = select i1 %12, i32 %14, i32 -1
br label %17
16: ; preds = %0
br label %17
17: ; preds = %0, %16, %10
%18 = phi i32 [ -2, %16 ], [ %15, %10 ], [ -1, %0 ]
ret i32 %18
}