#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;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int IN_CREATE;
int IN_DELETE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int netns_monitor(int argc, char **argv) {
  char buf[4096];
  struct inotify_event *event;
  int fd;
  fd = inotify_init();
  if (fd < 0) {
    fprintf(stderr, "inotify_init failed: %s\n", strerror(errno));
    return -1;
  }
  if (inotify_add_watch(fd, "/var/run/netns", IN_CREATE | IN_DELETE) < 0) {
    fprintf(stderr, "inotify_add_watch failed: %s\n", strerror(errno));
    return -1;
  }
  for (;;) {
    ssize_t len = read(fd, buf, sizeof(buf));
    if (len < 0) {
      fprintf(stderr, "read failed: %s\n", strerror(errno));
      return -1;
    }
    for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
         event = (struct inotify_event *)((char *)event + sizeof(*event) +
                                          event->len)) {
      if (event->mask & IN_CREATE)
        printf("add %s\n", event->name);
      if (event->mask & IN_DELETE)
        printf("delete %s\n", event->name);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<netns_monitor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <inotify_init@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    11ca <netns_monitor+0x6a>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x102c(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x102c(%rbp),%edi
mov    %rax,%rdx
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1365 <netns_monitor+0x205>
mov    -0x101c(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
lea    0xe30(%rip),%rsi
call   1070 <inotify_add_watch@plt>
cmp    $0x0,%rax
jge    1236 <netns_monitor+0xd6>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1030(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1030(%rbp),%edi
mov    %rax,%rdx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1365 <netns_monitor+0x205>
jmp    123b <netns_monitor+0xdb>
mov    -0x101c(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jae    12a5 <netns_monitor+0x145>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1034(%rbp),%edi
mov    %rax,%rdx
lea    0xdb2(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1365 <netns_monitor+0x205>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
add    -0x1028(%rbp),%rcx
cmp    %rcx,%rax
jae    1360 <netns_monitor+0x200>
mov    -0x1018(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2ce6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1304 <netns_monitor+0x1a4>
mov    -0x1018(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd58(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1018(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2cd3(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1337 <netns_monitor+0x1d7>
mov    -0x1018(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd2d(%rip),%rdi
call   1030 <printf@plt>
jmp    133c <netns_monitor+0x1dc>
mov    -0x1018(%rbp),%rax
add    $0x10,%rax
mov    -0x1018(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x1018(%rbp)
jmp    12b3 <netns_monitor+0x153>
jmp    123b <netns_monitor+0xdb>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netns_monitor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
xor    %eax,%eax
call   1060 <inotify_init@plt>
test   %eax,%eax
js     1239 <netns_monitor+0xd9>
mov    %eax,%r14d
mov    0x2e3e(%rip),%rbp
mov    0x2e57(%rip),%rbx
mov    (%rbx),%edx
or     0x0(%rbp),%edx
lea    0xe7c(%rip),%rsi
mov    %eax,%edi
call   1070 <inotify_add_watch@plt>
test   %rax,%rax
js     1242 <netns_monitor+0xe2>
mov    %r14d,0xc(%rsp)
lea    0xe8d(%rip),%r13
nopl   0x0(%rax)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11c0 <netns_monitor+0x60>
lea    (%rsp,%rax,1),%r14
add    $0x10,%r14
lea    0x10(%rsp),%r15
lea    0x14(%rsp),%r12
jmp    120a <netns_monitor+0xaa>
nopl   0x0(%rax)
movslq (%r15),%rax
lea    (%r15,%rax,1),%r12
add    $0x14,%r12
add    %r15,%rax
add    $0x10,%rax
mov    %rax,%r15
cmp    %rax,%r14
jbe    11c0 <netns_monitor+0x60>
mov    (%r12),%eax
test   %eax,0x0(%rbp)
je     1223 <netns_monitor+0xc3>
mov    0x8(%r15),%rsi
mov    %r13,%rdi
call   1030 <printf@plt>
mov    (%r12),%eax
test   %eax,(%rbx)
je     11f0 <netns_monitor+0x90>
mov    0x8(%r15),%rsi
lea    0xe1c(%rip),%rdi
call   1030 <printf@plt>
jmp    11f0 <netns_monitor+0x90>
lea    0xdc0(%rip),%rbx
jmp    1249 <netns_monitor+0xe9>
lea    0xddf(%rip),%rbx
mov    0x2d88(%rip),%rax
mov    (%rax),%ebp
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.inotify_event = type { i32, i32, i8* }
@stderr = global i32 0
@.str = [25 x i8] c"inotify_init failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"/var/run/netns\00"
@IN_CREATE = global i32 0
@IN_DELETE = global i32 0
@.str.2 = [30 x i8] c"inotify_add_watch failed: %s\0A\00"
@.str.4 = [8 x i8] c"add %s\0A\00"
@.str.5 = [11 x i8] c"delete %s\0A\00"
define i32 @netns_monitor(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @inotify_init()
%6 = icmp slt i32 %5, 0
br i1 %6, label %53, label %7
7: ; preds = %2
%8 = load i32, i32* @IN_CREATE
%9 = load i32, i32* @IN_DELETE
%10 = or i32 %9, %8
%11 = tail call i64 @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 i64 %11, 0
br i1 %12, label %53, label %13
13: ; preds = %7
%14 = bitcast [4096 x i8]* %3 to %struct.inotify_event*
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4
br label %16
16: ; preds = %20, %13
%17 = call i64 @read(i32 %5, i8* %4, i32 4096)
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %17
%19 = icmp sgt i64 %17, 0
br i1 %19, label %21, label %20
20: ; preds = %43, %16
br label %16
21: ; preds = %16, %43
%22 = phi %struct.inotify_event* [ %50, %43 ], [ %14, %16 ]
%23 = phi i8* [ %51, %43 ], [ %15, %16 ]
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = load i32, i32* @IN_CREATE
%27 = and i32 %26, %25
%28 = icmp eq i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %21
%30 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %22, i64 0, i32 2
%31 = load i8*, i8** %30
%32 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %24
br label %34
34: ; preds = %29, %21
%35 = phi i32 [ %33, %29 ], [ %25, %21 ]
%36 = load i32, i32* @IN_DELETE
%37 = and i32 %36, %35
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %34
%40 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %22, i64 0, i32 2
%41 = load i8*, i8** %40
%42 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %34, %39
%44 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %22, i64 1
%45 = bitcast %struct.inotify_event* %44 to i8*
%46 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %22, i64 0, i32 0
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = bitcast i8* %49 to %struct.inotify_event*
%51 = getelementptr inbounds i8, i8* %49, i64 4
%52 = icmp ugt i8* %18, %49
br i1 %52, label %21, label %20
53: ; preds = %7, %2
%54 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %7 ]
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = tail call i8* @strerror(i32 %56)
%58 = tail call i32 @fprintf(i32 %55, i8* %54, i8* %57)
ret i32 -1
}