#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 STDIN_FILENO;
unsigned int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gpio_export(unsigned int);
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*/ memset(void *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int pread(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ 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, rc;
  char buf[64];
  unsigned int gpio;
  int len, i = 0;
  if (argc < 2) {
    printf("Usage: gpio-int <gpio-pin>\n\n");
    printf(
        "Waits for a change in the GPIO pin voltage level or input on stdin\n");
    exit(-1);
  }
  gpio = atoi(argv[1]);
  gpio_export(gpio);
  gpio_set_dir(gpio, 0);
  gpio_set_edge(gpio, "falling");
  gpio_fd = gpio_fd_open(gpio);
  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, (2 * 1000));
    if (rc < 0) {
      printf("poll() failed!\n");
      return -1;
    }
    if (rc == 0) {
      printf(".");
    }
    if (fdset[1].revents & POLLPRI) {
      len = pread(fdset[1].fd, buf, 64, 0);
      printf("poll() GPIO %d interrupt occurred. Read %d bytes %c\n", gpio, len,
             buf[0]);
    }
    if (fdset[0].revents & POLLIN) {
      (void)read(fdset[0].fd, buf, 1);
      printf("poll() 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   $0x0,-0x8c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    121e <main+0x5e>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%edi
call   1050 <gpio_export@plt>
mov    -0x84(%rbp),%edi
xor    %esi,%esi
call   1030 <gpio_set_dir@plt>
mov    -0x84(%rbp),%edi
lea    0xe0b(%rip),%rsi
call   1080 <gpio_set_edge@plt>
mov    -0x84(%rbp),%edi
call   1070 <gpio_fd_open@plt>
mov    %eax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    $0x7d0,%edx
call   10b0 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12dd <main+0x11d>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
cmpl   $0x0,-0x3c(%rbp)
jne    12f5 <main+0x135>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    0x2cf1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1341 <main+0x181>
mov    -0x24(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   10d0 <pread@plt>
mov    %eax,-0x88(%rbp)
mov    -0x84(%rbp),%esi
mov    -0x88(%rbp),%edx
movsbl -0x80(%rbp),%ecx
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2c95(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1379 <main+0x1b9>
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    0x2c50(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1269 <main+0xa9>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
cmp    $0x1,%edi
jle    133a <main+0x15a>
mov    0x8(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   1070 <gpio_export@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1050 <gpio_set_dir@plt>
lea    0xde5(%rip),%rsi
mov    %r14d,%edi
call   10a0 <gpio_set_edge@plt>
mov    %r14d,%edi
call   1090 <gpio_fd_open@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    0x2da9(%rip),%r13
mov    0x2db2(%rip),%r12
lea    0x20(%rsp),%r15
jmp    125e <main+0x7e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,0xc(%rsp)
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0x7d0,%edx
call   10d0 <poll@plt>
test   %eax,%eax
js     131a <main+0x13a>
jne    12af <main+0xcf>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%eax
test   %eax,0x14(%rsp)
je     12e4 <main+0x104>
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   10f0 <pread@plt>
movsbl 0x20(%rsp),%ecx
lea    0xd30(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     1250 <main+0x70>
mov    (%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
movsbl 0x20(%rsp),%esi
lea    0xd2f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0x70>
lea    0xd37(%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
lea    0xd26(%rip),%rdi
call   1040 <puts@plt>
lea    0xd36(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [29 x i8] c"Usage: gpio-int <gpio-pin>\0A\0A\00"
@.str.1 = [68 x i8] c"Waits for a change in the GPIO pin voltage level or input on stdin\0A\00"
@.str.2 = [8 x i8] c"falling\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLPRI = global i32 0
@.str.3 = [16 x i8] c"poll() failed!\0A\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [53 x i8] c"poll() GPIO %d interrupt occurred. Read %d bytes %c\0A\00"
@.str.6 = [27 x i8] c"poll() 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 [64 x i8]
%13 = alloca i32
%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 0, i32* %15
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %3
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %13
%27 = load i32, i32* %13
%28 = call i32 @gpio_export(i32 %27)
%29 = load i32, i32* %13
%30 = call i32 @gpio_set_dir(i32 %29, i32 0)
%31 = load i32, i32* %13
%32 = call i32 @gpio_set_edge(i32 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %13
%34 = call i32 @gpio_fd_open(i32 %33)
store i32 %34, i32* %10
br label %35
35: ; preds = %22, %99
%36 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%37 = bitcast %struct.pollfd* %36 to i8*
%38 = call i32 @memset(i8* %37, i32 0, i32 24)
%39 = load i32, i32* @STDIN_FILENO
%40 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* @POLLIN
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = load i32, i32* %10
%46 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %46, i32 0, i32 0
store i32 %45, i32* %47
%48 = load i32, i32* @POLLPRI
%49 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%50 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %49, i32 0, i32 1
store i32 %48, i32* %50
%51 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%52 = load i32, i32* %9
%53 = call i32 @poll(%struct.pollfd* %51, i32 %52, i32 2000)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %35
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
ret i32 -1
58: ; preds = %35
%59 = load i32, i32* %11
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %58
%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 2
%66 = load i32, i32* %65
%67 = load i32, i32* @POLLPRI
%68 = and i32 %66, %67
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %82
70: ; preds = %63
%71 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%72 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%75 = call i32 @pread(i32 %73, i8* %74, i32 64, i32 0)
store i32 %75, i32* %14
%76 = load i32, i32* %13
%77 = load i32, i32* %14
%78 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%79 = load i8, i8* %78
%80 = sext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i32 %76, i32 %77, i32 %80)
br label %82
82: ; preds = %70, %63
%83 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%84 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %83, i32 0, i32 2
%85 = load i32, i32* %84
%86 = load i32, i32* @POLLIN
%87 = and i32 %85, %86
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %99
89: ; preds = %82
%90 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%91 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %90, i32 0, i32 0
%92 = load i32, i32* %91
%93 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%94 = call i32 @read(i32 %92, i8* %93, i32 1)
%95 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%96 = load i8, i8* %95
%97 = sext i8 %96 to i32
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %97)
br label %99
99: ; preds = %89, %82
%100 = load i32, i32* @stdout
%101 = call i32 @fflush(i32 %100)
br label %35
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str.2 = [8 x i8] c"falling\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLPRI = global i32 0
@.str.5 = [53 x i8] c"poll() GPIO %d interrupt occurred. Read %d bytes %c\0A\00"
@.str.6 = [27 x i8] c"poll() stdin read 0x%2.2X\0A\00"
@stdout = global i32 0
@str = [15 x i8] c"poll() failed!\00"
@str.7 = [28 x i8] c"Usage: gpio-int <gpio-pin>\0A\00"
@str.8 = [67 x i8] c"Waits for a change in the GPIO pin voltage level or input on stdin\00"
define i32 @main(i32 %0, i8** nocapture %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 = icmp slt i32 %0, 2
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.8, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %3
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = tail call i32 @gpio_export(i32 %16)
%18 = tail call i32 @gpio_set_dir(i32 %16, i32 0)
%19 = tail call i32 @gpio_set_edge(i32 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @gpio_fd_open(i32 %16)
%21 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
%23 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
%24 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 0
%25 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 1
%26 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 2
%27 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
br label %28
28: ; preds = %63, %13
%29 = call i32 @memset(i8* %6, i32 0, i32 24)
%30 = load i32, i32* @STDIN_FILENO
store i32 %30, i32* %22
%31 = load i32, i32* @POLLIN
store i32 %31, i32* %23
store i32 %20, i32* %24
%32 = load i32, i32* @POLLPRI
store i32 %32, i32* %25
%33 = call i32 @poll(%struct.pollfd* %21, i32 2, i32 2000)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret i32 -1
37: ; preds = %28
%38 = icmp eq i32 %33, 0
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = call i32 @putchar(i32 46)
br label %41
41: ; preds = %39, %37
%42 = load i32, i32* %26
%43 = load i32, i32* @POLLPRI
%44 = and i32 %43, %42
%45 = icmp eq i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %41
%47 = load i32, i32* %24
%48 = call i32 @pread(i32 %47, i8* %7, i32 64, i32 0)
%49 = load i8, i8* %7
%50 = sext i8 %49 to i32
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i32 %16, i32 %48, i32 %50)
br label %52
52: ; preds = %46, %41
%53 = load i32, i32* %27
%54 = load i32, i32* @POLLIN
%55 = and i32 %54, %53
%56 = icmp eq i32 %55, 0
br i1 %56, label %63, label %57
57: ; preds = %52
%58 = load i32, i32* %22
%59 = call i32 @read(i32 %58, i8* %7, i32 1)
%60 = load i8, i8* %7
%61 = sext i8 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %57, %52
%64 = load i32, i32* @stdout
%65 = call i32 @fflush(i32 %64)
br label %28
}