#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 wd;
  int mask;
  int cookie;
  int len;
  char *name;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int IN_ACCESS;
int IN_ISDIR;
int IN_MODIFY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int main() {
  int fd, wd;
  ssize_t len, i;
  char buf[4096] __attribute__((aligned(4)));
  fd = inotify_init();
  if (fd == -1) {
    perror("inotify_init");
    exit(EXIT_FAILURE);
  }
  wd = inotify_add_watch(fd, "/etc", IN_ACCESS | IN_MODIFY);
  if (wd == -1) {
    perror("inotify_add_watch");
    exit(EXIT_FAILURE);
  }
  len = read(fd, buf, 4096);
  for (i = 0; i < len;) {
    struct inotify_event *ev = (struct inotify_event *)&(buf[i]);
    printf("wd=%d mask=%d cookie=%d len=%d dir=%s\n", ev->wd, ev->mask,
           ev->cookie, ev->len, (ev->mask & IN_ISDIR) ? "yes" : "no");
    if (ev->len)
      printf("name=%s\n", ev->name);
    i += sizeof(struct inotify_event) + ev->len;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <inotify_init@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b0 <main+0x40>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2e2d(%rip),%rax
or     (%rax),%edx
lea    0xe41(%rip),%rsi
call   1070 <inotify_add_watch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11f8 <main+0x88>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12dc <main+0x16c>
lea    -0x1020(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rax
mov    (%rax),%esi
mov    -0x1028(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x1028(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x1028(%rbp),%rax
mov    0xc(%rax),%r8d
mov    -0x1028(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d6f(%rip),%rax
and    (%rax),%edi
lea    0xdd5(%rip),%r9
lea    0xdca(%rip),%rax
cmp    $0x0,%edi
cmovne %rax,%r9
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1028(%rbp),%rax
cmpl   $0x0,0xc(%rax)
je     12c0 <main+0x150>
mov    -0x1028(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1028(%rbp),%rax
movslq 0xc(%rax),%rax
add    $0x18,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1218 <main+0xa8>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
xor    %eax,%eax
call   1060 <inotify_init@plt>
cmp    $0xffffffff,%eax
je     1276 <main+0x106>
mov    %eax,%ebx
mov    0x2e36(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe61(%rip),%rsi
mov    %ebx,%edi
call   1070 <inotify_add_watch@plt>
cmp    $0xffffffff,%eax
je     127f <main+0x10f>
lea    0x8(%rsp),%rsi
mov    %ebx,0x4(%rsp)
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1259 <main+0xe9>
mov    %rax,%rbx
mov    0x2dfc(%rip),%r13
lea    0xe64(%rip),%r14
lea    0xe32(%rip),%r12
xor    %r15d,%r15d
jmp    120c <main+0x9c>
nopw   0x0(%rax,%rax,1)
add    %rbp,%r15
add    $0x18,%r15
cmp    %rbx,%r15
jae    1259 <main+0xe9>
mov    0xc(%rsp,%r15,1),%edx
mov    0x0(%r13),%eax
mov    %r14,%r9
and    %edx,%eax
je     1223 <main+0xb3>
lea    0xe28(%rip),%r9
mov    0x8(%rsp,%r15,1),%esi
mov    0x10(%rsp,%r15,1),%ecx
movslq 0x14(%rsp,%r15,1),%rbp
mov    %r12,%rdi
mov    %ebp,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
test   %rbp,%rbp
je     1200 <main+0x90>
mov    0x18(%rsp,%r15,1),%rsi
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1200 <main+0x90>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd83(%rip),%rdi
jmp    1286 <main+0x116>
lea    0xd8c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.inotify_event = type { i32, i32, i32, i32, i8* }
@.str = [13 x i8] c"inotify_init\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"/etc\00"
@IN_ACCESS = global i32 0
@IN_MODIFY = global i32 0
@.str.2 = [18 x i8] c"inotify_add_watch\00"
@.str.3 = [39 x i8] c"wd=%d mask=%d cookie=%d len=%d dir=%s\0A\00"
@IN_ISDIR = global i32 0
@.str.4 = [4 x i8] c"yes\00"
@.str.5 = [3 x i8] c"no\00"
@.str.6 = [9 x i8] c"name=%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca [4096 x i8]
%7 = alloca %struct.inotify_event*
store i32 0, i32* %1
%8 = call i32 (...) @inotify_init()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
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
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = load i32, i32* @IN_ACCESS
%18 = load i32, i32* @IN_MODIFY
%19 = or i32 %17, %18
%20 = call i32 @inotify_add_watch(i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %15
%28 = load i32, i32* %2
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%30 = call i64 @read(i32 %28, i8* %29, i32 4096)
store i64 %30, i64* %4
store i64 0, i64* %5
br label %31
31: ; preds = %69, %27
%32 = load i64, i64* %5
%33 = load i64, i64* %4
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %77
35: ; preds = %31
%36 = load i64, i64* %5
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %36
%38 = bitcast i8* %37 to %struct.inotify_event*
store %struct.inotify_event* %38, %struct.inotify_event** %7
%39 = load %struct.inotify_event*, %struct.inotify_event** %7
%40 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load %struct.inotify_event*, %struct.inotify_event** %7
%43 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.inotify_event*, %struct.inotify_event** %7
%46 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %45, i32 0, i32 2
%47 = load i32, i32* %46
%48 = load %struct.inotify_event*, %struct.inotify_event** %7
%49 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %48, i32 0, i32 3
%50 = load i32, i32* %49
%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_ISDIR
%55 = and i32 %53, %54
%56 = icmp ne i32 %55, 0
%57 = zext i1 %56 to i64
%58 = select i1 %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %44, i32 %47, i32 %50, i8* %58)
%60 = load %struct.inotify_event*, %struct.inotify_event** %7
%61 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %60, i32 0, i32 3
%62 = load i32, i32* %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %35
%65 = load %struct.inotify_event*, %struct.inotify_event** %7
%66 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %65, i32 0, i32 4
%67 = load i8*, i8** %66
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %64, %35
%70 = load %struct.inotify_event*, %struct.inotify_event** %7
%71 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %70, i32 0, i32 3
%72 = load i32, i32* %71
%73 = sext i32 %72 to i64
%74 = add i64 24, %73
%75 = load i64, i64* %5
%76 = add i64 %75, %74
store i64 %76, i64* %5
br label %31
77: ; preds = %31
%78 = load i32, i32* %2
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
@.str = [13 x i8] c"inotify_init\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"/etc\00"
@IN_ACCESS = global i32 0
@IN_MODIFY = global i32 0
@.str.2 = [18 x i8] c"inotify_add_watch\00"
@.str.3 = [39 x i8] c"wd=%d mask=%d cookie=%d len=%d dir=%s\0A\00"
@IN_ISDIR = global i32 0
@.str.4 = [4 x i8] c"yes\00"
@.str.5 = [3 x i8] c"no\00"
@.str.6 = [9 x i8] c"name=%s\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @inotify_init()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
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
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i32, i32* @IN_ACCESS
%11 = load i32, i32* @IN_MODIFY
%12 = or i32 %11, %10
%13 = tail call i32 @inotify_add_watch(i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
%20 = call i64 @read(i32 %3, i8* %2, i32 4096)
%21 = icmp eq i64 %20, 0
br i1 %21, label %52, label %22
22: ; preds = %19, %47
%23 = phi i64 [ %50, %47 ], [ 0, %19 ]
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %23
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = getelementptr inbounds i8, i8* %24, i64 4
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = getelementptr inbounds i8, i8* %24, i64 8
%31 = bitcast i8* %30 to i32*
%32 = load i32, i32* %31
%33 = getelementptr inbounds i8, i8* %24, i64 12
%34 = bitcast i8* %33 to i32*
%35 = load i32, i32* %34
%36 = load i32, i32* @IN_ISDIR
%37 = and i32 %36, %29
%38 = icmp eq i32 %37, 0
%39 = select i1 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %29, i32 %32, i32 %35, i8* %39)
%41 = icmp eq i32 %35, 0
br i1 %41, label %47, label %42
42: ; preds = %22
%43 = getelementptr inbounds i8, i8* %24, i64 16
%44 = bitcast i8* %43 to i8**
%45 = load i8*, i8** %44
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %42, %22
%48 = sext i32 %35 to i64
%49 = add i64 %23, 24
%50 = add i64 %49, %48
%51 = icmp ult i64 %50, %20
br i1 %51, label %22, label %52
52: ; preds = %47, %19
%53 = tail call i32 @close(i32 %3)
ret i32 0
}