#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;
  scalar_t__ len;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ IN_ALL_EVENTS;
int /*<<< orphan*/ close(int);
int inotify_add_watch(int, char *, int /*<<< orphan*/);
int inotify_init();
int /*<<< orphan*/ inotify_rm_watch(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, wd;
  pid_t pid;
  int len, i;
  char buf[1024];
  fd = inotify_init();
  if (fd <= 0) {
    perror("inotify init");
  }
  wd = inotify_add_watch(fd, "/dev/userspace", IN_ALL_EVENTS);
  if (wd < 0) {
    perror("inotify add watch");
  }
  while (1) {
    len = read(fd, buf, 1024);
    i = 0;
    while (i < len) {
      struct inotify_event *event = (struct inotify_event *)&buf[i];
      printf("inotify: %d\n", event->mask);
      i += (sizeof(struct inotify_event)) + event->len;
    }
  }
  inotify_rm_watch(fd, wd);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <inotify_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1189 <main+0x39>
lea    0xe7c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
lea    0xe71(%rip),%rsi
call   1060 <inotify_add_watch@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ba <main+0x6a>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
jmp    11bf <main+0x6f>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1231 <main+0xe1>
movslq -0x24(%rbp),%rcx
lea    -0x430(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
mov    -0x438(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x10,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11dd <main+0x8d>
jmp    11bf <main+0x6f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   1050 <inotify_init@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    11dd <main+0x8d>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
lea    0xe90(%rip),%rsi
mov    %r15d,%edi
call   1060 <inotify_add_watch@plt>
test   %eax,%eax
js     11eb <main+0x9b>
mov    %rsp,%r14
lea    0xe9b(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11a0 <main+0x50>
mov    %eax,%ebp
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movslq %eax,%rbx
mov    (%rsp,%rbx,1),%esi
mov    %r12,%rdi
call   1030 <printf@plt>
mov    0x8(%rsp,%rbx,1),%eax
add    %ebx,%eax
add    $0x10,%eax
cmp    %ebp,%eax
jl     11c0 <main+0x70>
jmp    11a0 <main+0x50>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
jmp    116d <main+0x1d>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
jmp    1189 <main+0x39>

LLVM IR (-O0):
%struct.inotify_event = type { i32, i64 }
@.str = [13 x i8] c"inotify init\00"
@.str.1 = [15 x i8] c"/dev/userspace\00"
@IN_ALL_EVENTS = global i32 0
@.str.2 = [18 x i8] c"inotify add watch\00"
@.str.3 = [13 x i8] c"inotify: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca %struct.inotify_event*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @inotify_init()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = load i32, i32* @IN_ALL_EVENTS
%21 = call i32 @inotify_add_watch(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
br label %27
27: ; preds = %26, %52
%28 = load i32, i32* %6
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 1024)
store i32 %30, i32* %9
store i32 0, i32* %10
br label %31
31: ; preds = %35, %27
%32 = load i32, i32* %10
%33 = load i32, i32* %9
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %52
35: ; preds = %31
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %37
%39 = bitcast i8* %38 to %struct.inotify_event*
store %struct.inotify_event* %39, %struct.inotify_event** %12
%40 = load %struct.inotify_event*, %struct.inotify_event** %12
%41 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load %struct.inotify_event*, %struct.inotify_event** %12
%45 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %44, i32 0, i32 1
%46 = load i64, i64* %45
%47 = add i64 16, %46
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = add i64 %49, %47
%51 = trunc i64 %50 to i32
store i32 %51, i32* %10
br label %31
52: ; preds = %31
br label %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"inotify init\00"
@.str.1 = [15 x i8] c"/dev/userspace\00"
@IN_ALL_EVENTS = global i32 0
@.str.2 = [18 x i8] c"inotify add watch\00"
@.str.3 = [13 x i8] c"inotify: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @inotify_init()
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @IN_ALL_EVENTS
%11 = tail call i32 @inotify_add_watch(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
br label %16
16: ; preds = %19, %15
%17 = call i32 @read(i32 %5, i8* %4, i32 1024)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %20, %16
br label %16
20: ; preds = %16, %20
%21 = phi i32 [ %32, %20 ], [ 0, %16 ]
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %22
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds i8, i8* %23, i64 8
%28 = bitcast i8* %27 to i64*
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = add i32 %21, 16
%32 = add i32 %31, %30
%33 = icmp slt i32 %32, %17
br i1 %33, label %20, label %19
}