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

/* Variables and functions */
int POLLIN;
int POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int STDIN_FILENO;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gpio_fd_close(int);
int gpio_fd_open(unsigned int);
int /*<<< orphan*/ gpio_set_dir(unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ gpio_set_edge(unsigned int, char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv, char **envp) {
  struct pollfd fdset[2];
  int nfds = 2;
  int gpio_fd, timeout, rc;
  char buf[64];
  unsigned int gpio = 25;
  int len;
  gpio_set_dir(gpio, 0);
  gpio_set_edge(gpio, "rising");
  gpio_fd = gpio_fd_open(gpio);
  timeout = (3 * 1000);
  while (1) {
    memset((void *)fdset, 0, sizeof(fdset));
    fdset[0].fd = STDIN_FILENO;
    fdset[0].events = POLLIN;
    fdset[1].fd = gpio_fd;
    fdset[1].events = POLLPRI;
    rc = poll(fdset, nfds, timeout);
    if (rc < 0) {
      printf("\npoll() failed!\n");
      return -1;
    }
    if (rc == 0) {
      printf(".");
    }
    if (fdset[1].revents & POLLPRI) {
      lseek(fdset[1].fd, 0, SEEK_SET);
      len = read(fdset[1].fd, buf, 64);
      printf("\npoll() GPIO %d interrupt occurred\n", gpio);
    }
    if (fdset[0].revents & POLLIN) {
      (void)read(fdset[0].fd, buf, 1);
      printf("\npoll() stdin read 0x%2.2X\n", (unsigned int)buf[0]);
    }
    fflush(stdout);
  }
  gpio_fd_close(gpio_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x34(%rbp)
movl   $0x19,-0x84(%rbp)
mov    -0x84(%rbp),%edi
xor    %esi,%esi
call   1030 <gpio_set_dir@plt>
mov    -0x84(%rbp),%edi
lea    0xe28(%rip),%rsi
call   1080 <gpio_set_edge@plt>
mov    -0x84(%rbp),%edi
call   1070 <gpio_fd_open@plt>
mov    %eax,-0x38(%rbp)
movl   $0xbb8,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x3c(%rbp),%edx
call   10b0 <poll@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1264 <main+0xd4>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
cmpl   $0x0,-0x40(%rbp)
jne    127c <main+0xec>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    0x2d6a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12cf <main+0x13f>
mov    -0x24(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x40,%edx
call   1090 <read@plt>
mov    %eax,-0x88(%rbp)
mov    -0x84(%rbp),%esi
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2d07(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1307 <main+0x177>
mov    -0x30(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
movsbl -0x80(%rbp),%esi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    11f2 <main+0x62>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    $0x19,%edi
xor    %esi,%esi
call   1050 <gpio_set_dir@plt>
lea    0xe2f(%rip),%rsi
mov    $0x19,%edi
call   10a0 <gpio_set_edge@plt>
mov    $0x19,%edi
call   1090 <gpio_fd_open@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    0x2def(%rip),%r15
mov    0x2df8(%rip),%r12
mov    0x2dc9(%rip),%r13
lea    0x20(%rsp),%r14
jmp    1219 <main+0x69>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
call   10c0 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,0xc(%rsp)
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0xbb8,%edx
call   10d0 <poll@plt>
test   %eax,%eax
js     12e4 <main+0x134>
jne    126d <main+0xbd>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%eax
test   %eax,0x14(%rsp)
je     12af <main+0xff>
mov    0xc(%rsp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   10b0 <read@plt>
lea    0xd64(%rip),%rdi
mov    $0x19,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r15),%eax
test   %eax,0x8(%rsp)
je     1210 <main+0x60>
mov    (%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
movsbl 0x20(%rsp),%esi
lea    0xd53(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1210 <main+0x60>
lea    0xd5c(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [7 x i8] c"rising\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLPRI = global i32 0
@.str.1 = [17 x i8] c"\0Apoll() failed!\0A\00"
@.str.2 = [2 x i8] c".\00"
@SEEK_SET = global i32 0
@.str.3 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@.str.4 = [28 x i8] c"\0Apoll() stdin read 0x%2.2X\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [2 x %struct.pollfd]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [64 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 2, i32* %9
store i32 25, i32* %14
%16 = load i32, i32* %14
%17 = call i32 @gpio_set_dir(i32 %16, i32 0)
%18 = load i32, i32* %14
%19 = call i32 @gpio_set_edge(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %14
%21 = call i32 @gpio_fd_open(i32 %20)
store i32 %21, i32* %10
store i32 3000, i32* %11
br label %22
22: ; preds = %3, %88
%23 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%24 = bitcast %struct.pollfd* %23 to i8*
%25 = call i32 @memset(i8* %24, i32 0, i32 24)
%26 = load i32, i32* @STDIN_FILENO
%27 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* @POLLIN
%30 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %30, i32 0, i32 1
store i32 %29, i32* %31
%32 = load i32, i32* %10
%33 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* @POLLPRI
%36 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 1
store i32 %35, i32* %37
%38 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%39 = load i32, i32* %9
%40 = load i32, i32* %11
%41 = call i32 @poll(%struct.pollfd* %38, i32 %39, i32 %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %22
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
46: ; preds = %22
%47 = load i32, i32* %12
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%53 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %52, i32 0, i32 2
%54 = load i32, i32* %53
%55 = load i32, i32* @POLLPRI
%56 = and i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %71
58: ; preds = %51
%59 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%60 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = load i32, i32* @SEEK_SET
%63 = call i32 @lseek(i32 %61, i32 0, i32 %62)
%64 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%65 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%68 = call i32 @read(i32 %66, i8* %67, i32 64)
store i32 %68, i32* %15
%69 = load i32, i32* %14
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %58, %51
%72 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 2
%74 = load i32, i32* %73
%75 = load i32, i32* @POLLIN
%76 = and i32 %74, %75
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %88
78: ; preds = %71
%79 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%80 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %79, i32 0, i32 0
%81 = load i32, i32* %80
%82 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%83 = call i32 @read(i32 %81, i8* %82, i32 1)
%84 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %78, %71
%89 = load i32, i32* @stdout
%90 = call i32 @fflush(i32 %89)
br label %22
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [7 x i8] c"rising\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLPRI = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@.str.4 = [28 x i8] c"\0Apoll() stdin read 0x%2.2X\0A\00"
@stdout = global i32 0
@str = [16 x i8] c"\0Apoll() failed!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca [2 x %struct.pollfd]
%5 = alloca [64 x i8]
%6 = bitcast [2 x %struct.pollfd]* %4 to i8*
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @gpio_set_dir(i32 25, i32 0)
%9 = tail call i32 @gpio_set_edge(i32 25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @gpio_fd_open(i32 25)
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%12 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 0
%15 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 1
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 2
%17 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
br label %18
18: ; preds = %54, %3
%19 = call i32 @memset(i8* %6, i32 0, i32 24)
%20 = load i32, i32* @STDIN_FILENO
store i32 %20, i32* %12
%21 = load i32, i32* @POLLIN
store i32 %21, i32* %13
store i32 %10, i32* %14
%22 = load i32, i32* @POLLPRI
store i32 %22, i32* %15
%23 = call i32 @poll(%struct.pollfd* %11, i32 2, i32 3000)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 -1
27: ; preds = %18
%28 = icmp eq i32 %23, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = call i32 @putchar(i32 46)
br label %31
31: ; preds = %29, %27
%32 = load i32, i32* %16
%33 = load i32, i32* @POLLPRI
%34 = and i32 %33, %32
%35 = icmp eq i32 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %31
%37 = load i32, i32* %14
%38 = load i32, i32* @SEEK_SET
%39 = call i32 @lseek(i32 %37, i32 0, i32 %38)
%40 = load i32, i32* %14
%41 = call i32 @read(i32 %40, i8* %7, i32 64)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 25)
br label %43
43: ; preds = %36, %31
%44 = load i32, i32* %17
%45 = load i32, i32* @POLLIN
%46 = and i32 %45, %44
%47 = icmp eq i32 %46, 0
br i1 %47, label %54, label %48
48: ; preds = %43
%49 = load i32, i32* %12
%50 = call i32 @read(i32 %49, i8* %7, i32 1)
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %48, %43
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %18
}