#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 inotify_event {
  int len;
  int mask;
  char *name;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int IN_CREATE;
int IN_DELETE;
int IN_ISDIR;
int /*<<< orphan*/ close(int);
int inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ inotify_rm_watch(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int main(void) {
  int length, i = 0;
  int fd;
  int wd;
  char buffer[(1024 * ((sizeof(struct inotify_event)) + 16))];
  fd = inotify_init();
  if (fd < 0) {
    perror("inotify_init");
    return EXIT_FAILURE;
  }
  wd = inotify_add_watch(fd, "/tmp", IN_CREATE | IN_DELETE);
  for (;;) {
    i = 0;
    length = read(fd, buffer, (1024 * ((sizeof(struct inotify_event)) + 16)));
    if (length < 0)
      perror("read");
    while (i < length) {
      struct inotify_event *event = (struct inotify_event *)&buffer[i];
      if ((event->len < 1) || (event->mask & IN_ISDIR))
        continue;
      if (event->mask & IN_CREATE)
        printf("File %s created.\n", event->name);
      else if (event->mask & IN_DELETE)
        printf("File %s deleted.\n", event->name);
      i += (sizeof(struct inotify_event)) + event->len;
    }
  }
  inotify_rm_watch(fd, wd);
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8030,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1050 <inotify_init@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    119b <main+0x4b>
lea    0xe7c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
add    $0x8030,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
lea    0xe56(%rip),%rsi
call   1060 <inotify_add_watch@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x8000,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f3 <main+0xa3>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
jmp    11f8 <main+0xa8>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12d7 <main+0x187>
movslq -0xc(%rbp),%rcx
lea    -0x8020(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8028(%rbp)
mov    -0x8028(%rbp),%rax
cmpl   $0x1,(%rax)
jl     1245 <main+0xf5>
mov    -0x8028(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2da6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     124a <main+0xfa>
jmp    11f8 <main+0xa8>
mov    -0x8028(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2d75(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1282 <main+0x132>
mov    -0x8028(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd9f(%rip),%rdi
call   1030 <printf@plt>
jmp    12ba <main+0x16a>
mov    -0x8028(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2d55(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b5 <main+0x165>
mov    -0x8028(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd79(%rip),%rdi
call   1030 <printf@plt>
jmp    12ba <main+0x16a>
mov    -0x8028(%rbp),%rax
movslq (%rax),%rcx
add    $0x10,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
jmp    11f8 <main+0xa8>
jmp    11bf <main+0x6f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
xor    %eax,%eax
call   1050 <inotify_init@plt>
test   %eax,%eax
js     1227 <main+0xd7>
mov    0x2e59(%rip),%rbx
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%edx
or     (%rbx),%edx
lea    0xe84(%rip),%rsi
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1060 <inotify_add_watch@plt>
mov    0x2e45(%rip),%r15
lea    0xe75(%rip),%r14
jmp    11bc <main+0x6c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x8000,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11b0 <main+0x60>
je     11bc <main+0x6c>
mov    %eax,%ebp
xor    %r13d,%r13d
jmp    11f6 <main+0xa6>
nopl   0x0(%rax)
mov    0x18(%rsp,%rax,1),%rsi
call   1030 <printf@plt>
add    %r12d,%r13d
add    $0x10,%r13d
cmp    %ebp,%r13d
jge    11bc <main+0x6c>
movslq %r13d,%rax
mov    0x10(%rsp,%rax,1),%r12d
test   %r12d,%r12d
jle    11f1 <main+0xa1>
mov    0x14(%rsp,%rax,1),%ecx
test   %ecx,(%r15)
jne    11f1 <main+0xa1>
mov    %r14,%rdi
test   %ecx,(%rbx)
jne    11e0 <main+0x90>
lea    0xe0f(%rip),%rdi
mov    0x2dc7(%rip),%rdx
test   %ecx,(%rdx)
jne    11e0 <main+0x90>
jmp    11ea <main+0x9a>
lea    0xdd2(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
add    $0x8018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32, i32, i8* }
@.str = [13 x i8] c"inotify_init\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
@IN_CREATE = global i32 0
@IN_DELETE = global i32 0
@.str.2 = [5 x i8] c"read\00"
@IN_ISDIR = global i32 0
@.str.3 = [18 x i8] c"File %s created.\0A\00"
@.str.4 = [18 x i8] c"File %s deleted.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32768 x i8]
%7 = alloca %struct.inotify_event*
store i32 0, i32* %1
store i32 0, i32* %3
%8 = call i32 (...) @inotify_init()
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
ret i32 %13
14: ; preds = %0
%15 = load i32, i32* %4
%16 = load i32, i32* @IN_CREATE
%17 = load i32, i32* @IN_DELETE
%18 = or i32 %16, %17
%19 = call i32 @inotify_add_watch(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %5
br label %20
20: ; preds = %85, %14
store i32 0, i32* %3
%21 = load i32, i32* %4
%22 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 32768)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
br label %29
29: ; preds = %75, %49, %28
%30 = load i32, i32* %3
%31 = load i32, i32* %2
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %85
33: ; preds = %29
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 %35
%37 = bitcast i8* %36 to %struct.inotify_event*
store %struct.inotify_event* %37, %struct.inotify_event** %7
%38 = load %struct.inotify_event*, %struct.inotify_event** %7
%39 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = icmp slt i32 %40, 1
br i1 %41, label %49, label %42
42: ; preds = %33
%43 = load %struct.inotify_event*, %struct.inotify_event** %7
%44 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32, i32* @IN_ISDIR
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %42, %33
br label %29
50: ; preds = %42
%51 = load %struct.inotify_event*, %struct.inotify_event** %7
%52 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load i32, i32* @IN_CREATE
%55 = and i32 %53, %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %50
%58 = load %struct.inotify_event*, %struct.inotify_event** %7
%59 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %58, i32 0, i32 2
%60 = load i8*, i8** %59
%61 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %60)
br label %75
62: ; preds = %50
%63 = load %struct.inotify_event*, %struct.inotify_event** %7
%64 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = load i32, i32* @IN_DELETE
%67 = and i32 %65, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %62
%70 = load %struct.inotify_event*, %struct.inotify_event** %7
%71 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %70, i32 0, i32 2
%72 = load i8*, i8** %71
%73 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %69, %62
br label %75
75: ; preds = %74, %57
%76 = load %struct.inotify_event*, %struct.inotify_event** %7
%77 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = sext i32 %78 to i64
%80 = add i64 16, %79
%81 = load i32, i32* %3
%82 = sext i32 %81 to i64
%83 = add i64 %82, %80
%84 = trunc i64 %83 to i32
store i32 %84, i32* %3
br label %29
85: ; preds = %29
br label %20
}

LLVM IR (-O2):
@.str = [13 x i8] c"inotify_init\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
@IN_CREATE = global i32 0
@IN_DELETE = global i32 0
@.str.2 = [5 x i8] c"read\00"
@IN_ISDIR = global i32 0
@.str.3 = [18 x i8] c"File %s created.\0A\00"
@.str.4 = [18 x i8] c"File %s deleted.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [32768 x i8]
%2 = getelementptr inbounds [32768 x i8], [32768 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @inotify_init()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
ret i32 %7
8: ; preds = %0
%9 = load i32, i32* @IN_CREATE
%10 = load i32, i32* @IN_DELETE
%11 = or i32 %10, %9
%12 = tail call i32 @inotify_add_watch(i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %18, %8
%14 = call i32 @read(i32 %3, i8* %2, i32 32768)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %52, %16, %19
br label %13
19: ; preds = %13
%20 = icmp eq i32 %14, 0
br i1 %20, label %18, label %21
21: ; preds = %19, %52
%22 = phi i32 [ %53, %52 ], [ 0, %19 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [32768 x i8], [32768 x i8]* %1, i64 0, i64 %23
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = icmp slt i32 %26, 1
br i1 %27, label %52, label %28
28: ; preds = %21
%29 = getelementptr inbounds i8, i8* %24, i64 4
%30 = bitcast i8* %29 to i32*
%31 = load i32, i32* %30
%32 = load i32, i32* @IN_ISDIR
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %28
%36 = load i32, i32* @IN_CREATE
%37 = and i32 %36, %31
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* @IN_DELETE
%41 = and i32 %40, %31
%42 = icmp eq i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %39, %35
%44 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %35 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), %39 ]
%45 = getelementptr inbounds i8, i8* %24, i64 8
%46 = bitcast i8* %45 to i8**
%47 = load i8*, i8** %46
%48 = tail call i32 @printf(i8* %44, i8* %47)
br label %49
49: ; preds = %43, %39
%50 = add i32 %22, 16
%51 = add i32 %50, %26
br label %52
52: ; preds = %21, %28, %49
%53 = phi i32 [ %51, %49 ], [ %22, %28 ], [ %22, %21 ]
%54 = icmp slt i32 %53, %14
br i1 %54, label %21, label %18
}