#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct input_event {
  int /*<<< orphan*/ value;
  int /*<<< orphan*/ code;
  int /*<<< orphan*/ type;
  TYPE_1__ time;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, struct input_event *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main() {
  int fd;
  struct input_event ie;
  int read_out;
  int time_out = 0;
  int HDMI_ON = 1;
  if ((fd = open("/dev/input/mice", O_RDONLY)) == -1) {
    perror(" ERROR: opening device failed\n");
    exit(EXIT_FAILURE);
  }
  int flags = fcntl(fd, F_GETFL, 0);
  fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  while (1) {
    read_out = read(fd, &ie, sizeof(struct input_event));
    if (read_out == 3) {
      printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n", ie.time.tv_sec,
             ie.time.tv_usec, ie.type, ie.code, ie.value);
      time_out = 0;
      if (HDMI_ON == 0) {
        system("/opt/vc/bin/tvservice -p");
        system("sudo xrefresh");
      }
      HDMI_ON = 1;
    } else {
      time_out++;
      if (time_out > 30) {
        printf("sleep now..\n");
        printf("power down  HDMI \n");
        system("/opt/vc/bin/tvservice -o");
        HDMI_ON = 0;
      }
    }
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11d8 <main+0x58>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x14,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jne    127d <main+0xfd>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1271 <main+0xf1>
lea    0xdf8(%rip),%rdi
call   1040 <system@plt>
lea    0xe05(%rip),%rdi
call   1040 <system@plt>
movl   $0x1,-0x2c(%rbp)
jmp    12c4 <main+0x144>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x1e,-0x28(%rbp)
jle    12bf <main+0x13f>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdec(%rip),%rdi
call   1040 <system@plt>
movl   $0x0,-0x2c(%rbp)
jmp    12c4 <main+0x144>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    120b <main+0x8b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     129e <main+0x10e>
mov    %eax,%ebx
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2df6(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    $0x1,%r13d
mov    %rsp,%r14
lea    0xe31(%rip),%r15
jmp    1239 <main+0xa9>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    (%rsp),%r9d
mov    0x4(%rsp),%r8d
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r13d,%r13d
mov    $0x1,%r13d
je     127c <main+0xec>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %ebp,%r12d
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1070 <read@plt>
cmp    $0x3,%eax
je     1200 <main+0x70>
lea    0x1(%r12),%ebp
cmp    $0x1e,%r12d
jl     122c <main+0x9c>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
xor    %r13d,%r13d
lea    0xe05(%rip),%rdi
jmp    1297 <main+0x107>
lea    0xdd5(%rip),%rdi
call   1050 <system@plt>
xor    %ebp,%ebp
mov    $0x1,%r13d
lea    0xdda(%rip),%rdi
call   1050 <system@plt>
jmp    122c <main+0x9c>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@.str = [16 x i8] c"/dev/input/mice\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c" ERROR: opening device failed\0A\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [41 x i8] c"time %ld.%06ld\09type %d\09code %d\09value %d\0A\00"
@.str.3 = [25 x i8] c"/opt/vc/bin/tvservice -p\00"
@.str.4 = [14 x i8] c"sudo xrefresh\00"
@.str.5 = [13 x i8] c"sleep now..\0A\00"
@.str.6 = [19 x i8] c"power down HDMI \0A\00"
@.str.7 = [25 x i8] c"/opt/vc/bin/tvservice -o\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.input_event
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
store i32 1, i32* %6
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %7
%19 = load i32, i32* %2
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* %7
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @fcntl(i32 %19, i32 %20, i32 %23)
br label %25
25: ; preds = %15, %60
%26 = load i32, i32* %2
%27 = call i32 @read(i32 %26, %struct.input_event* %3, i32 20)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %50
30: ; preds = %25
%31 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i32 0, i32 3
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i32 0, i32 3
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i32 0, i32 2
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i32 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %36, i32 %38, i32 %40, i32 %42)
store i32 0, i32* %5
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %30
%47 = call i32 @system(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %46, %30
store i32 1, i32* %6
br label %60
50: ; preds = %25
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
%53 = load i32, i32* %5
%54 = icmp sgt i32 %53, 30
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%58 = call i32 @system(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %6
br label %59
59: ; preds = %55, %50
br label %60
60: ; preds = %59, %49
%61 = call i32 @sleep(i32 1)
br label %25
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@.str = [16 x i8] c"/dev/input/mice\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c" ERROR: opening device failed\0A\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [41 x i8] c"time %ld.%06ld\09type %d\09code %d\09value %d\0A\00"
@.str.3 = [25 x i8] c"/opt/vc/bin/tvservice -p\00"
@.str.4 = [14 x i8] c"sudo xrefresh\00"
@.str.7 = [25 x i8] c"/opt/vc/bin/tvservice -o\00"
@str = [12 x i8] c"sleep now..\00"
@str.8 = [18 x i8] c"power down HDMI \00"
define i32 @main() {
%1 = alloca %struct.input_event
%2 = bitcast %struct.input_event* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 @fcntl(i32 %4, i32 %11, i32 0)
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %14, %12
%16 = tail call i32 @fcntl(i32 %4, i32 %13, i32 %15)
%17 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 3, i32 1
%18 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 3, i32 0
%19 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 2
%20 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 1
%21 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 0
br label %22
22: ; preds = %48, %10
%23 = phi i32 [ 1, %10 ], [ %49, %48 ]
%24 = phi i32 [ 0, %10 ], [ %50, %48 ]
%25 = call i32 @read(i32 %4, %struct.input_event* %1, i32 20)
%26 = icmp eq i32 %25, 3
br i1 %26, label %27, label %37
27: ; preds = %22
%28 = load i32, i32* %17
%29 = load i32, i32* %18
%30 = load i32, i32* %19
%31 = load i32, i32* %20
%32 = load i32, i32* %21
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29, i32 %30, i32 %31, i32 %32)
%34 = icmp eq i32 %23, 0
br i1 %34, label %35, label %48
35: ; preds = %27
%36 = tail call i32 @system(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %43
37: ; preds = %22
%38 = add nsw i32 %24, 1
%39 = icmp sgt i32 %24, 29
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %43
43: ; preds = %40, %35
%44 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), %35 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), %40 ]
%45 = phi i32 [ 1, %35 ], [ 0, %40 ]
%46 = phi i32 [ 0, %35 ], [ %38, %40 ]
%47 = tail call i32 @system(i8* %44)
br label %48
48: ; preds = %43, %27, %37
%49 = phi i32 [ %23, %37 ], [ 1, %27 ], [ %45, %43 ]
%50 = phi i32 [ %38, %37 ], [ 0, %27 ], [ %46, %43 ]
%51 = tail call i32 @sleep(i32 1)
br label %22
}