#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_DATA_TYPE {
  unsigned int buttons;
};
typedef int /*<<< orphan*/ strbuf;

/* Variables and functions */
scalar_t__ JS_RETURN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, struct JS_DATA_TYPE *, scalar_t__);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int fd;
  struct JS_DATA_TYPE js;
  bool err = false;
  bool button_pressed = false;
  char strbuf[255];
  unsigned int timeout_counter = 10;
  if ((fd = open("/dev/input/js0", O_RDONLY)) < 0) {
    perror("Failed to open joystick device");
    err = true;
  }
  while (!err && !button_pressed && (timeout_counter > 0)) {
    if (read(fd, &js, JS_RETURN) != JS_RETURN) {
      perror("Error reading joystick button");
      err = true;
    } else {
      if ((js.buttons & 1u) || (js.buttons & 2u)) {
        button_pressed = true;
      } else {
        usleep(10 * 1000);
        timeout_counter--;
      }
    }
  }
  if (button_pressed) {
    snprintf(strbuf, sizeof(strbuf), "%s %s", "/usr/bin/systemctl start ",
             "emulationstation.service");
  } else {
    snprintf(strbuf, sizeof(strbuf), "%s %s", "/usr/bin/systemctl start ",
             "xbmc.service");
  }
  system(strbuf);
  if (fd >= 0) {
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0xa,-0x124(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d5 <main+0x65>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x1c(%rbp)
jmp    11da <main+0x6a>
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x125(%rbp)
jne    120e <main+0x9e>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x125(%rbp)
jne    120e <main+0x9e>
cmpl   $0x0,-0x124(%rbp)
seta   %al
mov    %al,-0x125(%rbp)
mov    -0x125(%rbp),%al
test   $0x1,%al
jne    1221 <main+0xb1>
jmp    12ac <main+0x13c>
mov    -0x14(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%rdx
lea    -0x18(%rbp),%rsi
call   1060 <read@plt>
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
je     125f <main+0xef>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x1c(%rbp)
jmp    12a7 <main+0x137>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    127d <main+0x10d>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1289 <main+0x119>
movl   $0x1,-0x20(%rbp)
jmp    12a2 <main+0x132>
mov    $0x2710,%edi
call   1090 <usleep@plt>
mov    -0x124(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x124(%rbp)
jmp    12a7 <main+0x137>
jmp    11da <main+0x6a>
cmpl   $0x0,-0x20(%rbp)
je     12e1 <main+0x171>
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
lea    0xd83(%rip),%rdx
lea    0xd82(%rip),%rcx
lea    0xd95(%rip),%r8
call   1040 <snprintf@plt>
jmp    1307 <main+0x197>
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
lea    0xd58(%rip),%rdx
lea    0xd57(%rip),%rcx
lea    0xd83(%rip),%r8
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1030 <system@plt>
cmpl   $0x0,-0x14(%rbp)
jl     1325 <main+0x1b5>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
js     123b <main+0xcb>
mov    $0xfffffff6,%ebp
mov    0x2e3b(%rip),%rbx
lea    0x8(%rsp),%r12
lea    0xe75(%rip),%r15
lea    0xeac(%rip),%r14
mov    (%rbx),%rdx
mov    %r13d,%edi
mov    %r12,%rsi
call   1060 <read@plt>
cmp    (%rbx),%rax
jne    1242 <main+0xd2>
testb  $0x3,0x8(%rsp)
jne    11ef <main+0x7f>
mov    $0x2710,%edi
call   1090 <usleep@plt>
inc    %ebp
jne    11c0 <main+0x50>
lea    0xe96(%rip),%r14
lea    0xe56(%rip),%rdx
lea    0xe55(%rip),%rcx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xff,%esi
mov    %r14,%r8
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <system@plt>
test   %r13d,%r13d
js     1227 <main+0xb7>
mov    %r13d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcd(%rip),%r15
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    11e8 <main+0x78>

LLVM IR (-O0):
%struct.JS_DATA_TYPE = type { i32 }
@.str = [15 x i8] c"/dev/input/js0\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Failed to open joystick device\00"
@JS_RETURN = global i64 0
@.str.2 = [30 x i8] c"Error reading joystick button\00"
@.str.3 = [6 x i8] c"%s %s\00"
@.str.4 = [26 x i8] c"/usr/bin/systemctl start \00"
@.str.5 = [25 x i8] c"emulationstation.service\00"
@.str.6 = [13 x i8] c"xbmc.service\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.JS_DATA_TYPE
%8 = alloca i32
%9 = alloca i32
%10 = alloca [255 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
store i32 10, i32* %11
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %8
br label %17
17: ; preds = %15, %2
br label %18
18: ; preds = %53, %17
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* %11
%26 = icmp ugt i32 %25, 0
br label %27
27: ; preds = %24, %21, %18
%28 = phi i1 [ false, %21 ], [ false, %18 ], [ %26, %24 ]
br i1 %28, label %29, label %54
29: ; preds = %27
%30 = load i32, i32* %6
%31 = load i64, i64* @JS_RETURN
%32 = call i64 @read(i32 %30, %struct.JS_DATA_TYPE* %7, i64 %31)
%33 = load i64, i64* @JS_RETURN
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %8
br label %53
37: ; preds = %29
%38 = getelementptr inbounds %struct.JS_DATA_TYPE, %struct.JS_DATA_TYPE* %7, i32 0, i32 0
%39 = load i32, i32* %38
%40 = and i32 %39, 1
%41 = icmp ne i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = getelementptr inbounds %struct.JS_DATA_TYPE, %struct.JS_DATA_TYPE* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = and i32 %44, 2
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42, %37
store i32 1, i32* %9
br label %52
48: ; preds = %42
%49 = call i32 @usleep(i32 10000)
%50 = load i32, i32* %11
%51 = add i32 %50, -1
store i32 %51, i32* %11
br label %52
52: ; preds = %48, %47
br label %53
53: ; preds = %52, %35
br label %18
54: ; preds = %27
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%59 = call i32 @snprintf(i8* %58, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %63
60: ; preds = %54
%61 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%62 = call i32 @snprintf(i8* %61, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %63
63: ; preds = %60, %57
%64 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%65 = call i32 @system(i8* %64)
%66 = load i32, i32* %6
%67 = icmp sge i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %63
ret i32 0
}

LLVM IR (-O2):
%struct.JS_DATA_TYPE = type { i32 }
@.str = [15 x i8] c"/dev/input/js0\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Failed to open joystick device\00"
@JS_RETURN = global i64 0
@.str.2 = [30 x i8] c"Error reading joystick button\00"
@.str.3 = [6 x i8] c"%s %s\00"
@.str.4 = [26 x i8] c"/usr/bin/systemctl start \00"
@.str.5 = [25 x i8] c"emulationstation.service\00"
@.str.6 = [13 x i8] c"xbmc.service\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.JS_DATA_TYPE
%4 = alloca [255 x i8]
%5 = bitcast %struct.JS_DATA_TYPE* %3 to i8*
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.JS_DATA_TYPE, %struct.JS_DATA_TYPE* %3, i64 0, i32 0
br label %12
12: ; preds = %10, %22
%13 = phi i32 [ 10, %10 ], [ %24, %22 ]
%14 = load i64, i64* @JS_RETURN
%15 = call i64 @read(i32 %8, %struct.JS_DATA_TYPE* %3, i64 %14)
%16 = load i64, i64* @JS_RETURN
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = load i32, i32* %11
%20 = and i32 %19, 3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = tail call i32 @usleep(i32 10000)
%24 = add i32 %13, -1
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %12
26: ; preds = %12, %2
%27 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %12 ]
%28 = tail call i32 @perror(i8* %27)
br label %29
29: ; preds = %22, %18, %26
%30 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), %26 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), %18 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), %22 ]
%31 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = call i32 @system(i8* %6)
%33 = icmp sgt i32 %8, -1
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @close(i32 %8)
br label %36
36: ; preds = %34, %29
ret i32 0
}