#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;
  scalar_t__ revents;
};
typedef int /*<<< orphan*/ pfd;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ POLLPRI;
int /*<<< orphan*/ _direction(int, char *);
int /*<<< orphan*/ _edge(int, char *);
int /*<<< orphan*/ _export(int);
int _open_value(int, int /*<<< orphan*/);
int _read_value(int);
int /*<<< orphan*/ _unexport(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct pollfd *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ usleep(int);
void gpio_keypress(int pin, int active_value) {
  struct pollfd pfd[1];
  int val, fd;
  memset(pfd, 0, sizeof(pfd));
  _export(pin);
  _direction(pin, "in");
  _edge(pin, "both");
  fd = _open_value(pin, O_RDONLY);
  val = _read_value(fd);
  do {
    pfd[0].fd = fd;
    pfd[0].revents = 0;
    pfd[0].events = POLLPRI;
    if (poll(pfd, 1, -1) < 0) {
      perror("poll");
      exit(1);
    }
    usleep(1000 * 5);
    val = _read_value(fd);
  } while (val != active_value);
  close(fd);
  _unexport(pin);
}


x86 assembly (clang, -O0):
<gpio_keypress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
call   1070 <_export@plt>
mov    -0x4(%rbp),%edi
lea    0xe10(%rip),%rsi
call   10d0 <_direction@plt>
mov    -0x4(%rbp),%edi
lea    0xe04(%rip),%rsi
call   10b0 <_edge@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   10a0 <_open_value@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <_read_value@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0x0,%rax
jge    1270 <gpio_keypress+0xb0>
lea    0xda7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x1388,%edi
call   10e0 <usleep@plt>
mov    -0x28(%rbp),%edi
call   1040 <_read_value@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1223 <gpio_keypress+0x63>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <_unexport@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_keypress>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
call   1070 <_export@plt>
lea    0xe0e(%rip),%rsi
mov    %r14d,%edi
call   10d0 <_direction@plt>
lea    0xe02(%rip),%rsi
mov    %r14d,%edi
call   10b0 <_edge@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   10a0 <_open_value@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <_read_value@plt>
mov    0x2dc6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,(%rsp)
movq   $0x0,0x8(%rsp)
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %rax,%rax
js     128c <gpio_keypress+0xcc>
mov    $0x1388,%edi
call   10e0 <usleep@plt>
mov    %ebx,%edi
call   1040 <_read_value@plt>
cmp    %ebp,%eax
jne    1230 <gpio_keypress+0x70>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1030 <_unexport@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i64 }
@.str = [3 x i8] c"in\00"
@.str.1 = [5 x i8] c"both\00"
@O_RDONLY = global i32 0
@POLLPRI = global i32 0
@.str.2 = [5 x i8] c"poll\00"
define void @gpio_keypress(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x %struct.pollfd]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%9 = call i32 @memset(%struct.pollfd* %8, i32 0, i32 16)
%10 = load i32, i32* %3
%11 = call i32 @_export(i32 %10)
%12 = load i32, i32* %3
%13 = call i32 @_direction(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %3
%15 = call i32 @_edge(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %3
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @_open_value(i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = call i32 @_read_value(i32 %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %40, %2
%22 = load i32, i32* %7
%23 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %25, i32 0, i32 2
store i64 0, i64* %26
%27 = load i32, i32* @POLLPRI
%28 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 1
store i32 %27, i32* %29
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%31 = call i64 @poll(%struct.pollfd* %30, i32 1, i32 -1)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %21
%37 = call i32 @usleep(i32 5000)
%38 = load i32, i32* %7
%39 = call i32 @_read_value(i32 %38)
store i32 %39, i32* %6
br label %40
40: ; preds = %36
%41 = load i32, i32* %6
%42 = load i32, i32* %4
%43 = icmp ne i32 %41, %42
br i1 %43, label %21, label %44
44: ; preds = %40
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %3
%48 = call i32 @_unexport(i32 %47)
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i64 }
@.str = [3 x i8] c"in\00"
@.str.1 = [5 x i8] c"both\00"
@O_RDONLY = global i32 0
@POLLPRI = global i32 0
@.str.2 = [5 x i8] c"poll\00"
define void @gpio_keypress(i32 %0, i32 %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = bitcast [1 x %struct.pollfd]* %3 to i8*
%5 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%6 = call i32 @memset(%struct.pollfd* %5, i32 0, i32 16)
%7 = call i32 @_export(i32 %0)
%8 = call i32 @_direction(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @_edge(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @_open_value(i32 %0, i32 %10)
%12 = call i32 @_read_value(i32 %11)
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
br label %16
16: ; preds = %23, %2
store i32 %11, i32* %13
store i64 0, i64* %14
%17 = load i32, i32* @POLLPRI
store i32 %17, i32* %15
%18 = call i64 @poll(%struct.pollfd* %5, i32 1, i32 -1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 @usleep(i32 5000)
%25 = call i32 @_read_value(i32 %11)
%26 = icmp eq i32 %25, %1
br i1 %26, label %27, label %16
27: ; preds = %23
%28 = call i32 @close(i32 %11)
%29 = call i32 @_unexport(i32 %0)
ret void
}