#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 input_event {
  scalar_t__ type;
  int value;
};

/* Variables and functions */
scalar_t__ EV_SW;
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int read(int, struct input_event *, int);
int system(char *);
int main(int argc, char **argv) {
  int fd, rd, i, ret;
  struct input_event ev[64];
  if ((fd = open("/dev/input/event0", O_RDONLY)) < 0) {
    perror("hinged");
    return 1;
  }
  while (1) {
    rd = read(fd, ev, sizeof(struct input_event) * 64);
    for (i = 0; i < rd / sizeof(struct input_event); i++)
      if (ev[i].type == EV_SW) {
        if (ev[i].value == 1) {
          ret = system("/usr/bin/lidclose");
          printf("hinged: Hinge closed, Executed %s, Returned %d\n",
                 "/usr/bin/lidclose", ret);
        } else {
          ret = system("/usr/bin/lidopen");
          printf("Hinge opened, Executed %s, Returned %d\n", "/usr/bin/lidopen",
                 ret);
        }
      }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a4 <main+0x54>
lea    0xe81(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
add    $0x420,%rsp
pop    %rbp
ret
jmp    11a9 <main+0x59>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movslq -0x18(%rbp),%rcx
shr    $0x4,%rcx
cmp    %rcx,%rax
jae    1284 <main+0x134>
movslq -0x1c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <main+0x121>
movslq -0x1c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
cmpl   $0x1,0x8(%rax)
jne    1247 <main+0xf7>
lea    0xdf5(%rip),%rdi
call   1030 <system@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edx
lea    0xdf5(%rip),%rdi
lea    0xddc(%rip),%rsi
call   1040 <printf@plt>
jmp    126c <main+0x11c>
lea    0xe0d(%rip),%rdi
call   1030 <system@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edx
lea    0xe0c(%rip),%rdi
lea    0xdf4(%rip),%rsi
call   1040 <printf@plt>
jmp    1271 <main+0x121>
jmp    1276 <main+0x126>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c7 <main+0x77>
jmp    11a9 <main+0x59>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     1224 <main+0xd4>
mov    0x2e57(%rip),%r14
lea    0xe89(%rip),%rbx
lea    0xec4(%rip),%rbp
lea    0xece(%rip),%r13
xchg   %ax,%ax
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jb     11a0 <main+0x50>
movslq %eax,%r15
shr    $0x4,%r15
cmp    $0x2,%r15
mov    $0x1,%eax
cmovb  %rax,%r15
lea    0x18(%rsp),%r12
jmp    11ff <main+0xaf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1030 <system@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
add    $0x10,%r12
add    $0xffffffffffffffff,%r15
je     11a0 <main+0x50>
mov    -0x8(%r12),%rax
cmp    (%r14),%rax
jne    11f5 <main+0xa5>
cmpl   $0x1,(%r12)
jne    11e0 <main+0x90>
mov    %rbx,%rdi
call   1030 <system@plt>
lea    0xe0c(%rip),%rdi
mov    %rbx,%rsi
jmp    11ee <main+0x9e>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i64, i32 }
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"hinged\00"
@EV_SW = global i64 0
@.str.2 = [18 x i8] c"/usr/bin/lidclose\00"
@.str.3 = [48 x i8] c"hinged: Hinge closed, Executed %s, Returned %d\0A\00"
@.str.4 = [17 x i8] c"/usr/bin/lidopen\00"
@.str.5 = [40 x i8] c"Hinge opened, Executed %s, Returned %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x %struct.input_event]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
16: ; preds = %2
br label %17
17: ; preds = %16, %56
%18 = load i32, i32* %6
%19 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %10, i64 0, i64 0
%20 = call i32 @read(i32 %18, %struct.input_event* %19, i32 1024)
store i32 %20, i32* %7
store i32 0, i32* %8
br label %21
21: ; preds = %53, %17
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = udiv i64 %25, 16
%27 = icmp ult i64 %23, %26
br i1 %27, label %28, label %56
28: ; preds = %21
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %10, i64 0, i64 %30
%32 = getelementptr inbounds %struct.input_event, %struct.input_event* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @EV_SW
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %52
36: ; preds = %28
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %10, i64 0, i64 %38
%40 = getelementptr inbounds %struct.input_event, %struct.input_event* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %51
47: ; preds = %36
%48 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %43
br label %52
52: ; preds = %51, %28
br label %53
53: ; preds = %52
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %21
56: ; preds = %21
br label %17
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32 }
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"hinged\00"
@EV_SW = global i64 0
@.str.2 = [18 x i8] c"/usr/bin/lidclose\00"
@.str.3 = [48 x i8] c"hinged: Hinge closed, Executed %s, Returned %d\0A\00"
@.str.4 = [17 x i8] c"/usr/bin/lidopen\00"
@.str.5 = [40 x i8] c"Hinge opened, Executed %s, Returned %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x %struct.input_event]
%4 = bitcast [64 x %struct.input_event]* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
br label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
12: ; preds = %15, %8
%13 = call i32 @read(i32 %6, %struct.input_event* %9, i32 1024)
%14 = icmp ult i32 %13, 16
br i1 %14, label %15, label %16
15: ; preds = %36, %12
br label %12
16: ; preds = %12
%17 = sext i32 %13 to i64
%18 = lshr i64 %17, 4
%19 = call i64 @llvm.umax.i64(i64 %18, i64 1)
br label %20
20: ; preds = %16, %36
%21 = phi i64 [ 0, %16 ], [ %37, %36 ]
%22 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 %21, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @EV_SW
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 %21, i32 1
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %36
33: ; preds = %26
%34 = tail call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %20, %33, %30
%37 = add nuw nsw i64 %21, 1
%38 = icmp eq i64 %37, %19
br i1 %38, label %15, label %20
}