#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 */
typedef int /*<<< orphan*/ uint32_t;
struct rlimit {
  int rlim_cur;
  int rlim_max;
};

/* Variables and functions */
int FILENAME_MAX;
int /*<<< orphan*/ IN_ALL_EVENTS;
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 /*<<< orphan*/);
int inotify_init();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ szmask_to_uint32mask(char *);
int main(int argc, char *argv[]) {
  char target[FILENAME_MAX];
  int fd;
  int wd;
  struct rlimit rl;
  rl.rlim_cur = 3072;
  rl.rlim_max = 8172;
  uint32_t mask = 0;
  setrlimit(RLIMIT_NOFILE, &rl);
  if (argc < 2) {
    fprintf(stderr, "Watching the current directory\n");
    fprintf(stderr, "No mask specified using IN_ALL_EVENTS\n");
    strcpy(target, ".");
    mask = IN_ALL_EVENTS;
  } else {
    fprintf(stderr, "Watching %s\n", argv[1]);
    strcpy(target, argv[1]);
    mask = szmask_to_uint32mask(argv[2]);
  }
  fd = inotify_init();
  if (fd < 0) {
    printf("inotify_init failed\n");
    handle_error(errno);
    return -1;
  }
  wd = inotify_add_watch(fd, target, mask);
  if (wd < 0) {
    printf("add_watch failed\n");
    handle_error(errno);
    return -1;
  }
  for (;;)
    get_event(fd, target);
  return 0;
}


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)
mov    0x2e33(%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,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0xc00,-0x30(%rbp)
movl   $0x1fec,-0x2c(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1080 <setrlimit@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1252 <main+0xc2>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1292 <main+0x102>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <szmask_to_uint32mask@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1060 <inotify_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12d3 <main+0x143>
lea    0xda9(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10b0 <handle_error@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1332 <main+0x1a2>
mov    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edi
mov    -0x34(%rbp),%edx
call   1070 <inotify_add_watch@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    131c <main+0x18c>
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10b0 <handle_error@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1332 <main+0x1a2>
jmp    1321 <main+0x191>
mov    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edi
call   10a0 <get_event@plt>
jmp    1321 <main+0x191>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e35(%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,-0x28(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1080 <setrlimit@plt>
mov    0x2deb(%rip),%r12
mov    (%r12),%edi
cmp    $0x1,%ebx
jg     1229 <main+0x99>
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r12),%edi
lea    0xe19(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe32(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%r14d
jmp    1253 <main+0xc3>
mov    0x8(%r14),%rdx
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    0x10(%r14),%rdi
call   1090 <szmask_to_uint32mask@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1060 <inotify_init@plt>
test   %eax,%eax
js     128c <main+0xfc>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <inotify_add_watch@plt>
test   %eax,%eax
js     1295 <main+0x105>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
call   10a0 <get_event@plt>
jmp    1280 <main+0xf0>
lea    0xdd4(%rip),%rdi
jmp    129c <main+0x10c>
lea    0xdba(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10b0 <handle_error@plt>
mov    $0xffffffff,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
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 = [39 x i8] c"No mask specified using IN_ALL_EVENTS\0A\00"
@.str.2 = [2 x i8] c".\00"
@IN_ALL_EVENTS = global i32 0
@.str.3 = [13 x i8] c"Watching %s\0A\00"
@.str.4 = [21 x i8] c"inotify_init failed\0A\00"
@errno = global i32 0
@.str.5 = [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 %struct.rlimit
%11 = alloca i32
%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
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 0
store i32 3072, i32* %17
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 1
store i32 8172, i32* %18
store i32 0, i32* %11
%19 = load i32, i32* @RLIMIT_NOFILE
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %10)
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %30
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 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @IN_ALL_EVENTS
store i32 %29, i32* %11
br label %44
30: ; preds = %2
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @strcpy(i8* %16, i8* %38)
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @szmask_to_uint32mask(i8* %42)
store i32 %43, i32* %11
br label %44
44: ; preds = %30, %23
%45 = call i32 (...) @inotify_init()
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* @errno
%51 = call i32 @handle_error(i32 %50)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %66
52: ; preds = %44
%53 = load i32, i32* %8
%54 = load i32, i32* %11
%55 = call i32 @inotify_add_watch(i32 %53, i8* %16, i32 %54)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%60 = load i32, i32* @errno
%61 = call i32 @handle_error(i32 %60)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %66
62: ; preds = %52
br label %63
63: ; preds = %63, %62
%64 = load i32, i32* %8
%65 = call i32 @get_event(i32 %64, i8* %16)
br label %63
66: ; preds = %58, %48
%67 = load i8*, i8** %6
%68 = load i32, i32* %3
ret i32 %68
}

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 = [39 x i8] c"No mask specified using IN_ALL_EVENTS\0A\00"
@.str.2 = [2 x i8] c".\00"
@IN_ALL_EVENTS = global i32 0
@.str.3 = [13 x i8] c"Watching %s\0A\00"
@errno = global i32 0
@str = [17 x i8] c"add_watch failed\00"
@str.6 = [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 %20
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))
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @IN_ALL_EVENTS
br label %29
20: ; preds = %2
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %21
%25 = call i32 @strcpy(i8* %6, i8* %24)
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @szmask_to_uint32mask(i8* %27)
br label %29
29: ; preds = %20, %14
%30 = phi i32 [ %19, %14 ], [ %28, %20 ]
%31 = call i32 (...) @inotify_init()
%32 = icmp slt i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = call i32 @inotify_add_watch(i32 %31, i8* %6, i32 %30)
%35 = icmp slt i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33, %36
%37 = call i32 @get_event(i32 %31, i8* %6)
br label %36
38: ; preds = %33, %29
%39 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0), %29 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %33 ]
%40 = call i32 @puts(i8* dereferenceable(1) %39)
%41 = load i32, i32* @errno
%42 = call i32 @handle_error(i32 %41)
ret i32 -1
}