#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 {
  long member_0;
  long member_1;
};
struct msgqueue_t {
  int dummy;
};
typedef int /*<<< orphan*/ inputname;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ EVIOCSREP;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int get_input_key(int);
int getchar();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ msgqueue_add(struct msgqueue_t *, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
void input_thread(struct msgqueue_t *msgqueue) {
  int inputfd;
  char inputname[256] = "Unknown";
  char *inputdevice = "/dev/input/event0";
  int c;
  if ((inputfd = open(inputdevice, O_RDONLY)) >= 0) {
    ioctl(inputfd, EVIOCGNAME(sizeof(inputname)), inputname);
    fprintf(stderr, "Using %s - %s\n", inputdevice, inputname);
    int ioctl_params[2] = {0, 0};
    ioctl(inputfd, EVIOCSREP, ioctl_params);
  }
  while (1) {
    struct timeval tv = {0L, 100000L};
    fd_set fds;
    int maxfd = 0;
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    if (inputfd >= 0) {
      FD_SET(inputfd, &fds);
      maxfd = inputfd;
    }
    c = -1;
    if (select(maxfd + 1, &fds, NULL, NULL, &tv) == 0) {
      c = -1;
    } else {
      if (FD_ISSET(0, &fds)) {
        c = getchar();
      }
      if ((inputfd >= 0) && (FD_ISSET(inputfd, &fds))) {
        c = get_input_key(inputfd);
      }
    }
    if (c != -1) {
      msgqueue_add(msgqueue, c);
    }
  }
}


x86 assembly (clang, -O0):
<input_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xe13(%rip),%rsi
mov    $0x100,%edx
call   1080 <memcpy@plt>
lea    0xf02(%rip),%rax
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     129f <input_thread+0xcf>
mov    -0xc(%rbp),%eax
mov    %eax,-0x144(%rbp)
mov    $0x100,%edi
call   10d0 <EVIOCGNAME@plt>
mov    -0x144(%rbp),%edi
mov    %eax,%esi
lea    -0x110(%rbp),%rdx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xea5(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x124(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    -0x124(%rbp),%rdx
mov    $0x0,%al
call   1050 <ioctl@plt>
jmp    12a4 <input_thread+0xd4>
mov    0xe7d(%rip),%rax
mov    %rax,-0x138(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x130(%rbp)
movl   $0x0,-0x140(%rbp)
lea    -0x13c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x13c(%rbp),%rsi
call   10e0 <FD_SET@plt>
cmpl   $0x0,-0xc(%rbp)
jl     1306 <input_thread+0x136>
mov    -0xc(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x140(%rbp)
movl   $0xffffffff,-0x11c(%rbp)
ff ff ff
mov    -0x140(%rbp),%edi
add    $0x1,%edi
lea    -0x13c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x138(%rbp),%r8
mov    %rcx,%rdx
call   10a0 <select@plt>
cmp    $0x0,%rax
jne    134c <input_thread+0x17c>
movl   $0xffffffff,-0x11c(%rbp)
ff ff ff
jmp    13a7 <input_thread+0x1d7>
xor    %edi,%edi
lea    -0x13c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1371 <input_thread+0x1a1>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     13a2 <input_thread+0x1d2>
mov    -0xc(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13a2 <input_thread+0x1d2>
mov    -0xc(%rbp),%edi
call   10f0 <get_input_key@plt>
mov    %eax,-0x11c(%rbp)
jmp    13a7 <input_thread+0x1d7>
cmpl   $0xffffffff,-0x11c(%rbp)
je     13c3 <input_thread+0x1f3>
mov    -0x8(%rbp),%rdi
mov    -0x11c(%rbp),%esi
call   10b0 <msgqueue_add@plt>
jmp    12a4 <input_thread+0xd4>

x86 assembly (clang, -O2):
<input_thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%r14
lea    0xe27(%rip),%rsi
lea    0x20(%rsp),%rdi
mov    $0x100,%edx
call   1070 <memcpy@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0xf08(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     125c <input_thread+0x9c>
mov    $0x100,%edi
call   10c0 <EVIOCGNAME@plt>
lea    0x20(%rsp),%rbx
mov    %ebp,%edi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xee2(%rip),%rsi
lea    0xec9(%rip),%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
movq   $0x0,(%rsp)
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
lea    0x1c(%rsp),%r12
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movups 0xeb1(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %r12,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   10d0 <FD_SET@plt>
mov    $0x0,%edi
test   %ebp,%ebp
js     12a2 <input_thread+0xe2>
mov    %ebp,%edi
mov    %r12,%rsi
call   10d0 <FD_SET@plt>
mov    %ebp,%edi
add    $0x1,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1090 <select@plt>
test   %rax,%rax
je     1270 <input_thread+0xb0>
xor    %edi,%edi
mov    %r12,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12e0 <input_thread+0x120>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
test   %ebp,%ebp
jns    12e9 <input_thread+0x129>
jmp    1301 <input_thread+0x141>
nopw   0x0(%rax,%rax,1)
mov    $0xffffffff,%ebx
test   %ebp,%ebp
js     1301 <input_thread+0x141>
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1301 <input_thread+0x141>
mov    %ebp,%edi
call   10e0 <get_input_key@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%ebx
je     1270 <input_thread+0xb0>
mov    %r14,%rdi
mov    %ebx,%esi
call   10a0 <msgqueue_add@plt>
jmp    1270 <input_thread+0xb0>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.msgqueue_t = type { i32 }
@__const.input_thread.inputname = [256 x i8] c"Unknown\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"Using %s - %s\0A\00"
@EVIOCSREP = global i32 0
@__const.input_thread.tv = %struct.timeval { i64 0, i64 100000 }
define void @input_thread(%struct.msgqueue_t* %0) {
%2 = alloca %struct.msgqueue_t*
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
store %struct.msgqueue_t* %0, %struct.msgqueue_t** %2
%11 = bitcast [256 x i8]* %4 to i8*
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %5
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %3
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %1
%17 = load i32, i32* %3
%18 = call i32 @EVIOCGNAME(i32 256)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%20 = call i32 (i32, i32, ...) @ioctl(i32 %17, i32 %18, i8* %19)
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %5
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %23)
%25 = bitcast [2 x i32]* %7 to i8*
%26 = load i32, i32* %3
%27 = load i32, i32* @EVIOCSREP
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%29 = call i32 (i32, i32, ...) @ioctl(i32 %26, i32 %27, i32* %28)
br label %30
30: ; preds = %16, %1
br label %31
31: ; preds = %30, %70
%32 = bitcast %struct.timeval* %8 to i8*
store i32 0, i32* %10
%33 = call i32 @FD_ZERO(i32* %9)
%34 = call i32 @FD_SET(i32 0, i32* %9)
%35 = load i32, i32* %3
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i32, i32* %3
%39 = call i32 @FD_SET(i32 %38, i32* %9)
%40 = load i32, i32* %3
store i32 %40, i32* %10
br label %41
41: ; preds = %37, %31
store i32 -1, i32* %6
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
%44 = call i64 @select(i32 %43, i32* %9, i32* null, i32* null, %struct.timeval* %8)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %6
br label %63
47: ; preds = %41
%48 = call i64 @FD_ISSET(i32 0, i32* %9)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 (...) @getchar()
store i32 %51, i32* %6
br label %52
52: ; preds = %50, %47
%53 = load i32, i32* %3
%54 = icmp sge i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %52
%56 = load i32, i32* %3
%57 = call i64 @FD_ISSET(i32 %56, i32* %9)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %3
%61 = call i32 @get_input_key(i32 %60)
store i32 %61, i32* %6
br label %62
62: ; preds = %59, %55, %52
br label %63
63: ; preds = %62, %46
%64 = load i32, i32* %6
%65 = icmp ne i32 %64, -1
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load %struct.msgqueue_t*, %struct.msgqueue_t** %2
%68 = load i32, i32* %6
%69 = call i32 @msgqueue_add(%struct.msgqueue_t* %67, i32 %68)
br label %70
70: ; preds = %66, %63
br label %31
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.msgqueue_t = type { i32 }
@__const.input_thread.inputname = [256 x i8] c"Unknown\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"Using %s - %s\0A\00"
@EVIOCSREP = global i32 0
@__const.input_thread.tv = %struct.timeval { i64 0, i64 100000 }
define void @input_thread(%struct.msgqueue_t* %0) {
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = tail call i32 @EVIOCGNAME(i32 256)
%12 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %11, i8* %6)
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %6)
%15 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%16 = load i32, i32* @EVIOCSREP
%17 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %16, i64* %3)
br label %18
18: ; preds = %10, %1
%19 = bitcast %struct.timeval* %4 to i8*
%20 = bitcast i32* %5 to i8*
br label %21
21: ; preds = %48, %18
%22 = call i32 @FD_ZERO(i32* %5)
%23 = call i32 @FD_SET(i32 0, i32* %5)
br i1 %9, label %24, label %26
24: ; preds = %21
%25 = call i32 @FD_SET(i32 %8, i32* %5)
br label %26
26: ; preds = %24, %21
%27 = phi i32 [ %8, %24 ], [ 0, %21 ]
%28 = add nuw nsw i32 %27, 1
%29 = call i64 @select(i32 %28, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %48, label %31
31: ; preds = %26
%32 = call i64 @FD_ISSET(i32 0, i32* %5)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 (...) @getchar()
br label %36
36: ; preds = %34, %31
%37 = phi i32 [ %35, %34 ], [ -1, %31 ]
br i1 %9, label %38, label %43
38: ; preds = %36
%39 = call i64 @FD_ISSET(i32 %8, i32* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @get_input_key(i32 %8)
br label %43
43: ; preds = %36, %38, %41
%44 = phi i32 [ %42, %41 ], [ %37, %38 ], [ %37, %36 ]
%45 = icmp eq i32 %44, -1
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @msgqueue_add(%struct.msgqueue_t* %0, i32 %44)
br label %48
48: ; preds = %26, %46, %43
br label %21
}