#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 js_event {
  scalar_t__ type;
};

/* Variables and functions */
scalar_t__ JS_EVENT_AXIS;
scalar_t__ JS_EVENT_BUTTON;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, struct js_event *, int);
int /*<<< orphan*/ usleep(int);
int main() {
  struct js_event msg;
  char *device = "/dev/input/js0";
  int fd = open(device, O_RDONLY);
  while (1) {
    if (read(fd, &msg, sizeof(struct js_event)) != sizeof(struct js_event)) {
      printf("Error when reading from joystick\n");
      exit(1);
    } else {
      if (msg.type == JS_EVENT_BUTTON)
        printf("button press\n");
      if (msg.type == JS_EVENT_AXIS)
        printf("axis pressed\n");
      usleep(10000);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
cltq
cmp    $0x8,%rax
je     11b2 <main+0x62>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <main+0x82>
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11f2 <main+0xa2>
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2710,%edi
call   1070 <usleep@plt>
jmp    117f <main+0x2f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1040 <read@plt>
cmp    $0x8,%eax
jne    11e4 <main+0x94>
mov    0x2e3b(%rip),%r13
mov    0x2e4c(%rip),%rbp
lea    0xe74(%rip),%r15
mov    %rsp,%r12
jmp    11be <main+0x6e>
mov    $0x2710,%edi
call   1070 <usleep@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1040 <read@plt>
cmp    $0x8,%eax
jne    11e4 <main+0x94>
mov    (%rsp),%r14
cmp    0x0(%r13),%r14
jne    11d4 <main+0x84>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
cmp    0x0(%rbp),%r14
jne    11a0 <main+0x50>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    11a0 <main+0x50>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.js_event = type { i64 }
@.str = [15 x i8] c"/dev/input/js0\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Error when reading from joystick\0A\00"
@JS_EVENT_BUTTON = global i64 0
@.str.2 = [14 x i8] c"button press\0A\00"
@JS_EVENT_AXIS = global i64 0
@.str.3 = [14 x i8] c"axis pressed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.js_event
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
br label %8
8: ; preds = %0, %32
%9 = load i32, i32* %4
%10 = call i32 @read(i32 %9, %struct.js_event* %2, i32 8)
%11 = sext i32 %10 to i64
%12 = icmp ne i64 %11, 8
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = getelementptr inbounds %struct.js_event, %struct.js_event* %2, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @JS_EVENT_BUTTON
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = getelementptr inbounds %struct.js_event, %struct.js_event* %2, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @JS_EVENT_AXIS
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = call i32 @usleep(i32 10000)
br label %32
32: ; preds = %30
br label %8
}

LLVM IR (-O2):
%struct.js_event = type { i64 }
@.str = [15 x i8] c"/dev/input/js0\00"
@O_RDONLY = global i32 0
@JS_EVENT_BUTTON = global i64 0
@JS_EVENT_AXIS = global i64 0
@str = [13 x i8] c"axis pressed\00"
@str.4 = [13 x i8] c"button press\00"
@str.5 = [33 x i8] c"Error when reading from joystick\00"
define i32 @main() {
%1 = alloca %struct.js_event
%2 = bitcast %struct.js_event* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @read(i32 %4, %struct.js_event* %1, i32 8)
%6 = icmp eq i32 %5, 8
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = getelementptr inbounds %struct.js_event, %struct.js_event* %1, i64 0, i32 0
br label %12
9: ; preds = %23, %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.5, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %7, %23
%13 = load i64, i64* %8
%14 = load i64, i64* @JS_EVENT_BUTTON
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i64, i64* @JS_EVENT_AXIS
%20 = icmp eq i64 %13, %19
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = tail call i32 @usleep(i32 10000)
%25 = call i32 @read(i32 %4, %struct.js_event* %1, i32 8)
%26 = icmp eq i32 %25, 8
br i1 %26, label %12, label %9
}