#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 timespec {
  int /*<<< orphan*/ member_0;
};
struct input_event {
  scalar_t__ type;
  scalar_t__ code;
  int value;
};
typedef int /*<<< orphan*/ ev;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_BOOTTIME;
scalar_t__ EV_KEY;
scalar_t__ KEY_HOME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(struct input_event *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
double timespec_diff(struct timespec *, struct timespec *);
int main(int argc, char **argv) {
  FILE *evf = fopen("/dev/input/event2", "rb");
  if (!evf) {
    fprintf(stderr, "Could not open /dev/input/event2\n");
    return 1;
  }
  struct input_event ev;
  struct timespec press_time = {0};
  bool pressed = false;
  while (1) {
    size_t sz = fread(&ev, sizeof(ev), 1, evf);
    if (sz == 0) {
      return 1;
    }
    if (ev.type == EV_KEY && ev.code == KEY_HOME) {
      if (ev.value == 0 && pressed) {
        struct timespec now;
        clock_gettime(CLOCK_BOOTTIME, &now);
        double t = timespec_diff(&press_time, &now);
        pressed = false;
        if (t >= 3.0) {
          system("systemctl start koreader");
        }
      } else if (ev.value == 1) {
        clock_gettime(CLOCK_BOOTTIME, &press_time);
        pressed = true;
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe7b(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <main+0x59>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x163>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1212 <main+0xa2>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x30(%rbp),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    12ce <main+0x15e>
mov    -0x28(%rbp),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    12ce <main+0x15e>
cmpl   $0x0,-0x20(%rbp)
jne    12a1 <main+0x131>
cmpl   $0x0,-0x3c(%rbp)
je     12a1 <main+0x131>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x38(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <timespec_diff@plt>
movsd  %xmm0,-0x58(%rbp)
movl   $0x0,-0x3c(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  0xd7a(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     129c <main+0x12c>
lea    0xda8(%rip),%rdi
call   1050 <system@plt>
jmp    12c9 <main+0x159>
cmpl   $0x1,-0x20(%rbp)
jne    12c4 <main+0x154>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x1,-0x3c(%rbp)
jmp    12c9 <main+0x159>
jmp    12ce <main+0x15e>
jmp    11e0 <main+0x70>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe93(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1279 <main+0x119>
mov    %rax,%rbx
movl   $0x0,(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     128e <main+0x12e>
xor    %r15d,%r15d
mov    0x2e1a(%rip),%rbp
mov    0x2e1b(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r13
jmp    11fe <main+0x9e>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    $0x18,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     128e <main+0x12e>
mov    0x10(%rsp),%rax
cmp    0x0(%rbp),%rax
jne    11e0 <main+0x80>
mov    0x18(%rsp),%rax
cmp    (%r12),%rax
jne    11e0 <main+0x80>
mov    0x20(%rsp),%eax
test   %eax,%eax
jne    1258 <main+0xf8>
test   %r15d,%r15d
je     1258 <main+0xf8>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1040 <clock_gettime@plt>
mov    %rsp,%rdi
mov    %r14,%rsi
call   1080 <timespec_diff@plt>
ucomisd 0xdbb(%rip),%xmm0
jb     11d1 <main+0x71>
lea    0xdf1(%rip),%rdi
call   1050 <system@plt>
jmp    11d1 <main+0x71>
cmp    $0x1,%eax
jne    11e0 <main+0x80>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
mov    $0x1,%r15d
jmp    11e0 <main+0x80>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i64, i64, i32 }
%struct.timespec = type { i32 }
@.str = [18 x i8] c"/dev/input/event2\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Could not open /dev/input/event2\0A\00"
@EV_KEY = global i64 0
@KEY_HOME = global i64 0
@CLOCK_BOOTTIME = global i32 0
@.str.3 = [25 x i8] c"systemctl start koreader\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.input_event
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.timespec
%12 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %63
19: ; preds = %2
%20 = bitcast %struct.timespec* %8 to i8*
store i32 0, i32* %9
br label %21
21: ; preds = %19, %62
%22 = load i32*, i32** %6
%23 = call i64 @fread(%struct.input_event* %7, i32 24, i32 1, i32* %22)
store i64 %23, i64* %10
%24 = load i64, i64* %10
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 1, i32* %3
br label %63
27: ; preds = %21
%28 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @EV_KEY
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %62
32: ; preds = %27
%33 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%34 = load i64, i64* %33
%35 = load i64, i64* @KEY_HOME
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %62
37: ; preds = %32
%38 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
%39 = load i32, i32* %38
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %37
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %41
%45 = load i32, i32* @CLOCK_BOOTTIME
%46 = call i32 @clock_gettime(i32 %45, %struct.timespec* %11)
%47 = call double @timespec_diff(%struct.timespec* %8, %struct.timespec* %11)
store double %47, double* %12
store i32 0, i32* %9
%48 = load double, double* %12
%49 = fcmp oge double %48, 3.000000e+00
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @system(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %44
br label %61
53: ; preds = %41, %37
%54 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
%55 = load i32, i32* %54
%56 = icmp eq i32 %55, 1
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i32, i32* @CLOCK_BOOTTIME
%59 = call i32 @clock_gettime(i32 %58, %struct.timespec* %8)
store i32 1, i32* %9
br label %60
60: ; preds = %57, %53
br label %61
61: ; preds = %60, %52
br label %62
62: ; preds = %61, %32, %27
br label %21
63: ; preds = %26, %16
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.input_event = type { i64, i64, i32 }
%struct.timespec = type { i32 }
@.str = [18 x i8] c"/dev/input/event2\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Could not open /dev/input/event2\0A\00"
@EV_KEY = global i64 0
@KEY_HOME = global i64 0
@CLOCK_BOOTTIME = global i32 0
@.str.3 = [25 x i8] c"systemctl start koreader\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.input_event
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %54
11: ; preds = %2
%12 = bitcast %struct.input_event* %3 to i8*
%13 = bitcast %struct.timespec* %4 to i8*
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 0, i32* %14
%15 = call i64 @fread(%struct.input_event* %3, i32 24, i32 1, i32* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %53, label %17
17: ; preds = %11
%18 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
%20 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
%21 = bitcast %struct.timespec* %5 to i8*
br label %22
22: ; preds = %17, %49
%23 = phi i32 [ 0, %17 ], [ %50, %49 ]
%24 = load i64, i64* %18
%25 = load i64, i64* @EV_KEY
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = load i64, i64* %19
%29 = load i64, i64* @KEY_HOME
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %49
31: ; preds = %27
%32 = load i32, i32* %20
%33 = icmp eq i32 %32, 0
%34 = icmp ne i32 %23, 0
%35 = select i1 %33, i1 %34, i1 false
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i32, i32* @CLOCK_BOOTTIME
%38 = call i32 @clock_gettime(i32 %37, %struct.timespec* %5)
%39 = call double @timespec_diff(%struct.timespec* %4, %struct.timespec* %5)
%40 = fcmp ult double %39, 3.000000e+00
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = call i32 @system(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
br label %49
44: ; preds = %31
%45 = icmp eq i32 %32, 1
br i1 %45, label %46, label %49
46: ; preds = %44
%47 = load i32, i32* @CLOCK_BOOTTIME
%48 = call i32 @clock_gettime(i32 %47, %struct.timespec* %4)
br label %49
49: ; preds = %22, %27, %44, %46, %43
%50 = phi i32 [ 0, %43 ], [ 1, %46 ], [ %23, %44 ], [ %23, %27 ], [ %23, %22 ]
%51 = call i64 @fread(%struct.input_event* %3, i32 24, i32 1, i32* %6)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %22
53: ; preds = %49, %11
br label %54
54: ; preds = %53, %8
ret i32 1
}