#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 mask;
};

/* Variables and functions */
int IN_MODIFY;
int NAME_MAX;
int inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ perror(char *);
int xread(int, char *, int);
int file_watch(char *path, void (*callback)(char *filepath)) {
  char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
  struct inotify_event *event;
  int fd, wd;
  if ((fd = inotify_init()) == -1) {
    perror("inotify_init");
    return -1;
  }
  if ((wd = inotify_add_watch(fd, path, IN_MODIFY)) == -1) {
    perror("inotify_add_watch");
    return -1;
  }
  while (1) {
    if ((xread(fd, buf, sizeof(struct inotify_event) + NAME_MAX + 1)) == -1) {
      perror("read");
      return -1;
    }
    event = (struct inotify_event *)buf;
    if ((event->mask & IN_MODIFY) == IN_MODIFY)
      callback(path);
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_watch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
movslq (%rax),%rdx
mov    %rdx,%rax
add    $0x5,%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
add    $0x14,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <inotify_init@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <file_watch+0x73>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    127a <file_watch+0x13a>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1050 <inotify_add_watch@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    11f3 <file_watch+0xb3>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    127a <file_watch+0x13a>
jmp    11f8 <file_watch+0xb8>
jmp    11fd <file_watch+0xbd>
mov    -0x48(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    0x2dc5(%rip),%rax
movslq (%rax),%rax
add    $0x4,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1040 <xread@plt>
cmp    $0xffffffff,%eax
jne    1245 <file_watch+0x105>
lea    0xdf2(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    127a <file_watch+0x13a>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    0x2d96(%rip),%rcx
and    (%rcx),%eax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1275 <file_watch+0x135>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
jmp    11f8 <file_watch+0xb8>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_watch>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2e74(%rip),%r13
movslq 0x0(%r13),%rax
mov    %rsp,%rbx
add    $0x14,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1030 <inotify_init@plt>
cmp    $0xffffffff,%eax
jne    1186 <file_watch+0x46>
lea    0xe7c(%rip),%rdi
jmp    11df <file_watch+0x9f>
mov    %eax,%r12d
mov    0x2e60(%rip),%r14
mov    (%r14),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <inotify_add_watch@plt>
cmp    $0xffffffff,%eax
jne    11b0 <file_watch+0x70>
lea    0xe64(%rip),%rdi
jmp    11df <file_watch+0x9f>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
add    $0x5,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1040 <xread@plt>
cmp    $0xffffffff,%eax
je     11d8 <file_watch+0x98>
mov    (%rbx),%eax
not    %eax
test   %eax,(%r14)
jne    11b0 <file_watch+0x70>
mov    %r15,%rdi
call   *-0x30(%rbp)
jmp    11b0 <file_watch+0x70>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32 }
@NAME_MAX = global i32 0
@.str = [13 x i8] c"inotify_init\00"
@IN_MODIFY = global i32 0
@.str.1 = [18 x i8] c"inotify_add_watch\00"
@.str.2 = [5 x i8] c"read\00"
define i32 @file_watch(i8* %0, void (i8*)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca void (i8*)*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.inotify_event*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store void (i8*)* %1, void (i8*)** %5
%12 = load i32, i32* @NAME_MAX
%13 = sext i32 %12 to i64
%14 = add i64 4, %13
%15 = add i64 %14, 1
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = call i32 (...) @inotify_init()
store i32 %18, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
22: ; preds = %2
%23 = load i32, i32* %9
%24 = load i8*, i8** %4
%25 = load i32, i32* @IN_MODIFY
%26 = call i32 @inotify_add_watch(i32 %23, i8* %24, i32 %25)
store i32 %26, i32* %10
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
30: ; preds = %22
br label %31
31: ; preds = %55, %30
br label %32
32: ; preds = %31
%33 = load i32, i32* %9
%34 = load i32, i32* @NAME_MAX
%35 = sext i32 %34 to i64
%36 = add i64 4, %35
%37 = add i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i32 @xread(i32 %33, i8* %17, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
43: ; preds = %32
%44 = bitcast i8* %17 to %struct.inotify_event*
store %struct.inotify_event* %44, %struct.inotify_event** %8
%45 = load %struct.inotify_event*, %struct.inotify_event** %8
%46 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32, i32* @IN_MODIFY
%49 = and i32 %47, %48
%50 = load i32, i32* @IN_MODIFY
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = load void (i8*)*, void (i8*)** %5
%54 = load i8*, i8** %4
br label %55
55: ; preds = %52, %43
br label %31
56: ; preds = %41, %28, %20
%57 = load i8*, i8** %6
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str = [13 x i8] c"inotify_init\00"
@IN_MODIFY = global i32 0
@.str.1 = [18 x i8] c"inotify_add_watch\00"
@.str.2 = [5 x i8] c"read\00"
define i32 @file_watch(i8* %0, void (i8*)* nocapture %1) {
%3 = load i32, i32* @NAME_MAX
%4 = sext i32 %3 to i64
%5 = add nsw i64 %4, 5
%6 = alloca i8, i64 %5
%7 = tail call i32 (...) @inotify_init()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %37
11: ; preds = %2
%12 = load i32, i32* @IN_MODIFY
%13 = tail call i32 @inotify_add_watch(i32 %7, i8* %0, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %22, label %15
15: ; preds = %11
%16 = load i32, i32* @NAME_MAX
%17 = add i32 %16, 5
%18 = call i32 @xread(i32 %7, i8* %6, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = bitcast i8* %6 to i32*
br label %26
22: ; preds = %11
%23 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %37
24: ; preds = %32, %15
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
26: ; preds = %20, %32
%27 = load i32, i32* %21
%28 = load i32, i32* @IN_MODIFY
%29 = and i32 %28, %27
%30 = icmp eq i32 %29, %28
br i1 %30, label %31, label %32
31: ; preds = %26
br label %32
32: ; preds = %31, %26
%33 = load i32, i32* @NAME_MAX
%34 = add i32 %33, 5
%35 = call i32 @xread(i32 %7, i8* %6, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %24, label %26
37: ; preds = %24, %22, %9
ret i32 -1
}