#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int read(int, struct input_event *, int);
int main() {
  int fd;
  struct input_event ie;
  unsigned char button, bLeft, bMiddle, bRight;
  char x, y;
  int absolute_x, absolute_y;
  if ((fd = open("/dev/input/mouse0", O_RDONLY | O_NONBLOCK)) == -1) {
    printf("Device open ERROR\n");
    exit(EXIT_FAILURE);
  } else {
    printf("Device open OK\n");
  }
  int ret, xp = 0, yp = 0;
  printf("right-click to set absolute x,y coordinates origin (0,0)\n");
  int ptr = 0;
  while (1) {
    ret = read(fd, &ie, sizeof(struct input_event));
    if (ret != -1) {
      unsigned char *ptr = (unsigned char *)&ie;
      int i;
      button = ptr[0];
      bLeft = button & 0x1;
      bMiddle = (button & 0x4) > 0;
      bRight = (button & 0x2) > 0;
      x = (char)ptr[1];
      y = (char)ptr[2];
      if (x > 0)
        xp++;
      else if (x < 0)
        xp--;
      if (y > 0)
        yp--;
      else if (y < 0)
        yp++;
      if (xp == 5) {
        xp = 0;
        printf("x DERECHA");
      } else if (xp == -5) {
        xp = 0;
        printf("x IZQUIERDA");
      }
      if (yp == 5) {
        yp = 0;
        printf("x ARRIBA");
      } else if (yp == -5) {
        yp = 0;
        printf("x ABAJO");
      }
      absolute_x += x;
      absolute_y -= y;
      if (bRight == 1) {
        absolute_x = 0;
        absolute_y = 0;
        printf("Absolute x,y coords origin recorded\n");
      }
      printf("\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1193 <main+0x53>
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     1374 <main+0x234>
lea    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x1,%eax
mov    %al,-0x12(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x13(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x14(%rbp)
mov    -0x38(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x15(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x16(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jle    1252 <main+0x112>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    126d <main+0x12d>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jge    1268 <main+0x128>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    126d <main+0x12d>
movsbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jle    1288 <main+0x148>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    12a3 <main+0x163>
movsbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jge    129e <main+0x15e>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12a3 <main+0x163>
cmpl   $0x5,-0x28(%rbp)
jne    12c5 <main+0x185>
movl   $0x0,-0x28(%rbp)
lea    0xdb4(%rip),%rdi
call   1030 <printf@plt>
jmp    12e7 <main+0x1a7>
cmpl   $0xfffffffb,-0x28(%rbp)
jne    12e2 <main+0x1a2>
movl   $0x0,-0x28(%rbp)
lea    0xd9c(%rip),%rdi
call   1030 <printf@plt>
jmp    12e7 <main+0x1a7>
cmpl   $0x5,-0x2c(%rbp)
jne    1309 <main+0x1c9>
movl   $0x0,-0x2c(%rbp)
lea    0xd86(%rip),%rdi
call   1030 <printf@plt>
jmp    132b <main+0x1eb>
cmpl   $0xfffffffb,-0x2c(%rbp)
jne    1326 <main+0x1e6>
movl   $0x0,-0x2c(%rbp)
lea    0xd6d(%rip),%rdi
call   1030 <printf@plt>
jmp    132b <main+0x1eb>
movsbl -0x15(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movsbl -0x16(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
movzbl -0x14(%rbp),%eax
cmp    $0x1,%eax
jne    1368 <main+0x228>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xd33(%rip),%rdi
call   1030 <printf@plt>
lea    0xcfe(%rip),%rdi
call   1030 <printf@plt>
jmp    11c0 <main+0x80>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     127e <main+0x11e>
mov    %eax,%ebx
lea    0xe9e(%rip),%rdi
call   1040 <puts@plt>
lea    0xea1(%rip),%rdi
call   1040 <puts@plt>
xor    %r15d,%r15d
mov    %rsp,%r14
xor    %r12d,%r12d
jmp    11d0 <main+0x70>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11d0 <main+0x70>
movzbl (%rsp),%ebp
movzbl 0x1(%rsp),%eax
mov    %eax,%ecx
sar    $0x7,%cl
test   %al,%al
movzbl %cl,%eax
mov    $0x1,%ecx
cmovg  %ecx,%eax
movzbl 0x2(%rsp),%ecx
movsbl %al,%eax
add    %eax,%r12d
mov    %ecx,%r13d
shr    $0x7,%r13d
test   %cl,%cl
mov    $0xffffffff,%eax
cmovg  %eax,%r13d
lea    0xdef(%rip),%rdi
cmp    $0x5,%r12d
je     1236 <main+0xd6>
cmp    $0xfffffffb,%r12d
jne    123e <main+0xde>
lea    0xde6(%rip),%rdi
call   1050 <printf@plt>
xor    %r12d,%r12d
add    %r15d,%r13d
lea    0xde0(%rip),%rdi
cmp    $0x5,%r13d
je     125b <main+0xfb>
cmp    $0xfffffffb,%r13d
jne    1263 <main+0x103>
lea    0xdd6(%rip),%rdi
call   1050 <printf@plt>
xor    %r13d,%r13d
test   $0x2,%bpl
je     11b7 <main+0x57>
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
jmp    11b7 <main+0x57>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.input_event = type { i32 }
@.str = [18 x i8] c"/dev/input/mouse0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"Device open ERROR\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"Device open OK\0A\00"
@.str.3 = [58 x i8] c"right-click to set absolute x,y coordinates origin (0,0)\0A\00"
@.str.4 = [10 x i8] c"x DERECHA\00"
@.str.5 = [12 x i8] c"x IZQUIERDA\00"
@.str.6 = [9 x i8] c"x ARRIBA\00"
@.str.7 = [8 x i8] c"x ABAJO\00"
@.str.8 = [37 x i8] c"Absolute x,y coords origin recorded\0A\00"
@.str.9 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.input_event
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
store i32 0, i32* %1
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %2
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %0
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %0
%28 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27
store i32 0, i32* %13
store i32 0, i32* %14
%30 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %15
br label %31
31: ; preds = %29, %130
%32 = load i32, i32* %2
%33 = call i32 @read(i32 %32, %struct.input_event* %3, i32 4)
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %130
36: ; preds = %31
%37 = bitcast %struct.input_event* %3 to i8*
store i8* %37, i8** %16
%38 = load i8*, i8** %16
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
store i8 %40, i8* %4
%41 = load i8, i8* %4
%42 = zext i8 %41 to i32
%43 = and i32 %42, 1
%44 = trunc i32 %43 to i8
store i8 %44, i8* %5
%45 = load i8, i8* %4
%46 = zext i8 %45 to i32
%47 = and i32 %46, 4
%48 = icmp sgt i32 %47, 0
%49 = zext i1 %48 to i32
%50 = trunc i32 %49 to i8
store i8 %50, i8* %6
%51 = load i8, i8* %4
%52 = zext i8 %51 to i32
%53 = and i32 %52, 2
%54 = icmp sgt i32 %53, 0
%55 = zext i1 %54 to i32
%56 = trunc i32 %55 to i8
store i8 %56, i8* %7
%57 = load i8*, i8** %16
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = load i8, i8* %58
store i8 %59, i8* %8
%60 = load i8*, i8** %16
%61 = getelementptr inbounds i8, i8* %60, i64 2
%62 = load i8, i8* %61
store i8 %62, i8* %9
%63 = load i8, i8* %8
%64 = sext i8 %63 to i32
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %36
%67 = load i32, i32* %13
%68 = add nsw i32 %67, 1
store i32 %68, i32* %13
br label %77
69: ; preds = %36
%70 = load i8, i8* %8
%71 = sext i8 %70 to i32
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i32, i32* %13
%75 = add nsw i32 %74, -1
store i32 %75, i32* %13
br label %76
76: ; preds = %73, %69
br label %77
77: ; preds = %76, %66
%78 = load i8, i8* %9
%79 = sext i8 %78 to i32
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %77
%82 = load i32, i32* %14
%83 = add nsw i32 %82, -1
store i32 %83, i32* %14
br label %92
84: ; preds = %77
%85 = load i8, i8* %9
%86 = sext i8 %85 to i32
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %84
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %91
91: ; preds = %88, %84
br label %92
92: ; preds = %91, %81
%93 = load i32, i32* %13
%94 = icmp eq i32 %93, 5
br i1 %94, label %95, label %97
95: ; preds = %92
store i32 0, i32* %13
%96 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %103
97: ; preds = %92
%98 = load i32, i32* %13
%99 = icmp eq i32 %98, -5
br i1 %99, label %100, label %102
100: ; preds = %97
store i32 0, i32* %13
%101 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %102
102: ; preds = %100, %97
br label %103
103: ; preds = %102, %95
%104 = load i32, i32* %14
%105 = icmp eq i32 %104, 5
br i1 %105, label %106, label %108
106: ; preds = %103
store i32 0, i32* %14
%107 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %114
108: ; preds = %103
%109 = load i32, i32* %14
%110 = icmp eq i32 %109, -5
br i1 %110, label %111, label %113
111: ; preds = %108
store i32 0, i32* %14
%112 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %113
113: ; preds = %111, %108
br label %114
114: ; preds = %113, %106
%115 = load i8, i8* %8
%116 = sext i8 %115 to i32
%117 = load i32, i32* %10
%118 = add nsw i32 %117, %116
store i32 %118, i32* %10
%119 = load i8, i8* %9
%120 = sext i8 %119 to i32
%121 = load i32, i32* %11
%122 = sub nsw i32 %121, %120
store i32 %122, i32* %11
%123 = load i8, i8* %7
%124 = zext i8 %123 to i32
%125 = icmp eq i32 %124, 1
br i1 %125, label %126, label %128
126: ; preds = %114
store i32 0, i32* %10
store i32 0, i32* %11
%127 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
br label %128
128: ; preds = %126, %114
%129 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %130
130: ; preds = %128, %31
br label %31
}

LLVM IR (-O2):
%struct.input_event = type { i32 }
@.str = [18 x i8] c"/dev/input/mouse0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [10 x i8] c"x DERECHA\00"
@.str.5 = [12 x i8] c"x IZQUIERDA\00"
@.str.6 = [9 x i8] c"x ARRIBA\00"
@.str.7 = [8 x i8] c"x ABAJO\00"
@str = [15 x i8] c"Device open OK\00"
@str.10 = [57 x i8] c"right-click to set absolute x,y coordinates origin (0,0)\00"
@str.11 = [36 x i8] c"Absolute x,y coords origin recorded\00"
@str.12 = [18 x i8] c"Device open ERROR\00"
define i32 @main() {
%1 = alloca %struct.input_event
%2 = bitcast %struct.input_event* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.12, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.10, i64 0, i64 0))
%15 = getelementptr inbounds i8, i8* %2, i64 1
%16 = getelementptr inbounds i8, i8* %2, i64 2
br label %17
17: ; preds = %53, %12
%18 = phi i32 [ %48, %53 ], [ 0, %12 ]
%19 = phi i32 [ %42, %53 ], [ 0, %12 ]
br label %20
20: ; preds = %17, %20
%21 = call i32 @read(i32 %6, %struct.input_event* %1, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %20, label %23
23: ; preds = %20
%24 = load i8, i8* %2
%25 = load i8, i8* %15
%26 = load i8, i8* %16
%27 = ashr i8 %25, 7
%28 = icmp slt i8 %25, 1
%29 = select i1 %28, i8 %27, i8 1
%30 = sext i8 %29 to i32
%31 = add nsw i32 %19, %30
%32 = icmp sgt i8 %26, 0
%33 = lshr i8 %26, 7
%34 = zext i8 %33 to i32
%35 = select i1 %32, i32 -1, i32 %34
%36 = add nsw i32 %35, %18
switch i32 %31, label %41 [
i32 5, label %38
i32 -5, label %37
]
37: ; preds = %23
br label %38
38: ; preds = %23, %37
%39 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), %37 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %23 ]
%40 = tail call i32 @printf(i8* dereferenceable(1) %39)
br label %41
41: ; preds = %38, %23
%42 = phi i32 [ %31, %23 ], [ 0, %38 ]
switch i32 %36, label %47 [
i32 5, label %44
i32 -5, label %43
]
43: ; preds = %41
br label %44
44: ; preds = %41, %43
%45 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), %43 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), %41 ]
%46 = tail call i32 @printf(i8* dereferenceable(1) %45)
br label %47
47: ; preds = %44, %41
%48 = phi i32 [ %36, %41 ], [ 0, %44 ]
%49 = and i8 %24, 2
%50 = icmp eq i8 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.11, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = tail call i32 @putchar(i32 10)
br label %17
}