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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int main(int argc, char **argv) {
  int i;
  int ret;
  int fd;
  int press_cnt[4];
  fd = open("/dev/buttons", 0);
  if (fd < 0) {
    printf("Can't open /dev/buttons\n");
    return -1;
  }
  while (1) {
    ret = read(fd, press_cnt, sizeof(press_cnt));
    if (ret < 0) {
      printf("read err!\n");
      continue;
    }
    for (i = 0; i < sizeof(press_cnt) / sizeof(press_cnt[0]); i++) {
      if (press_cnt[i])
        printf("K%d has been pressed %d times!\n", i + 1, press_cnt[i]);
    }
  }
  close(fd);
  return 0;
}


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)
lea    0xeb3(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117a <main+0x4a>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
jmp    117f <main+0x4f>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b0 <main+0x80>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    117f <main+0x4f>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    1203 <main+0xd3>
movslq -0x14(%rbp),%rax
cmpl   $0x0,-0x30(%rbp,%rax,4)
je     11f0 <main+0xc0>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f5 <main+0xc5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b7 <main+0x87>
jmp    117f <main+0x4f>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xee3(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     11f8 <main+0xb8>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xeb3(%rip),%r15
lea    0xe8c(%rip),%r12
jmp    1188 <main+0x48>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1180 <main+0x40>
mov    (%rsp),%edx
test   %edx,%edx
je     11b1 <main+0x71>
mov    %r12,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edx
test   %edx,%edx
je     11c8 <main+0x88>
mov    %r12,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%edx
test   %edx,%edx
je     11df <main+0x9f>
mov    %r12,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     1188 <main+0x48>
mov    %r12,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1188 <main+0x48>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/buttons\00"
@.str.1 = [25 x i8] c"Can't open /dev/buttons\0A\00"
@.str.2 = [11 x i8] c"read err!\0A\00"
@.str.3 = [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 [4 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
15: ; preds = %2
br label %16
16: ; preds = %15, %22, %47
%17 = load i32, i32* %8
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%19 = call i32 @read(i32 %17, i32* %18, i32 16)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %16
24: ; preds = %16
store i32 0, i32* %6
br label %25
25: ; preds = %44, %24
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = icmp ult i64 %27, 4
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %29
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %41)
br label %43
43: ; preds = %35, %29
br label %44
44: ; preds = %43
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %25
47: ; preds = %25
br label %16
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/buttons\00"
@.str.3 = [32 x i8] c"K%d has been pressed %d times!\0A\00"
@str = [10 x i8] c"read err!\00"
@str.4 = [24 x i8] c"Can't open /dev/buttons\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
br label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
ret i32 -1
14: ; preds = %37, %7
%15 = call i32 @read(i32 %5, i32* %8, i32 16)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %22
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %37
22: ; preds = %17
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %18)
br label %24
24: ; preds = %17, %22
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 %25)
br label %29
29: ; preds = %27, %24
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 3, i32 %30)
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* %11
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34, %38, %20
br label %14
38: ; preds = %34
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 %35)
br label %37
}