#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 nfds_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int FAN_CLASS_CONTENT;
int FAN_CLOEXEC;
int FAN_CLOSE_WRITE;
int FAN_ENABLE_AUDIT;
int FAN_MARK_ADD;
int FAN_MARK_MOUNT;
int FAN_NONBLOCK;
int FAN_OPEN_PERM;
int /*<<< orphan*/ O_RDONLY;
int POLLIN;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fanotify_init(int, int /*<<< orphan*/);
int fanotify_mark(int, int, int, int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ handle_events(int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd, poll_num;
  nfds_t nfds;
  struct pollfd fds[2];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s MOUNT\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK |
                         FAN_ENABLE_AUDIT,
                     O_RDONLY);
  if (fd == -1) {
    perror("fanotify_init");
    exit(EXIT_FAILURE);
  }
  if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
                    FAN_OPEN_PERM | FAN_CLOSE_WRITE, -1, argv[1]) == -1) {
    perror("fanotify_mark");
    exit(EXIT_FAILURE);
  }
  nfds = 1;
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  printf("Listening for events.\n");
  while (1) {
    poll_num = poll(fds, nfds, -1);
    if (poll_num == -1) {
      if (errno == EINTR)
        continue;
      perror("poll");
      exit(EXIT_FAILURE);
    }
    if (poll_num > 0) {
      if (fds[0].revents & POLLIN) {
        handle_events(fd);
      }
    }
  }
  printf("Listening for events stopped.\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ca <main+0x4a>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
or     (%rax),%edi
mov    0x2dfd(%rip),%rax
or     (%rax),%edi
mov    0x2d8c(%rip),%rax
or     (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1030 <fanotify_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1223 <main+0xa3>
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
mov    $0xffffffff,%ecx
call   1070 <fanotify_mark@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xff>
lea    0xdb3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    0xd8e(%rip),%rdi
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12fb <main+0x17b>
mov    0x2ccf(%rip),%rax
mov    (%rax),%rax
mov    0x2cb5(%rip),%rcx
cmp    (%rcx),%rax
jne    12e1 <main+0x161>
jmp    12a4 <main+0x124>
lea    0xd5c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jle    1327 <main+0x1a7>
mov    -0x38(%rbp),%eax
mov    0x2cb9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1322 <main+0x1a2>
mov    -0x14(%rbp),%edi
call   1060 <handle_events@plt>
jmp    1327 <main+0x1a7>
jmp    12a4 <main+0x124>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11d7 <main+0x57>
mov    0x2de5(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    0x2e33(%rip),%rax
or     (%rax),%edi
mov    0x2dc2(%rip),%rax
or     (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1040 <fanotify_init@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x74>
lea    0xe3f(%rip),%rdi
jmp    12af <main+0x12f>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe16(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b4 <main+0x134>
mov    %eax,%r15d
mov    0x2daa(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2da0(%rip),%rax
mov    0x2db9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%r14),%r8
mov    %r15d,%edi
mov    $0xffffffff,%ecx
call   1070 <fanotify_mark@plt>
cmp    $0xffffffff,%eax
jne    123a <main+0xba>
lea    0xde7(%rip),%rdi
jmp    12af <main+0x12f>
mov    %r15d,(%rsp)
mov    0x2d83(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    0x2d36(%rip),%r12
mov    0x2d1f(%rip),%rbp
jmp    127a <main+0xfa>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%rbp),%rax
jne    12a8 <main+0x128>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0xf0>
test   %eax,%eax
jle    127a <main+0xfa>
mov    (%r14),%eax
test   %eax,0x8(%rsp)
je     127a <main+0xfa>
mov    %r15d,%edi
call   1060 <handle_events@plt>
jmp    127a <main+0xfa>
lea    0xd7e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [17 x i8] c"Usage: %s MOUNT\0A\00"
@EXIT_FAILURE = global i32 0
@FAN_CLOEXEC = global i32 0
@FAN_CLASS_CONTENT = global i32 0
@FAN_NONBLOCK = global i32 0
@FAN_ENABLE_AUDIT = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"fanotify_init\00"
@FAN_MARK_ADD = global i32 0
@FAN_MARK_MOUNT = global i32 0
@FAN_OPEN_PERM = global i32 0
@FAN_CLOSE_WRITE = global i32 0
@.str.2 = [14 x i8] c"fanotify_mark\00"
@POLLIN = global i32 0
@.str.3 = [23 x i8] c"Listening for events.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [5 x i8] c"poll\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x %struct.pollfd]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i32, i32* @FAN_CLOEXEC
%22 = load i32, i32* @FAN_CLASS_CONTENT
%23 = or i32 %21, %22
%24 = load i32, i32* @FAN_NONBLOCK
%25 = or i32 %23, %24
%26 = load i32, i32* @FAN_ENABLE_AUDIT
%27 = or i32 %25, %26
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @fanotify_init(i32 %27, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %20
%37 = load i32, i32* %6
%38 = load i32, i32* @FAN_MARK_ADD
%39 = load i32, i32* @FAN_MARK_MOUNT
%40 = or i32 %38, %39
%41 = load i32, i32* @FAN_OPEN_PERM
%42 = load i32, i32* @FAN_CLOSE_WRITE
%43 = or i32 %41, %42
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @fanotify_mark(i32 %37, i32 %40, i32 %43, i32 -1, i8* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %36
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %36
store i32 1, i32* %8
%54 = load i32, i32* %6
%55 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %9, i64 0, i64 0
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 0
store i32 %54, i32* %56
%57 = load i32, i32* @POLLIN
%58 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %9, i64 0, i64 0
%59 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %58, i32 0, i32 1
store i32 %57, i32* %59
%60 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %53, %71, %90
%62 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %9, i64 0, i64 0
%63 = load i32, i32* %8
%64 = call i32 @poll(%struct.pollfd* %62, i32 %63, i32 -1)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %76
67: ; preds = %61
%68 = load i64, i64* @errno
%69 = load i64, i64* @EINTR
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %67
br label %61
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %61
%77 = load i32, i32* %7
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %90
79: ; preds = %76
%80 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %9, 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 %89
86: ; preds = %79
%87 = load i32, i32* %6
%88 = call i32 @handle_events(i32 %87)
br label %89
89: ; preds = %86, %79
br label %90
90: ; preds = %89, %76
br label %61
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [17 x i8] c"Usage: %s MOUNT\0A\00"
@EXIT_FAILURE = global i32 0
@FAN_CLOEXEC = global i32 0
@FAN_CLASS_CONTENT = global i32 0
@FAN_NONBLOCK = global i32 0
@FAN_ENABLE_AUDIT = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"fanotify_init\00"
@FAN_MARK_ADD = global i32 0
@FAN_MARK_MOUNT = global i32 0
@FAN_OPEN_PERM = global i32 0
@FAN_CLOSE_WRITE = global i32 0
@.str.2 = [14 x i8] c"fanotify_mark\00"
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [5 x i8] c"poll\00"
@EXIT_SUCCESS = global i32 0
@str = [22 x i8] c"Listening for events.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x %struct.pollfd]
%4 = bitcast [2 x %struct.pollfd]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = load i32, i32* @FAN_CLOEXEC
%14 = load i32, i32* @FAN_CLASS_CONTENT
%15 = or i32 %14, %13
%16 = load i32, i32* @FAN_NONBLOCK
%17 = or i32 %15, %16
%18 = load i32, i32* @FAN_ENABLE_AUDIT
%19 = or i32 %17, %18
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @fanotify_init(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %12
%24 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %12
%28 = load i32, i32* @FAN_MARK_ADD
%29 = load i32, i32* @FAN_MARK_MOUNT
%30 = or i32 %29, %28
%31 = load i32, i32* @FAN_OPEN_PERM
%32 = load i32, i32* @FAN_CLOSE_WRITE
%33 = or i32 %32, %31
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = tail call i32 @fanotify_mark(i32 %21, i32 %30, i32 %33, i32 -1, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %27
%39 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %27
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%44 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %21, i32* %44
%45 = load i32, i32* @POLLIN
%46 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %45, i32* %46
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%48 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
br label %49
49: ; preds = %62, %42
%50 = call i32 @poll(%struct.pollfd* %43, i32 1, i32 -1)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %60
52: ; preds = %49
%53 = load i64, i64* @errno
%54 = load i64, i64* @EINTR
%55 = icmp eq i64 %53, %54
br i1 %55, label %62, label %56
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %49
%61 = icmp sgt i32 %50, 0
br i1 %61, label %63, label %62
62: ; preds = %60, %68, %63, %52
br label %49
63: ; preds = %60
%64 = load i32, i32* %48
%65 = load i32, i32* @POLLIN
%66 = and i32 %65, %64
%67 = icmp eq i32 %66, 0
br i1 %67, label %62, label %68
68: ; preds = %63
%69 = call i32 @handle_events(i32 %21)
br label %62
}