#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 /*<<< 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;
  int len;
  int num_interrupts = 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);
  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) {
      len = read(fdset[1].fd, buf, 64);
      printf("\npoll() GPIO %d interrupt occurred\n", gpio);
      if (num_interrupts > 0)
        break;
      num_interrupts++;
    }
    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    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x34(%rbp)
movl   $0x0,-0x24c(%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   10d0 <atoi@plt>
mov    %eax,-0x244(%rbp)
mov    -0x244(%rbp),%edi
call   1050 <gpio_export@plt>
mov    -0x244(%rbp),%edi
xor    %esi,%esi
call   1030 <gpio_set_dir@plt>
mov    -0x244(%rbp),%edi
lea    0xe0b(%rip),%rsi
call   1080 <gpio_set_edge@plt>
mov    -0x244(%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    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d4b(%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    12e0 <main+0x120>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13be <main+0x1fe>
cmpl   $0x0,-0x40(%rbp)
jne    12f8 <main+0x138>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    0x2cee(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     135c <main+0x19c>
mov    -0x24(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x40,%edx
call   1090 <read@plt>
mov    %eax,-0x248(%rbp)
mov    -0x244(%rbp),%esi
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x24c(%rbp)
jle    134d <main+0x18d>
jmp    13af <main+0x1ef>
mov    -0x24c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2c7a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     139c <main+0x1dc>
mov    -0x30(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    -0x240(%rbp),%rax
mov    %eax,%esi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1270 <main+0xb0>
mov    -0x38(%rbp),%edi
call   10c0 <gpio_fd_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
cmp    $0x1,%edi
jle    134d <main+0x16d>
mov    0x8(%rsi),%rdi
call   10f0 <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    0xde2(%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    0x2da6(%rip),%r13
mov    0x2daf(%rip),%r12
xor    %r15d,%r15d
jmp    125e <main+0x7e>
cs nopw 0x0(%rax,%rax,1)
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    $0xbb8,%edx
call   10d0 <poll@plt>
test   %eax,%eax
js     131f <main+0x13f>
jne    12af <main+0xcf>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%eax
test   %eax,0x14(%rsp)
je     12e8 <main+0x108>
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
mov    $0x40,%edx
call   10b0 <read@plt>
lea    0xd35(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r15d,%r15d
mov    $0x1,%r15d
jg     1332 <main+0x152>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     1250 <main+0x70>
mov    (%rsp),%edi
lea    0x20(%rsp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
mov    0x20(%rsp),%esi
lea    0xd19(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0x70>
lea    0xd22(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    133b <main+0x15b>
mov    %ebp,%edi
call   10e0 <gpio_fd_close@plt>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd04(%rip),%rdi
call   1040 <puts@plt>
lea    0xd14(%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 = [17 x i8] c"\0Apoll() failed!\0A\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@.str.6 = [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
%16 = 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* %16
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %3
%24 = load i8**, i8*** %6
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %14
%28 = load i32, i32* %14
%29 = call i32 @gpio_export(i32 %28)
%30 = load i32, i32* %14
%31 = call i32 @gpio_set_dir(i32 %30, i32 0)
%32 = load i32, i32* %14
%33 = call i32 @gpio_set_edge(i32 %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %14
%35 = call i32 @gpio_fd_open(i32 %34)
store i32 %35, i32* %10
store i32 3000, i32* %11
br label %36
36: ; preds = %23, %103
%37 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%38 = bitcast %struct.pollfd* %37 to i8*
%39 = call i32 @memset(i8* %38, i32 0, i32 24)
%40 = load i32, i32* @STDIN_FILENO
%41 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 0
store i32 %40, i32* %42
%43 = load i32, i32* @POLLIN
%44 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %44, i32 0, i32 1
store i32 %43, i32* %45
%46 = load i32, i32* %10
%47 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%48 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* @POLLPRI
%50 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %50, i32 0, i32 1
store i32 %49, i32* %51
%52 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%53 = load i32, i32* %9
%54 = load i32, i32* %11
%55 = call i32 @poll(%struct.pollfd* %52, i32 %53, i32 %54)
store i32 %55, i32* %12
%56 = load i32, i32* %12
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %36
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %109
60: ; preds = %36
%61 = load i32, i32* %12
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%67 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %66, i32 0, i32 2
%68 = load i32, i32* %67
%69 = load i32, i32* @POLLPRI
%70 = and i32 %68, %69
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %86
72: ; preds = %65
%73 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%74 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8** %76, i32 64)
store i32 %77, i32* %15
%78 = load i32, i32* %14
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %78)
%80 = load i32, i32* %16
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %72
br label %106
83: ; preds = %72
%84 = load i32, i32* %16
%85 = add nsw i32 %84, 1
store i32 %85, i32* %16
br label %86
86: ; preds = %83, %65
%87 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%88 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %87, i32 0, i32 2
%89 = load i32, i32* %88
%90 = load i32, i32* @POLLIN
%91 = and i32 %89, %90
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %103
93: ; preds = %86
%94 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%95 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %94, i32 0, i32 0
%96 = load i32, i32* %95
%97 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%98 = call i32 @read(i32 %96, i8** %97, i32 1)
%99 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%100 = load i8*, i8** %99
%101 = ptrtoint i8* %100 to i32
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %101)
br label %103
103: ; preds = %93, %86
%104 = load i32, i32* @stdout
%105 = call i32 @fflush(i32 %104)
br label %36
106: ; preds = %82
%107 = load i32, i32* %10
%108 = call i32 @gpio_fd_close(i32 %107)
store i32 0, i32* %4
br label %109
109: ; preds = %106, %58
%110 = load i32, i32* %4
ret i32 %110
}

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 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@.str.6 = [28 x i8] c"\0Apoll() stdin read 0x%2.2X\0A\00"
@stdout = global i32 0
@str = [16 x i8] c"\0Apoll() 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 = bitcast [64 x i8*]* %5 to i8*
%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 [64 x i8*], [64 x i8*]* %5, i64 0, i64 0
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
br label %29
29: ; preds = %66, %13
%30 = phi i32 [ 0, %13 ], [ %54, %66 ]
%31 = call i32 @memset(i8* %6, i32 0, i32 24)
%32 = load i32, i32* @STDIN_FILENO
store i32 %32, i32* %22
%33 = load i32, i32* @POLLIN
store i32 %33, i32* %23
store i32 %20, i32* %24
%34 = load i32, i32* @POLLPRI
store i32 %34, i32* %25
%35 = call i32 @poll(%struct.pollfd* %21, i32 2, i32 3000)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %71
39: ; preds = %29
%40 = icmp eq i32 %35, 0
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = call i32 @putchar(i32 46)
br label %43
43: ; preds = %41, %39
%44 = load i32, i32* %26
%45 = load i32, i32* @POLLPRI
%46 = and i32 %45, %44
%47 = icmp eq i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %43
%49 = load i32, i32* %24
%50 = call i32 @read(i32 %49, i8** %27, i32 64)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%52 = icmp sgt i32 %30, 0
br i1 %52, label %69, label %53
53: ; preds = %48, %43
%54 = phi i32 [ %30, %43 ], [ 1, %48 ]
%55 = load i32, i32* %28
%56 = load i32, i32* @POLLIN
%57 = and i32 %56, %55
%58 = icmp eq i32 %57, 0
br i1 %58, label %66, label %59
59: ; preds = %53
%60 = load i32, i32* %22
%61 = call i32 @read(i32 %60, i8** %27, i32 1)
%62 = load i8*, i8** %27
%63 = ptrtoint i8* %62 to i64
%64 = trunc i64 %63 to i32
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %59, %53
%67 = load i32, i32* @stdout
%68 = call i32 @fflush(i32 %67)
br label %29
69: ; preds = %48
%70 = call i32 @gpio_fd_close(i32 %20)
br label %71
71: ; preds = %69, %37
%72 = phi i32 [ -1, %37 ], [ 0, %69 ]
ret i32 %72
}