#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*/ key_value;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int /*<<< orphan*/ usage(char **);
int main(int argc, char *argv[]) {
  int fd, ret, i;
  int key_value[5];
  if (argc != 2) {
    usage(argv);
    exit(1);
  }
  fd = open(argv[1], 0);
  if (fd < 0) {
    perror("open fail");
    exit(1);
  }
  while (1) {
    ret = read(fd, key_value, sizeof(key_value));
    if (ret < 0) {
      printf("read err!\n");
      continue;
    }
    for (i = 0; i < sizeof(key_value) / sizeof(key_value[0]); i++) {
      if (key_value[i])
        printf("K%d has been pressed %d times!\n", i + 1, key_value[i]);
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1193 <main+0x33>
mov    -0x10(%rbp),%rdi
call   1050 <usage@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c5 <main+0x65>
lea    0xe4a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11ca <main+0x6a>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11fb <main+0x9b>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ca <main+0x6a>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x5,%rax
jae    124e <main+0xee>
movslq -0x1c(%rbp),%rax
cmpl   $0x0,-0x30(%rbp,%rax,4)
je     123b <main+0xdb>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
movslq -0x1c(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1240 <main+0xe0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1202 <main+0xa2>
jmp    11ca <main+0x6a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1242 <main+0xd2>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     1254 <main+0xe4>
mov    %eax,%ebp
mov    %rsp,%r14
lea    0xe88(%rip),%r15
lea    0xe61(%rip),%rbx
jmp    11b8 <main+0x48>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11b0 <main+0x40>
mov    (%rsp),%edx
test   %edx,%edx
je     11e1 <main+0x71>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edx
test   %edx,%edx
je     11f8 <main+0x88>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%edx
test   %edx,%edx
je     120f <main+0x9f>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     1226 <main+0xb6>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rsp),%edx
test   %edx,%edx
je     11b8 <main+0x48>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b8 <main+0x48>
mov    %rsi,%rdi
call   1060 <usage@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xda5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"open fail\00"
@.str.1 = [11 x i8] c"read err!\0A\00"
@.str.2 = [32 x i8] c"K%d has been pressed %d times!\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 [5 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = call i32 @usage(i8** %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @open(i8* %19, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
br label %27
27: ; preds = %26, %33, %58
%28 = load i32, i32* %6
%29 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 0
%30 = call i32 @read(i32 %28, i32* %29, i32 20)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %27
35: ; preds = %27
store i32 0, i32* %8
br label %36
36: ; preds = %55, %35
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %38, 5
br i1 %39, label %40, label %58
40: ; preds = %36
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [5 x i32], [5 x i32]* %9, i64 0, i64 %50
%52 = load i32, i32* %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %52)
br label %54
54: ; preds = %46, %40
br label %55
55: ; preds = %54
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %36
58: ; preds = %36
br label %27
}

LLVM IR (-O2):
@.str = [10 x i8] c"open fail\00"
@.str.2 = [32 x i8] c"K%d has been pressed %d times!\0A\00"
@str = [10 x i8] c"read err!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [5 x i32]
%4 = bitcast [5 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @usage(i8** %1)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @open(i8* %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 0
%16 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 1
%17 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 2
%18 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 3
%19 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 4
br label %23
20: ; preds = %9
%21 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %51, %14
%24 = call i32 @read(i32 %12, i32* %15, i32 20)
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* %15
%28 = icmp eq i32 %27, 0
br i1 %28, label %33, label %31
29: ; preds = %23
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %51
31: ; preds = %26
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %27)
br label %33
33: ; preds = %26, %31
%34 = load i32, i32* %16
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %34)
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* %17
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %39)
br label %43
43: ; preds = %41, %38
%44 = load i32, i32* %18
%45 = icmp eq i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 %44)
br label %48
48: ; preds = %46, %43
%49 = load i32, i32* %19
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48, %52, %29
br label %23
52: ; preds = %48
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 %49)
br label %51
}