#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 rlimit {
  int rlim_cur;
  int rlim_max;
};

/* Variables and functions */
int FILENAME_MAX;
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ get_event(int, char *);
int /*<<< orphan*/ handle_error(int /*<<< orphan*/);
int inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  char target[FILENAME_MAX];
  int ev_count = 0;
  int fd;
  int wd;
  struct rlimit rl;
  rl.rlim_cur = 3072;
  rl.rlim_max = 8172;
  setrlimit(RLIMIT_NOFILE, &rl);
  if (argc < 2) {
    fprintf(stderr, "Watching the current directory\n");
    strcpy(target, ".");
  } else {
    fprintf(stderr, "Watching %s\n", argv[1]);
    strcpy(target, argv[1]);
  }
  fd = inotify_init();
  if (fd < 0) {
    printf("inotify_init failed\n");
    handle_error(errno);
    return 1;
  }
  wd = inotify_add_watch(fd, target,
                         (0x00000001 | 0x00000002 | 0x00000004 | 0x00000008 |
                          0x00000010 | 0x00000020 | 0x00000040 | 0x00000800 |
                          0x00000080 | 0x00000200 | 0x00000100 | 0x00000400));
  if (wd < 0) {
    printf("add_watch failed\n");
    handle_error(errno);
    return 1;
  }
  while (1) {
    ++ev_count;
    get_event(fd, target);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xc00,-0x38(%rbp)
movl   $0x1fec,-0x34(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1080 <setrlimit@plt>
cmpl   $0x2,-0x8(%rbp)
jge    121f <main+0x9f>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <strcpy@plt>
jmp    124f <main+0xcf>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    $0x0,%al
call   1060 <inotify_init@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1290 <main+0x110>
lea    0xdc5(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10a0 <handle_error@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12ff <main+0x17f>
mov    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%edi
mov    $0xfff,%edx
call   1070 <inotify_add_watch@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12db <main+0x15b>
lea    0xd8f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10a0 <handle_error@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12ff <main+0x17f>
jmp    12e0 <main+0x160>
jmp    12e5 <main+0x165>
mov    -0x48(%rbp),%rsi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <get_event@plt>
jmp    12e0 <main+0x160>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
movabs $0x1fec00000c00,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <setrlimit@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
cmp    $0x1,%ebx
jg     11ee <main+0x6e>
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe34(%rip),%rsi
jmp    1204 <main+0x84>
mov    0x8(%r14),%rdx
lea    0xe29(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
call   1060 <inotify_init@plt>
test   %eax,%eax
js     123c <main+0xbc>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    $0xfff,%edx
call   1070 <inotify_add_watch@plt>
test   %eax,%eax
js     1245 <main+0xc5>
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
call   1090 <get_event@plt>
jmp    1230 <main+0xb0>
lea    0xdfd(%rip),%rdi
jmp    124c <main+0xcc>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10a0 <handle_error@plt>
mov    $0x1,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@FILENAME_MAX = global i32 0
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Watching the current directory\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [13 x i8] c"Watching %s\0A\00"
@.str.3 = [21 x i8] c"inotify_init failed\0A\00"
@errno = global i32 0
@.str.4 = [18 x i8] c"add_watch failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.rlimit
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @FILENAME_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
store i32 0, i32* %8
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %11, i32 0, i32 0
store i32 3072, i32* %17
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %11, i32 0, i32 1
store i32 8172, i32* %18
%19 = load i32, i32* @RLIMIT_NOFILE
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %11)
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %37
27: ; preds = %2
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strcpy(i8* %16, i8* %35)
br label %37
37: ; preds = %27, %23
%38 = call i32 (...) @inotify_init()
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 @handle_error(i32 %43)
store i32 1, i32* %3
store i32 1, i32* %12
br label %61
45: ; preds = %37
%46 = load i32, i32* %9
%47 = call i32 @inotify_add_watch(i32 %46, i8* %16, i32 4095)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* @errno
%53 = call i32 @handle_error(i32 %52)
store i32 1, i32* %3
store i32 1, i32* %12
br label %61
54: ; preds = %45
br label %55
55: ; preds = %56, %54
br label %56
56: ; preds = %55
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
%59 = load i32, i32* %9
%60 = call i32 @get_event(i32 %59, i8* %16)
br label %55
61: ; preds = %50, %41
%62 = load i8*, i8** %6
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@FILENAME_MAX = global i32 0
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Watching the current directory\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [13 x i8] c"Watching %s\0A\00"
@errno = global i32 0
@str = [17 x i8] c"add_watch failed\00"
@str.5 = [20 x i8] c"inotify_init failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.rlimit
%4 = load i32, i32* @FILENAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.rlimit* %3 to i8*
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i32 3072, i32* %8
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i32 8172, i32* %9
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i32 @setrlimit(i32 %10, %struct.rlimit* %3)
%12 = icmp slt i32 %0, 2
%13 = load i32, i32* @stderr
br i1 %12, label %14, label %16
14: ; preds = %2
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %21
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %17
br label %21
21: ; preds = %16, %14
%22 = phi i8* [ %20, %16 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %14 ]
%23 = call i32 @strcpy(i8* %6, i8* %22)
%24 = call i32 (...) @inotify_init()
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = call i32 @inotify_add_watch(i32 %24, i8* %6, i32 4095)
%28 = icmp slt i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26, %29
%30 = call i32 @get_event(i32 %24, i8* %6)
br label %29
31: ; preds = %26, %21
%32 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0), %21 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %26 ]
%33 = call i32 @puts(i8* dereferenceable(1) %32)
%34 = load i32, i32* @errno
%35 = call i32 @handle_error(i32 %34)
ret i32 1
}