#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;
  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, "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) {
      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    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1204 <main+0x54>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <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    0xe25(%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    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d65(%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    12c8 <main+0x118>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x250,%rsp
pop    %rbp
ret
cmpl   $0x0,-0x40(%rbp)
jne    12e0 <main+0x130>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    0x2d06(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1323 <main+0x173>
mov    -0x24(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x40,%edx
call   1090 <read@plt>
mov    %eax,-0x248(%rbp)
mov    -0x244(%rbp),%esi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2cb3(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1363 <main+0x1b3>
mov    -0x30(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    -0x240(%rbp),%rax
mov    %eax,%esi
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1256 <main+0xa6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
cmp    $0x1,%edi
jle    1323 <main+0x153>
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    0xdf2(%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    0x2db6(%rip),%r13
mov    0x2dbf(%rip),%r12
lea    0x20(%rsp),%r15
jmp    124e <main+0x7e>
nopl   0x0(%rax,%rax,1)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    0x2d64(%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     1300 <main+0x130>
jne    129f <main+0xcf>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%eax
test   %eax,0x14(%rsp)
je     12cb <main+0xfb>
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   10b0 <read@plt>
lea    0xd46(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     1240 <main+0x70>
mov    (%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
mov    0x20(%rsp),%esi
lea    0xd37(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1240 <main+0x70>
lea    0xd40(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2d(%rip),%rdi
call   1040 <puts@plt>
lea    0xd3d(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10f0 <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 = [7 x i8] c"rising\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
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
%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* %14
%27 = load i32, i32* %14
%28 = call i32 @gpio_export(i32 %27)
%29 = load i32, i32* %14
%30 = call i32 @gpio_set_dir(i32 %29, i32 0)
%31 = load i32, i32* %14
%32 = call i32 @gpio_set_edge(i32 %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %14
%34 = call i32 @gpio_fd_open(i32 %33)
store i32 %34, i32* %10
store i32 3000, i32* %11
br label %35
35: ; preds = %22, %96
%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 = load i32, i32* %11
%54 = call i32 @poll(%struct.pollfd* %51, i32 %52, i32 %53)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %35
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
ret i32 -1
59: ; preds = %35
%60 = load i32, i32* %12
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %59
%65 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%66 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %65, i32 0, i32 2
%67 = load i32, i32* %66
%68 = load i32, i32* @POLLPRI
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %79
71: ; preds = %64
%72 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8** %75, i32 64)
store i32 %76, i32* %15
%77 = load i32, i32* %14
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %71, %64
%80 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%81 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %80, i32 0, i32 2
%82 = load i32, i32* %81
%83 = load i32, i32* @POLLIN
%84 = and i32 %82, %83
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %96
86: ; preds = %79
%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 0
%89 = load i32, i32* %88
%90 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%91 = call i32 @read(i32 %89, i8** %90, i32 1)
%92 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%93 = load i8*, i8** %92
%94 = ptrtoint i8* %93 to i32
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %94)
br label %96
96: ; preds = %86, %79
%97 = load i32, i32* @stdout
%98 = call i32 @fflush(i32 %97)
br label %35
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str.2 = [7 x i8] c"rising\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 ([7 x i8], [7 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 = %63, %13
%30 = call i32 @memset(i8* %6, i32 0, i32 24)
%31 = load i32, i32* @STDIN_FILENO
store i32 %31, i32* %22
%32 = load i32, i32* @POLLIN
store i32 %32, i32* %23
store i32 %20, i32* %24
%33 = load i32, i32* @POLLPRI
store i32 %33, i32* %25
%34 = call i32 @poll(%struct.pollfd* %21, i32 2, i32 3000)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 -1
38: ; preds = %29
%39 = icmp eq i32 %34, 0
br i1 %39, label %40, label %42
40: ; preds = %38
%41 = call i32 @putchar(i32 46)
br label %42
42: ; preds = %40, %38
%43 = load i32, i32* %26
%44 = load i32, i32* @POLLPRI
%45 = and i32 %44, %43
%46 = icmp eq i32 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %42
%48 = load i32, i32* %24
%49 = call i32 @read(i32 %48, i8** %27, i32 64)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %16)
br label %51
51: ; preds = %47, %42
%52 = load i32, i32* %28
%53 = load i32, i32* @POLLIN
%54 = and i32 %53, %52
%55 = icmp eq i32 %54, 0
br i1 %55, label %63, label %56
56: ; preds = %51
%57 = load i32, i32* %22
%58 = call i32 @read(i32 %57, i8** %27, i32 1)
%59 = load i8*, i8** %27
%60 = ptrtoint i8* %59 to i64
%61 = trunc i64 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %56, %51
%64 = load i32, i32* @stdout
%65 = call i32 @fflush(i32 %64)
br label %29
}