#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, char, int, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ time(int /*<<< orphan*/ *);
int main() {
  int x = 683, y = 384;
  FILE *input = fopen("/dev/input/mouse0", "r");
  FILE *log = fopen("/var/log/mouse_log", "a");
  signed char char_num;
  signed char protocol[3];
  while (input) {
    for (char_num = 0; char_num < 3; char_num++) {
      protocol[char_num] = fgetc(input);
    }
    x += protocol[1];
    if (x > 1366)
      x = 1366;
    if (x < 0)
      x = 0;
    y -= protocol[2];
    if (y > 768)
      y = 768;
    if (y < 0)
      y = 0;
    while (protocol[0] == -1) {
      sleep(1);
      input = fopen("/dev/input/mouse0", "r");
      if (input)
        break;
    }
    if (protocol[0] != -1)
      fprintf(log, "%d %d %d %d\n", (int)time(NULL), protocol[0], x, y);
  }
  fclose(input);
  fclose(log);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2ab,-0x8(%rbp)
movl   $0x180,-0xc(%rbp)
lea    0xe7c(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe79(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d3 <main+0x173>
movb   $0x0,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x3,%eax
jge    11e8 <main+0x88>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,%cl
movsbq -0x21(%rbp),%rax
mov    %cl,-0x24(%rbp,%rax,1)
mov    -0x21(%rbp),%al
add    $0x1,%al
mov    %al,-0x21(%rbp)
jmp    11ba <main+0x5a>
movsbl -0x23(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x556,-0x8(%rbp)
jle    1206 <main+0xa6>
movl   $0x556,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1217 <main+0xb7>
movl   $0x0,-0x8(%rbp)
movsbl -0x22(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x300,-0xc(%rbp)
jle    1237 <main+0xd7>
movl   $0x300,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1248 <main+0xe8>
movl   $0x0,-0xc(%rbp)
jmp    124d <main+0xed>
movsbl -0x24(%rbp),%eax
cmp    $0xffffffff,%eax
jne    1290 <main+0x130>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0xd95(%rip),%rdi
lea    0xda0(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128b <main+0x12b>
jmp    1290 <main+0x130>
jmp    124d <main+0xed>
movsbl -0x24(%rbp),%eax
cmp    $0xffffffff,%eax
je     12ce <main+0x16e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x24(%rbp),%al
mov    -0x8(%rbp),%r8d
mov    -0xc(%rbp),%r9d
lea    0xd63(%rip),%rsi
movsbl %al,%ecx
call   1050 <fprintf@plt>
jmp    11ab <main+0x4b>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe8c(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rbx,%rbx
je     12a2 <main+0x142>
mov    %rbp,(%rsp)
mov    $0x180,%ebp
mov    $0x2ab,%r13d
lea    0xe5c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1040 <fgetc@plt>
movsbl %r12b,%ecx
add    %r13d,%ecx
cmp    $0x556,%ecx
jl     1202 <main+0xa2>
mov    $0x556,%ecx
mov    %ecx,%r13d
test   %ecx,%ecx
jns    120c <main+0xac>
xor    %r13d,%r13d
movsbl %al,%eax
sub    %eax,%ebp
cmp    $0x300,%ebp
mov    %r14,%r12
jge    1230 <main+0xd0>
test   %ebp,%ebp
js     1239 <main+0xd9>
cmp    $0xff,%r15b
jne    1241 <main+0xe1>
jmp    1270 <main+0x110>
nopl   0x0(%rax,%rax,1)
mov    $0x300,%ebp
test   %ebp,%ebp
jns    1220 <main+0xc0>
xor    %ebp,%ebp
cmp    $0xff,%r15b
je     1270 <main+0x110>
xor    %edi,%edi
call   1060 <time@plt>
movsbl %r15b,%ecx
mov    (%rsp),%rdi
lea    0xdd2(%rip),%rsi
mov    %eax,%edx
mov    %r13d,%r8d
mov    %ebp,%r9d
call   1050 <fprintf@plt>
jmp    11d0 <main+0x70>
nopl   0x0(%rax)
lea    0xd89(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1280 <main+0x120>
mov    %rax,%rbx
jmp    11c0 <main+0x60>
xor    %edi,%edi
call   1030 <fclose@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/dev/input/mouse0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"/var/log/mouse_log\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [13 x i8] c"%d %d %d %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8
%7 = alloca [3 x i8]
store i32 0, i32* %1
store i32 683, i32* %2
store i32 384, i32* %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %9, i32** %5
br label %10
10: ; preds = %80, %0
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %81
13: ; preds = %10
store i8 0, i8* %6
br label %14
14: ; preds = %24, %13
%15 = load i8, i8* %6
%16 = sext i8 %15 to i32
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i32*, i32** %4
%20 = call signext i8 @fgetc(i32* %19)
%21 = load i8, i8* %6
%22 = sext i8 %21 to i64
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 %22
store i8 %20, i8* %23
br label %24
24: ; preds = %18
%25 = load i8, i8* %6
%26 = add i8 %25, 1
store i8 %26, i8* %6
br label %14
27: ; preds = %14
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = load i32, i32* %2
%32 = add nsw i32 %31, %30
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = icmp sgt i32 %33, 1366
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 1366, i32* %2
br label %36
36: ; preds = %35, %27
%37 = load i32, i32* %2
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 0, i32* %2
br label %40
40: ; preds = %39, %36
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 2
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = load i32, i32* %3
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp sgt i32 %46, 768
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 768, i32* %3
br label %49
49: ; preds = %48, %40
%50 = load i32, i32* %3
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %49
store i32 0, i32* %3
br label %53
53: ; preds = %52, %49
br label %54
54: ; preds = %65, %53
%55 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %66
59: ; preds = %54
%60 = call i32 @sleep(i32 1)
%61 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %61, i32** %4
%62 = load i32*, i32** %4
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %65
64: ; preds = %59
br label %66
65: ; preds = %59
br label %54
66: ; preds = %64, %54
%67 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp ne i32 %69, -1
br i1 %70, label %71, label %80
71: ; preds = %66
%72 = load i32*, i32** %5
%73 = call i64 @time(i32* null)
%74 = trunc i64 %73 to i32
%75 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%76 = load i8, i8* %75
%77 = load i32, i32* %2
%78 = load i32, i32* %3
%79 = call i32 @fprintf(i32* %72, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %74, i8 signext %76, i32 %77, i32 %78)
br label %80
80: ; preds = %71, %66
br label %10
81: ; preds = %10
%82 = load i32*, i32** %4
%83 = call i32 @fclose(i32* %82)
%84 = load i32*, i32** %5
%85 = call i32 @fclose(i32* %84)
%86 = load i32, i32* %1
ret i32 %86
}

LLVM IR (-O2):
@.str = [18 x i8] c"/dev/input/mouse0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"/var/log/mouse_log\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [13 x i8] c"%d %d %d %d\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%3 = icmp eq i32* %1, null
br i1 %3, label %35, label %4
4: ; preds = %27, %0
%5 = phi i32 [ 683, %0 ], [ %19, %27 ]
%6 = phi i32* [ %1, %0 ], [ %29, %27 ]
%7 = phi i32 [ 384, %0 ], [ %25, %27 ]
br label %8
8: ; preds = %4, %31
%9 = phi i32 [ %19, %31 ], [ %5, %4 ]
%10 = phi i32 [ %25, %31 ], [ %7, %4 ]
%11 = tail call signext i8 @fgetc(i32* %6)
%12 = tail call signext i8 @fgetc(i32* %6)
%13 = tail call signext i8 @fgetc(i32* %6)
%14 = sext i8 %12 to i32
%15 = add nsw i32 %9, %14
%16 = icmp slt i32 %15, 1366
%17 = select i1 %16, i32 %15, i32 1366
%18 = icmp sgt i32 %17, 0
%19 = select i1 %18, i32 %17, i32 0
%20 = sext i8 %13 to i32
%21 = sub nsw i32 %10, %20
%22 = icmp slt i32 %21, 768
%23 = select i1 %22, i32 %21, i32 768
%24 = icmp sgt i32 %23, 0
%25 = select i1 %24, i32 %23, i32 0
%26 = icmp eq i8 %11, -1
br i1 %26, label %27, label %31
27: ; preds = %8, %27
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %27, label %4
31: ; preds = %8
%32 = tail call i64 @time(i32* null)
%33 = trunc i64 %32 to i32
%34 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %33, i8 signext %11, i32 %19, i32 %25)
br label %8
35: ; preds = %0
%36 = tail call i32 @fclose(i32* null)
%37 = tail call i32 @fclose(i32* %2)
ret i32 0
}