#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;
};
struct termios {
  int /*<<< orphan*/ c_lflag;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ ECHO;
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char *argv[]) {
  fd_set rfds;
  char buf[1024];
  struct termios termios_save;
  struct termios termios_p;
  struct timeval tv;
  int timeout = -1;
  int ret;
  if (argc == 2)
    timeout = strtol(argv[1], NULL, 10);
  if (timeout < 0) {
    fprintf(stderr,
            "Usage: %s TIME\n"
            "\treturn 1, if any key is pressed within the given time\n"
            "\treturn 0, if timeout occurs and no key is pressed\n",
            argv[0]);
    return -1;
  }
  tcgetattr(0, &termios_save);
  memcpy(&termios_p, &termios_save, sizeof(struct termios));
  termios_p.c_lflag &= ~ECHO;
  termios_p.c_lflag &= ~ICANON;
  tcsetattr(0, 0, &termios_p);
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  ret = select(1, &rfds, NULL, NULL, &tv);
  if (ret == 1 && FD_ISSET(0, &rfds))
    read(0, buf, sizeof(buf));
  tcsetattr(0, 0, &termios_save);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x444(%rbp)
ff ff ff
cmpl   $0x2,-0x8(%rbp)
jne    11e9 <main+0x49>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jge    121e <main+0x7e>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf3(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <main+0x17f>
xor    %edi,%edi
lea    -0x428(%rbp),%rsi
call   10a0 <tcgetattr@plt>
lea    -0x430(%rbp),%rdi
lea    -0x428(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x430(%rbp),%eax
mov    %eax,-0x430(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x430(%rbp),%eax
mov    %eax,-0x430(%rbp)
xor    %esi,%esi
lea    -0x430(%rbp),%rdx
mov    %esi,%edi
call   10b0 <tcsetattr@plt>
lea    -0x14(%rbp),%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x444(%rbp),%eax
mov    %eax,-0x440(%rbp)
movq   $0x0,-0x438(%rbp)
mov    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x440(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x448(%rbp)
cmpl   $0x1,-0x448(%rbp)
jne    1306 <main+0x166>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1306 <main+0x166>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1040 <read@plt>
xor    %esi,%esi
lea    -0x428(%rbp),%rdx
mov    %esi,%edi
call   10b0 <tcsetattr@plt>
mov    -0x448(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1289 <main+0xe9>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
test   %eax,%eax
js     1289 <main+0xe9>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <tcgetattr@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    0x2dcd(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,(%rsp)
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   10b0 <tcsetattr@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    %ebp,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
mov    %eax,%ebx
cmp    $0x1,%eax
jne    1279 <main+0xd9>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1279 <main+0xd9>
lea    0x20(%rsp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1040 <read@plt>
lea    0x8(%rsp),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   10b0 <tcsetattr@plt>
jmp    12a6 <main+0x106>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd64(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [122 x i8] c"Usage: %s TIME\0A\09return 1, if any key is pressed within the given time\0A\09return 0, if timeout occurs and no key is pressed\0A\00"
@ECHO = global i32 0
@ICANON = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %11
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strtol(i8* %18, i32* null, i32 10)
store i32 %19, i32* %11
br label %20
20: ; preds = %15, %2
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([122 x i8], [122 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %60
29: ; preds = %20
%30 = call i32 @tcgetattr(i32 0, %struct.termios* %8)
%31 = call i32 @memcpy(%struct.termios* %9, %struct.termios* %8, i32 4)
%32 = load i32, i32* @ECHO
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* @ICANON
%38 = xor i32 %37, -1
%39 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = and i32 %40, %38
store i32 %41, i32* %39
%42 = call i32 @tcsetattr(i32 0, i32 0, %struct.termios* %9)
%43 = call i32 @FD_ZERO(i32* %6)
%44 = call i32 @FD_SET(i32 0, i32* %6)
%45 = load i32, i32* %11
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %47
%48 = call i32 @select(i32 1, i32* %6, i32* null, i32* null, %struct.timeval* %10)
store i32 %48, i32* %12
%49 = load i32, i32* %12
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %57
51: ; preds = %29
%52 = call i64 @FD_ISSET(i32 0, i32* %6)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%56 = call i32 @read(i32 0, i8* %55, i32 1024)
br label %57
57: ; preds = %54, %51, %29
%58 = call i32 @tcsetattr(i32 0, i32 0, %struct.termios* %8)
%59 = load i32, i32* %12
store i32 %59, i32* %3
br label %60
60: ; preds = %57, %23
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [122 x i8] c"Usage: %s TIME\0A\09return 1, if any key is pressed within the given time\0A\09return 0, if timeout occurs and no key is pressed\0A\00"
@ECHO = global i32 0
@ICANON = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = alloca %struct.timeval
%8 = bitcast i32* %3 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.termios* %5 to i8*
%11 = bitcast %struct.termios* %6 to i8*
%12 = bitcast %struct.timeval* %7 to i8*
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @strtol(i8* nocapture %16, i32* null, i32 10)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2, %14
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %1
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([122 x i8], [122 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %47
23: ; preds = %14
%24 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%25 = call i32 @memcpy(%struct.termios* %6, %struct.termios* %5, i32 4)
%26 = load i32, i32* @ECHO
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @ICANON
%30 = or i32 %26, %29
%31 = xor i32 %30, -1
%32 = and i32 %28, %31
store i32 %32, i32* %27
%33 = call i32 @tcsetattr(i32 0, i32 0, %struct.termios* %6)
%34 = call i32 @FD_ZERO(i32* %3)
%35 = call i32 @FD_SET(i32 0, i32* %3)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %17, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %37
%38 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %7)
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %45
40: ; preds = %23
%41 = call i64 @FD_ISSET(i32 0, i32* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = call i32 @read(i32 0, i8* %9, i32 1024)
br label %45
45: ; preds = %43, %40, %23
%46 = call i32 @tcsetattr(i32 0, i32 0, %struct.termios* %5)
br label %47
47: ; preds = %45, %19
%48 = phi i32 [ -1, %19 ], [ %38, %45 ]
ret i32 %48
}