#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 pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ POLLIN;
int open(char *, int /*<<< orphan*/);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd;
  unsigned char key_val;
  int ret;
  struct pollfd fds[1];
  fd = open("/dev/buttons", O_RDWR);
  if (fd < 0) {
    printf("can't open!\n");
  }
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  while (1) {
    ret = poll(fds, 1, 5000);
    if (ret == 0) {
      printf("time out\n");
    } else {
      read(fd, &key_val, 1);
      printf("key_val = 0x%x\n", key_val);
    }
  }
  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)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1186 <main+0x46>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11cb <main+0x8b>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ee <main+0xae>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl -0x15(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1198 <main+0x58>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1184 <main+0x34>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,0x8(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r14
lea    0xe7c(%rip),%r15
lea    0x7(%rsp),%r12
lea    0xe60(%rip),%r13
jmp    11b8 <main+0x68>
nop
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1060 <poll@plt>
test   %eax,%eax
je     11b0 <main+0x60>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0x7(%rsp),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b8 <main+0x68>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@.str = [13 x i8] c"/dev/buttons\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"can't open!\0A\00"
@POLLIN = global i32 0
@.str.2 = [10 x i8] c"time out\0A\00"
@.str.3 = [16 x i8] c"key_val = 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca [1 x %struct.pollfd]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %6
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %9, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* @POLLIN
%21 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %9, i64 0, i64 0
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %21, i32 0, i32 1
store i32 %20, i32* %22
br label %23
23: ; preds = %16, %36
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %9, i64 0, i64 0
%25 = call i32 @poll(%struct.pollfd* %24, i32 1, i32 5000)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %36
30: ; preds = %23
%31 = load i32, i32* %6
%32 = call i32 @read(i32 %31, i8* %7, i32 1)
%33 = load i8, i8* %7
%34 = zext i8 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %30, %28
br label %23
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@.str = [13 x i8] c"/dev/buttons\00"
@O_RDWR = global i32 0
@POLLIN = global i32 0
@.str.3 = [16 x i8] c"key_val = 0x%x\0A\00"
@str = [9 x i8] c"time out\00"
@str.4 = [12 x i8] c"can't open!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = alloca [1 x %struct.pollfd]
%5 = bitcast [1 x %struct.pollfd]* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %7, i32* %13
%14 = load i32, i32* @POLLIN
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i32 %14, i32* %15
br label %16
16: ; preds = %26, %11
%17 = call i32 @poll(%struct.pollfd* %12, i32 1, i32 5000)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %26
21: ; preds = %16
%22 = call i32 @read(i32 %7, i8* %3, i32 1)
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %21, %19
br label %16
}