#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 /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  struct timeval timeout;
  fd_set readfds;
  int i;
  char c;
  timeout.tv_sec = 5;
  timeout.tv_usec = 0;
  FD_ZERO(&readfds);
  FD_SET(0, &readfds);
  i = select(1, &readfds, NULL, NULL, &timeout);
  if (i > 0) {
    read(0, &c, 1);
    printf("%d\n", c);
  } else {
    if (i == 0) {
      printf("timeout\n");
    } else {
      printf("error\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11cf <main+0x7f>
xor    %edi,%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movsbl -0x21(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ff <main+0xaf>
cmpl   $0x0,-0x20(%rbp)
jne    11ec <main+0x9c>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fa <main+0xaa>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ff <main+0xaf>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
jle    11cd <main+0x6d>
lea    0xb(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl 0xb(%rsp),%esi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e4 <main+0x84>
je     11d8 <main+0x78>
lea    0xe2e(%rip),%rdi
jmp    11df <main+0x7f>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [9 x i8] c"timeout\0A\00"
@.str.2 = [7 x i8] c"error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i32 5, i32* %6
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %7
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 0, i32* %3)
%10 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %0
%14 = call i32 @read(i32 0, i8* %5, i32 1)
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %26
18: ; preds = %0
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %25
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
br label %26
26: ; preds = %25, %13
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [4 x i8] c"%d\0A\00"
@str = [6 x i8] c"error\00"
@str.3 = [8 x i8] c"timeout\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i8
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
store i32 5, i32* %6
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
store i64 0, i64* %7
%8 = call i32 @FD_ZERO(i32* %2)
%9 = call i32 @FD_SET(i32 0, i32* %2)
%10 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = call i32 @read(i32 0, i8* %3, i32 1)
%14 = load i8, i8* %3
%15 = sext i8 %14 to i32
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %23
17: ; preds = %0
%18 = icmp eq i32 %10, 0
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.3, i64 0, i64 0))
br label %23
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %19, %21, %12
ret i32 0
}