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

/* Variables and functions */
int /*<<< orphan*/ IN_ALL_EVENTS;
struct inotify_event *calloc(int, size_t);
int /*<<< orphan*/ close(int);
int inotify_add_watch(int, char *, int /*<<< orphan*/);
int inotify_init();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_event(struct inotify_event *);
int read(int, struct inotify_event *, size_t);
int watch(char *file) {
  int inot = inotify_init();
  if (-1 == inot) {
    perror("inotify_init");
    return 1;
  }
  int wid = inotify_add_watch(inot, file, IN_ALL_EVENTS);
  if (-1 == wid) {
    perror("inotify_add_watch");
    return 2;
  }
  struct inotify_event *evt;
  size_t buflen = sizeof(struct inotify_event) + 32;
  if (NULL == (evt = calloc(1, buflen))) {
    perror("calloc");
    return 3;
  }
  while (1) {
    int ret = read(inot, evt, buflen);
    if (ret == 0) {
      perror("read");
      return 4;
    }
    print_event(evt);
  }
  close(inot);
  return 0;
}


x86 assembly (clang, -O0):
<watch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <inotify_init@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    119c <watch+0x3c>
lea    0xe75(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1262 <watch+0x102>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   1070 <inotify_add_watch@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11da <watch+0x7a>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1262 <watch+0x102>
movq   $0x24,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    121a <watch+0xba>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1262 <watch+0x102>
jmp    121f <watch+0xbf>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1254 <watch+0xf4>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    1262 <watch+0x102>
mov    -0x20(%rbp),%rdi
call   1030 <print_event@plt>
jmp    121f <watch+0xbf>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
xor    %eax,%eax
call   1060 <inotify_init@plt>
cmp    $0xffffffff,%eax
jne    1185 <watch+0x25>
mov    $0x1,%r14d
lea    0xe7d(%rip),%r15
jmp    11cf <watch+0x6f>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <inotify_add_watch@plt>
cmp    $0xffffffff,%eax
jne    11ae <watch+0x4e>
mov    $0x2,%r14d
lea    0xe61(%rip),%r15
jmp    11cf <watch+0x6f>
mov    $0x24,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
test   %rax,%rax
jne    11e5 <watch+0x85>
mov    $0x3,%r14d
lea    0xe50(%rip),%r15
mov    %r15,%rdi
call   1080 <perror@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbp
mov    $0x24,%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   1040 <read@plt>
mov    $0x4,%r14d
lea    0xe22(%rip),%r15
test   %eax,%eax
je     11cf <watch+0x6f>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <print_event@plt>
mov    $0x24,%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <read@plt>
test   %eax,%eax
jne    1210 <watch+0xb0>
jmp    11cf <watch+0x6f>

LLVM IR (-O0):
%struct.inotify_event = type { i32 }
@.str = [13 x i8] c"inotify_init\00"
@IN_ALL_EVENTS = global i32 0
@.str.1 = [18 x i8] c"inotify_add_watch\00"
@.str.2 = [7 x i8] c"calloc\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @watch(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.inotify_event*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = call i32 (...) @inotify_init()
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 -1, %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %42
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i8*, i8** %3
%17 = load i32, i32* @IN_ALL_EVENTS
%18 = call i32 @inotify_add_watch(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %2
br label %42
23: ; preds = %14
store i64 36, i64* %7
%24 = load i64, i64* %7
%25 = call %struct.inotify_event* @calloc(i32 1, i64 %24)
store %struct.inotify_event* %25, %struct.inotify_event** %6
%26 = icmp eq %struct.inotify_event* null, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %2
br label %42
29: ; preds = %23
br label %30
30: ; preds = %29, %39
%31 = load i32, i32* %4
%32 = load %struct.inotify_event*, %struct.inotify_event** %6
%33 = load i64, i64* %7
%34 = call i32 @read(i32 %31, %struct.inotify_event* %32, i64 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 4, i32* %2
br label %42
39: ; preds = %30
%40 = load %struct.inotify_event*, %struct.inotify_event** %6
%41 = call i32 @print_event(%struct.inotify_event* %40)
br label %30
42: ; preds = %37, %27, %21, %12
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.inotify_event = type { i32 }
@.str = [13 x i8] c"inotify_init\00"
@IN_ALL_EVENTS = global i32 0
@.str.1 = [18 x i8] c"inotify_add_watch\00"
@.str.2 = [7 x i8] c"calloc\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @watch(i8* %0) {
%2 = tail call i32 (...) @inotify_init()
%3 = icmp eq i32 %2, -1
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = load i32, i32* @IN_ALL_EVENTS
%6 = tail call i32 @inotify_add_watch(i32 %2, i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %18, label %8
8: ; preds = %4
%9 = tail call %struct.inotify_event* @calloc(i32 1, i64 36)
%10 = icmp eq %struct.inotify_event* %9, null
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = tail call i32 @read(i32 %2, %struct.inotify_event* %9, i64 36)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11, %14
%15 = tail call i32 @print_event(%struct.inotify_event* %9)
%16 = tail call i32 @read(i32 %2, %struct.inotify_event* %9, i64 36)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %14
18: ; preds = %14, %11, %8, %4, %1
%19 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %11 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %14 ]
%20 = phi i32 [ 1, %1 ], [ 2, %4 ], [ 3, %8 ], [ 4, %11 ], [ 4, %14 ]
%21 = tail call i32 @perror(i8* %19)
ret i32 %20
}