#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;
  scalar_t__ code;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EIO;
scalar_t__ EV_KEY;
int EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ closeConnection(int);
int connectRemote(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, struct input_event *, int);
int /*<<< orphan*/ sendMessage(int, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(scalar_t__);
int main(int argc, char *argv[]) {
  char *dev;
  struct input_event ev;
  ssize_t n;
  int fd;
  int sockfd;
  char buffer[50];
  if (argc < 3) {
    printf("Usage a.out {Hostname} {/dev/input/by-path/kbd device}");
    exit(1);
  }
  fd = open(argv[2], O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
    return EXIT_FAILURE;
  }
  sockfd = connectRemote(argv[1]);
  while (1) {
    n = read(fd, &ev, sizeof ev);
    if (n == (ssize_t)-1) {
      if (errno == EINTR)
        continue;
      else
        break;
    } else {
      if (n != sizeof ev) {
        errno = EIO;
        break;
      }
    }
    if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2) {
      memset(buffer, 0, 50);
      sprintf(buffer, "%d-%d", (int)ev.code, ev.value);
      sendMessage(sockfd, buffer);
    }
  }
  fflush(stdout);
  closeConnection(sockfd);
  fprintf(stderr, "%s.\n", strerror(errno));
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f9 <main+0x39>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1268 <main+0xa8>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1399 <main+0x1d9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <connectRemote@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1060 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12ba <main+0xfa>
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0xf5>
jmp    1278 <main+0xb8>
jmp    1348 <main+0x188>
movslq -0x34(%rbp),%rax
cmp    $0x18,%rax
je     12e1 <main+0x121>
mov    0x2d01(%rip),%rax
mov    (%rax),%rcx
mov    0x2ce7(%rip),%rax
mov    %rcx,(%rax)
jmp    1348 <main+0x188>
jmp    12e6 <main+0x126>
mov    -0x30(%rbp),%rax
mov    0x2cef(%rip),%rcx
cmp    (%rcx),%rax
jne    1343 <main+0x183>
cmpl   $0x0,-0x28(%rbp)
jl     1343 <main+0x183>
cmpl   $0x2,-0x28(%rbp)
jg     1343 <main+0x183>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1050 <memset@plt>
lea    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%ecx
lea    0xd1a(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x3c(%rbp),%edi
lea    -0x70(%rbp),%rsi
call   1090 <sendMessage@plt>
jmp    1278 <main+0xb8>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x3c(%rbp),%edi
call   1080 <closeConnection@plt>
mov    0x2c63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
mov    0x2c4f(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x80(%rbp),%edi
mov    %rax,%rdx
lea    0xcc1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
cmp    $0x2,%edi
jle    1348 <main+0x188>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0xed>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1030 <connectRemote@plt>
mov    %eax,0x4(%rsp)
lea    0x8(%rsp),%r13
mov    0x2dd0(%rip),%r12
lea    0x20(%rsp),%r15
mov    0x2d94(%rip),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x18,%edx
call   1060 <read@plt>
cmp    $0x18,%eax
je     1260 <main+0xa0>
cmp    $0xffffffff,%eax
jne    12da <main+0x11a>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
je     1220 <main+0x60>
jmp    12ee <main+0x12e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
cmp    (%r12),%rax
jne    1220 <main+0x60>
mov    0x10(%rsp),%ebx
test   %ebx,%ebx
js     1220 <main+0x60>
cmp    $0x2,%ebx
jg     1220 <main+0x60>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1050 <memset@plt>
mov    0x18(%rsp),%edx
mov    %r15,%rdi
lea    0xdb7(%rip),%rsi
mov    %ebx,%ecx
call   10c0 <sprintf@plt>
mov    0x4(%rsp),%edi
mov    %r15,%rsi
call   1090 <sendMessage@plt>
jmp    1220 <main+0x60>
mov    0x2d14(%rip),%rax
mov    (%rax),%ebx
mov    0x2d03(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
lea    0xd6b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1330 <main+0x170>
mov    0x2cef(%rip),%rax
mov    (%rax),%rax
mov    0x2cd5(%rip),%rcx
mov    %rax,(%rcx)
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    0x4(%rsp),%edi
call   1080 <closeConnection@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%ebx
mov    0x2cab(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
lea    0xd23(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.input_event = type { i64, i32, i64 }
@.str = [55 x i8] c"Usage a.out {Hostname} {/dev/input/by-path/kbd device}\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot open %s: %s.\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
@EIO = global i64 0
@EV_KEY = global i64 0
@.str.2 = [6 x i8] c"%d-%d\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"%s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.input_event
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [50 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %6
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %3
br label %92
32: ; preds = %17
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @connectRemote(i8* %35)
store i32 %36, i32* %10
br label %37
37: ; preds = %32, %46, %81
%38 = load i32, i32* %9
%39 = call i32 @read(i32 %38, %struct.input_event* %7, i32 24)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %37
47: ; preds = %42
br label %82
48: ; preds = %37
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = icmp ne i64 %50, 24
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = load i64, i64* @EIO
store i64 %53, i64* @errno
br label %82
54: ; preds = %48
br label %55
55: ; preds = %54
%56 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
%57 = load i64, i64* %56
%58 = load i64, i64* @EV_KEY
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %81
60: ; preds = %55
%61 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%62 = load i32, i32* %61
%63 = icmp sge i32 %62, 0
br i1 %63, label %64, label %81
64: ; preds = %60
%65 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%66 = load i32, i32* %65
%67 = icmp sle i32 %66, 2
br i1 %67, label %68, label %81
68: ; preds = %64
%69 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%70 = call i32 @memset(i8* %69, i32 0, i32 50)
%71 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%72 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
%73 = load i64, i64* %72
%74 = trunc i64 %73 to i32
%75 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%76 = load i32, i32* %75
%77 = call i32 @sprintf(i8* %71, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %74, i32 %76)
%78 = load i32, i32* %10
%79 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%80 = call i32 @sendMessage(i32 %78, i8* %79)
br label %81
81: ; preds = %68, %64, %60, %55
br label %37
82: ; preds = %52, %47
%83 = load i32, i32* @stdout
%84 = call i32 @fflush(i32 %83)
%85 = load i32, i32* %10
%86 = call i32 @closeConnection(i32 %85)
%87 = load i32, i32* @stderr
%88 = load i64, i64* @errno
%89 = call i8* @strerror(i64 %88)
%90 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %89)
%91 = load i32, i32* @EXIT_FAILURE
store i32 %91, i32* %3
br label %92
92: ; preds = %82, %25
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32, i64 }
@.str = [55 x i8] c"Usage a.out {Hostname} {/dev/input/by-path/kbd device}\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot open %s: %s.\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
@EIO = global i64 0
@EV_KEY = global i64 0
@.str.2 = [6 x i8] c"%d-%d\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"%s.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = alloca [50 x i8]
%5 = bitcast %struct.input_event* %3 to i8*
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i64, i64* @errno
%20 = tail call i8* @strerror(i64 %19)
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* undef, i8* %20)
br label %61
22: ; preds = %11
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32 @connectRemote(i8* %24)
%26 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%27 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
%28 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
br label %29
29: ; preds = %46, %22
%30 = call i32 @read(i32 %15, %struct.input_event* %3, i32 24)
switch i32 %30, label %35 [
i32 -1, label %31
i32 24, label %37
]
31: ; preds = %29
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %46, label %53
35: ; preds = %29
%36 = load i64, i64* @EIO
store i64 %36, i64* @errno
br label %53
37: ; preds = %29
%38 = load i64, i64* %26
%39 = load i64, i64* @EV_KEY
%40 = icmp eq i64 %38, %39
%41 = load i32, i32* %27
%42 = icmp sgt i32 %41, -1
%43 = select i1 %40, i1 %42, i1 false
%44 = icmp slt i32 %41, 3
%45 = select i1 %43, i1 %44, i1 false
br i1 %45, label %47, label %46
46: ; preds = %37, %47, %31
br label %29
47: ; preds = %37
%48 = call i32 @memset(i8* %6, i32 0, i32 50)
%49 = load i64, i64* %28
%50 = trunc i64 %49 to i32
%51 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %50, i32 %41)
%52 = call i32 @sendMessage(i32 %25, i8* %6)
br label %46
53: ; preds = %31, %35
%54 = load i32, i32* @stdout
%55 = call i32 @fflush(i32 %54)
%56 = call i32 @closeConnection(i32 %25)
%57 = load i32, i32* @stderr
%58 = load i64, i64* @errno
%59 = call i8* @strerror(i64 %58)
%60 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %53, %17
%62 = load i32, i32* @EXIT_FAILURE
ret i32 %62
}