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

/* Variables and functions */
int POLLPRI;
unsigned int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gpio_fd_close(int /*<<< orphan*/);
int /*<<< orphan*/ gpio_fd_open(unsigned int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv, char **envp) {
  int nfds = 1;
  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]);
  timeout = (3 * 1000);
  do {
    struct pollfd fdset;
    fdset.fd = gpio_fd_open(gpio);
    fdset.events = POLLPRI;
    fdset.revents = 0;
    char c;
    len = read(fdset.fd, &c, 1);
    rc = poll(&fdset, nfds, 500);
    if (rc < 0) {
      printf("\npoll() failed!\n");
      return -1;
    }
    if (rc == 0) {
      printf(".");
    }
    if (fdset.revents & POLLPRI) {
      printf("\npoll() GPIO %d interrupt occurred\n", gpio);
    }
    fflush(stdout);
    gpio_fd_close(fdset.fd);
  } while (rc <= 0);
  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   $0x1,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d4 <main+0x54>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x234(%rbp)
movl   $0xbb8,-0x24(%rbp)
mov    -0x234(%rbp),%edi
call   1040 <gpio_fd_open@plt>
mov    %eax,-0x240(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x248(%rbp)
movl   $0x0,-0x244(%rbp)
mov    -0x240(%rbp),%edi
lea    -0x249(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x238(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x248(%rbp),%rdi
mov    $0x1f4,%edx
call   1070 <poll@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1270 <main+0xf0>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <main+0x15e>
cmpl   $0x0,-0x28(%rbp)
jne    1288 <main+0x108>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x244(%rbp),%eax
mov    0x2d5b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b4 <main+0x134>
mov    -0x234(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x240(%rbp),%edi
call   1080 <gpio_fd_close@plt>
cmpl   $0x0,-0x28(%rbp)
jle    11ee <main+0x6e>
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    $0x18,%rsp
cmp    $0x1,%edi
jle    1284 <main+0xe4>
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x2e27(%rip),%r13
lea    0x7(%rsp),%r14
lea    0x8(%rsp),%r15
mov    0x2dfe(%rip),%r12
jmp    11f6 <main+0x56>
nopl   0x0(%rax)
mov    (%r12),%edi
call   1080 <fflush@plt>
mov    0x10(%rsp),%edi
call   10a0 <gpio_fd_close@plt>
test   %ebp,%ebp
jne    1273 <main+0xd3>
mov    %ebx,%edi
call   1060 <gpio_fd_open@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%r13),%ecx
mov    %ecx,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1f4,%edx
call   1090 <poll@plt>
test   %eax,%eax
js     1260 <main+0xc0>
mov    %eax,%ebp
jne    1244 <main+0xa4>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x0(%r13),%eax
test   %eax,0xc(%rsp)
je     11e0 <main+0x40>
lea    0xdab(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e0 <main+0x40>
lea    0xdbd(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    1275 <main+0xd5>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <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"
@POLLPRI = global i32 0
@.str.2 = [17 x i8] c"\0Apoll() failed!\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [64 x i8*]
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.pollfd
%16 = alloca i8
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 1, i32* %8
%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* %13
store i32 3000, i32* %10
br label %28
28: ; preds = %64, %23
%29 = load i32, i32* %13
%30 = call i32 @gpio_fd_open(i32 %29)
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @POLLPRI
%33 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 0
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 1
store i32 0, i32* %34
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @read(i32 %36, i8* %16, i32 1)
store i32 %37, i32* %14
%38 = load i32, i32* %8
%39 = call i32 @poll(%struct.pollfd* %15, i32 %38, i32 500)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %28
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %68
44: ; preds = %28
%45 = load i32, i32* %11
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i32, i32* @POLLPRI
%53 = and i32 %51, %52
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i32, i32* %13
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %55, %49
%59 = load i32, i32* @stdout
%60 = call i32 @fflush(i32 %59)
%61 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
%62 = load i32, i32* %61
%63 = call i32 @gpio_fd_close(i32 %62)
br label %64
64: ; preds = %58
%65 = load i32, i32* %11
%66 = icmp sle i32 %65, 0
br i1 %66, label %28, label %67
67: ; preds = %64
store i32 0, i32* %4
br label %68
68: ; preds = %67, %42
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLPRI = global i32 0
@.str.4 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@stdout = global i32 0
@str = [16 x i8] c"\0Apoll() failed!\00"
@str.5 = [28 x i8] c"Usage: gpio-int <gpio-pin>\0A\00"
@str.6 = [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 %struct.pollfd
%5 = alloca i8
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %3
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = bitcast %struct.pollfd* %4 to i8*
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
br label %19
19: ; preds = %38, %11
%20 = call i32 @gpio_fd_open(i32 %14)
store i32 %20, i32* %16
%21 = load i32, i32* @POLLPRI
store i32 %21, i32* %17
store i32 0, i32* %18
%22 = call i32 @read(i32 %20, i8* %5, i32 1)
%23 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 500)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %43
27: ; preds = %19
%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* %18
%33 = load i32, i32* @POLLPRI
%34 = and i32 %33, %32
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %14)
br label %38
38: ; preds = %36, %31
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = load i32, i32* %16
%42 = call i32 @gpio_fd_close(i32 %41)
br i1 %28, label %19, label %43
43: ; preds = %38, %25
%44 = phi i32 [ -1, %25 ], [ 0, %38 ]
ret i32 %44
}