#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, int *, int);
int main(int argc, char **argv) {
  int fd;
  fd = open("/dev/key0", O_RDWR);
  if (fd == -1)
    printf("open failed!");
  int val = 0;
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  int result = poll(fds, 1, 5000);
  if (result == 0)
    printf("time off");
  else {
    read(fd, &val, 4);
    printf("key:%d", 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   $0xffffffff,-0x14(%rbp)
jne    1186 <main+0x46>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11d2 <main+0x92>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f4 <main+0xb4>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    116f <main+0x2f>
lea    0xea2(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,0x4(%rsp)
mov    %ebx,0x8(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     11c5 <main+0x85>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <read@plt>
mov    0x4(%rsp),%esi
lea    0xe64(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11d3 <main+0x93>
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@.str = [10 x i8] c"/dev/key0\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"open failed!\00"
@POLLIN = global i32 0
@.str.2 = [9 x i8] c"time off\00"
@.str.3 = [7 x i8] c"key:%d\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 [1 x %struct.pollfd]
%9 = alloca i32
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 ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
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
store i32 0, i32* %7
%17 = load i32, i32* %6
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, 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]* %8, i64 0, i64 0
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%24 = call i32 @poll(%struct.pollfd* %23, i32 1, i32 5000)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %34
29: ; preds = %16
%30 = load i32, i32* %6
%31 = call i32 @read(i32 %30, i32* %7, i32 4)
%32 = load i32, i32* %7
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %27
ret i32 0
}

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