#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 O_NONBLOCK;
int O_RDONLY;
int POLLPRI;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
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[1];
  int nfds = 1;
  int gpio_fd, timeout, rc;
  int len;
  char buf[64];
  if (argc < 2) {
    printf("Usage: %s <file>\n\n", argv[0]);
    printf(
        "Waits for a change in the GPIO pin voltage level or input on stdin\n");
    exit(-1);
  }
  gpio_fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (gpio_fd < 0) {
    perror("file open");
  }
  timeout = -1;
  int num_break = 2;
  while (1) {
    memset((void *)fdset, 0, sizeof(fdset));
    fdset[0].fd = gpio_fd;
    fdset[0].events = POLLPRI;
    rc = poll(fdset, nfds, timeout);
    if (rc < 0) {
      printf("\npoll() failed!\n");
      return -1;
    }
    printf(".");
    if (fdset[0].revents & POLLPRI) {
      len = read(fdset[0].fd, buf, 64);
      printf("poll() %s interrupt occurred\n", argv[1]);
      num_break--;
      if (num_break <= 0)
        break;
    }
    fflush(stdout);
  }
  close(gpio_fd);
  if (num_break <= 0)
    return 0;
  else
    return 1;
}


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   $0x1,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11eb <main+0x5b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1223 <main+0x93>
lea    0xe39(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x34(%rbp)
movl   $0x2,-0x84(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1080 <poll@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    128c <main+0xfc>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133a <main+0x1aa>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    0x2d4c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ff <main+0x16f>
mov    -0x28(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x84(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jg     12fa <main+0x16a>
jmp    1312 <main+0x182>
jmp    12ff <main+0x16f>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1234 <main+0xa4>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x84(%rbp)
jg     1333 <main+0x1a3>
movl   $0x0,-0x4(%rbp)
jmp    133a <main+0x1aa>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r15
cmp    $0x1,%edi
jle    12f7 <main+0x147>
mov    0x8(%r15),%rdi
mov    0x2e03(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12e6 <main+0x136>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebp,(%rsp)
mov    0x2de5(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   10a0 <poll@plt>
test   %eax,%eax
js     12c6 <main+0x116>
mov    $0x2,%r12d
mov    0x2d8f(%rip),%r13
mov    %rsp,%rbx
jmp    1278 <main+0xc8>
xchg   %ax,%ax
mov    0x0(%r13),%edi
call   1090 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebp,(%rsp)
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   10a0 <poll@plt>
test   %eax,%eax
js     12c6 <main+0x116>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r14),%eax
test   %eax,0x8(%rsp)
je     1240 <main+0x90>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x40,%edx
call   1080 <read@plt>
mov    0x8(%r15),%rsi
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x2,%r12d
mov    $0x1,%r12d
jge    1240 <main+0x90>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    12d7 <main+0x127>
lea    0xd6e(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd26(%rip),%rdi
call   10c0 <perror@plt>
jmp    11ef <main+0x3f>
mov    (%r15),%rsi
lea    0xcff(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd3c(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [19 x i8] c"Usage: %s <file>\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"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [10 x i8] c"file open\00"
@POLLPRI = global i32 0
@.str.3 = [17 x i8] c"\0Apoll() failed!\0A\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [30 x i8] c"poll() %s 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 [1 x %struct.pollfd]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [64 x i8]
%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 1, i32* %9
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %25
18: ; preds = %3
%19 = load i8**, i8*** %6
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %3
%26 = load i8**, i8*** %6
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDONLY
%30 = load i32, i32* @O_NONBLOCK
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %25
store i32 -1, i32* %11
store i32 2, i32* %15
br label %38
38: ; preds = %37, %80
%39 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%40 = bitcast %struct.pollfd* %39 to i8*
%41 = call i32 @memset(i8* %40, i32 0, i32 12)
%42 = load i32, i32* %10
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %43, i32 0, i32 0
store i32 %42, i32* %44
%45 = load i32, i32* @POLLPRI
%46 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %46, i32 0, i32 1
store i32 %45, i32* %47
%48 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%49 = load i32, i32* %9
%50 = load i32, i32* %11
%51 = call i32 @poll(%struct.pollfd* %48, i32 %49, i32 %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %38
%55 = 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 %90
56: ; preds = %38
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%58 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%59 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %58, i32 0, i32 2
%60 = load i32, i32* %59
%61 = load i32, i32* @POLLPRI
%62 = and i32 %60, %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %80
64: ; preds = %56
%65 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%66 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 64)
store i32 %69, i32* %13
%70 = load i8**, i8*** %6
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %72)
%74 = load i32, i32* %15
%75 = add nsw i32 %74, -1
store i32 %75, i32* %15
%76 = load i32, i32* %15
%77 = icmp sle i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %64
br label %83
79: ; preds = %64
br label %80
80: ; preds = %79, %56
%81 = load i32, i32* @stdout
%82 = call i32 @fflush(i32 %81)
br label %38
83: ; preds = %78
%84 = load i32, i32* %10
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %15
%87 = icmp sle i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %83
store i32 0, i32* %4
br label %90
89: ; preds = %83
store i32 1, i32* %4
br label %90
90: ; preds = %89, %88, %54
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [19 x i8] c"Usage: %s <file>\0A\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [10 x i8] c"file open\00"
@POLLPRI = global i32 0
@.str.5 = [30 x i8] c"poll() %s interrupt occurred\0A\00"
@stdout = global i32 0
@str = [16 x i8] c"\0Apoll() failed!\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 [1 x %struct.pollfd]
%5 = alloca [64 x i8]
%6 = bitcast [1 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 %14
9: ; preds = %3
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.6, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %3
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* %16, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %14
%25 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%26 = call i32 @memset(i8* %6, i32 0, i32 12)
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %20, i32* %27
%28 = load i32, i32* @POLLPRI
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @poll(%struct.pollfd* %25, i32 1, i32 -1)
%31 = icmp slt i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
br label %36
34: ; preds = %49, %24
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %59
36: ; preds = %32, %49
%37 = phi i32 [ 2, %32 ], [ %50, %49 ]
%38 = call i32 @putchar(i32 46)
%39 = load i32, i32* %33
%40 = load i32, i32* @POLLPRI
%41 = and i32 %40, %39
%42 = icmp eq i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %36
%44 = load i32, i32* %27
%45 = call i32 @read(i32 %44, i8* %7, i32 64)
%46 = load i8*, i8** %15
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %46)
%48 = icmp slt i32 %37, 2
br i1 %48, label %57, label %49
49: ; preds = %43, %36
%50 = phi i32 [ 1, %43 ], [ %37, %36 ]
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
%53 = call i32 @memset(i8* %6, i32 0, i32 12)
store i32 %20, i32* %27
%54 = load i32, i32* @POLLPRI
store i32 %54, i32* %29
%55 = call i32 @poll(%struct.pollfd* %25, i32 1, i32 -1)
%56 = icmp slt i32 %55, 0
br i1 %56, label %34, label %36
57: ; preds = %43
%58 = call i32 @close(i32 %20)
br label %59
59: ; preds = %57, %34
%60 = phi i32 [ -1, %34 ], [ 0, %57 ]
ret i32 %60
}