#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;
  char const *name;
  int mask;
};
typedef int /*<<< orphan*/ event_buf;

/* Variables and functions */
scalar_t__ EINTR;
int IN_CREATE;
int PATH_MAX;
int /*<<< orphan*/ close_device(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ open_device(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
char *strerror(scalar_t__);
int strlen(char *);
int read_notify(const char *dirname, int nfd, int print_flags) {
  int res;
  char devname[PATH_MAX];
  char *filename;
  char event_buf[512];
  int event_size;
  int event_pos = 0;
  struct inotify_event *event;
  res = read(nfd, event_buf, sizeof(event_buf));
  if (res < (int)sizeof(*event)) {
    if (errno == EINTR)
      return 0;
    fprintf(stderr, "could not get event, %s\n", strerror(errno));
    return 1;
  }
  strcpy(devname, dirname);
  filename = devname + strlen(devname);
  *filename++ = '/';
  while (res >= (int)sizeof(*event)) {
    event = (struct inotify_event *)(event_buf + event_pos);
    if (event->len) {
      strcpy(filename, event->name);
      if (event->mask & IN_CREATE) {
        open_device(devname, print_flags);
      } else {
        close_device(devname, print_flags);
      }
    }
    event_size = sizeof(*event) + event->len;
    res -= event_size;
    event_pos += event_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_notify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x260(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x248(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x18,-0x1c(%rbp)
jge    1258 <read_notify+0xe8>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <read_notify+0x9f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    136d <read_notify+0x1fd>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x264(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x264(%rbp),%edi
mov    %rax,%rdx
lea    0xdc3(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    136d <read_notify+0x1fd>
mov    -0x260(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x260(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x260(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2f,(%rax)
cmpl   $0x18,-0x1c(%rbp)
jl     135c <read_notify+0x1ec>
lea    -0x240(%rbp),%rax
movslq -0x248(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x250(%rbp)
mov    -0x250(%rbp),%rax
cmpl   $0x0,(%rax)
je     1323 <read_notify+0x1b3>
mov    -0x38(%rbp),%rdi
mov    -0x250(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x250(%rbp),%rax
mov    0x10(%rax),%eax
mov    0x2cd8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     130f <read_notify+0x19f>
mov    -0x260(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <open_device@plt>
jmp    131e <read_notify+0x1ae>
mov    -0x260(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <close_device@plt>
jmp    1323 <read_notify+0x1b3>
mov    -0x250(%rbp),%rax
movslq (%rax),%rax
add    $0x18,%rax
mov    %eax,-0x244(%rbp)
mov    -0x244(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x244(%rbp),%eax
add    -0x248(%rbp),%eax
mov    %eax,-0x248(%rbp)
jmp    1299 <read_notify+0x129>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_notify>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edx,%ebx
mov    %esi,%eax
mov    %rdi,%r12
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r15
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
lea    -0x240(%rbp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0x17,%eax
jg     1202 <read_notify+0x92>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rdi
mov    0x2df2(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdi
je     12a1 <read_notify+0x131>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xe12(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    12a1 <read_notify+0x131>
mov    %eax,%r13d
mov    %ebx,-0x2c(%rbp)
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2f,(%r15,%rax,1)
xor    %ebx,%ebx
jmp    125e <read_notify+0xee>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    -0x2c(%rbp),%esi
call   1080 <close_device@plt>
lea    0x18(%r14),%eax
sub    %eax,%r13d
add    %r14d,%ebx
add    $0x18,%ebx
cmp    $0x17,%r13d
jle    129f <read_notify+0x12f>
movslq %ebx,%r12
mov    -0x240(%rbp,%r12,1),%r14d
ff
test   %r14d,%r14d
je     124b <read_notify+0xdb>
mov    -0x238(%rbp,%r12,1),%rsi
ff
mov    -0x38(%rbp),%rdi
call   1030 <strcpy@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x230(%rbp,%r12,1)
ff
je     1240 <read_notify+0xd0>
mov    %r15,%rdi
mov    -0x2c(%rbp),%esi
call   1070 <open_device@plt>
jmp    124b <read_notify+0xdb>
xor    %eax,%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, i8*, i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@IN_CREATE = global i32 0
define i32 @read_notify(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca [512 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.inotify_event*
%16 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %9
%20 = alloca i8, i64 %18
store i64 %18, i64* %10
store i32 0, i32* %14
%21 = load i32, i32* %6
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 512)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 24
br i1 %25, label %26, label %36
26: ; preds = %3
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %4
store i32 1, i32* %16
br label %90
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %34)
store i32 1, i32* %4
store i32 1, i32* %16
br label %90
36: ; preds = %3
%37 = load i8*, i8** %5
%38 = call i32 @strcpy(i8* %20, i8* %37)
%39 = call i32 @strlen(i8* %20)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %20, i64 %40
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %11
store i8 47, i8* %42
br label %44
44: ; preds = %76, %36
%45 = load i32, i32* %8
%46 = icmp sge i32 %45, 24
br i1 %46, label %47, label %89
47: ; preds = %44
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%49 = load i32, i32* %14
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = bitcast i8* %51 to %struct.inotify_event*
store %struct.inotify_event* %52, %struct.inotify_event** %15
%53 = load %struct.inotify_event*, %struct.inotify_event** %15
%54 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %76
57: ; preds = %47
%58 = load i8*, i8** %11
%59 = load %struct.inotify_event*, %struct.inotify_event** %15
%60 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %59, i32 0, i32 1
%61 = load i8*, i8** %60
%62 = call i32 @strcpy(i8* %58, i8* %61)
%63 = load %struct.inotify_event*, %struct.inotify_event** %15
%64 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %63, i32 0, i32 2
%65 = load i32, i32* %64
%66 = load i32, i32* @IN_CREATE
%67 = and i32 %65, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %57
%70 = load i32, i32* %7
%71 = call i32 @open_device(i8* %20, i32 %70)
br label %75
72: ; preds = %57
%73 = load i32, i32* %7
%74 = call i32 @close_device(i8* %20, i32 %73)
br label %75
75: ; preds = %72, %69
br label %76
76: ; preds = %75, %47
%77 = load %struct.inotify_event*, %struct.inotify_event** %15
%78 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = sext i32 %79 to i64
%81 = add i64 24, %80
%82 = trunc i64 %81 to i32
store i32 %82, i32* %13
%83 = load i32, i32* %13
%84 = load i32, i32* %8
%85 = sub nsw i32 %84, %83
store i32 %85, i32* %8
%86 = load i32, i32* %13
%87 = load i32, i32* %14
%88 = add nsw i32 %87, %86
store i32 %88, i32* %14
br label %44
89: ; preds = %44
store i32 0, i32* %4
store i32 1, i32* %16
br label %90
90: ; preds = %89, %31, %30
%91 = load i8*, i8** %9
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@IN_CREATE = global i32 0
define i32 @read_notify(i8* %0, i32 %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %1, i8* %8, i32 512)
%10 = icmp slt i32 %9, 24
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %53, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i8* @strerror(i64 %12)
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %53
19: ; preds = %3
%20 = call i32 @strcpy(i8* %7, i8* %0)
%21 = call i32 @strlen(i8* %7)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %7, i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 47, i8* %23
br label %25
25: ; preds = %19, %48
%26 = phi i32 [ 0, %19 ], [ %51, %48 ]
%27 = phi i32 [ %9, %19 ], [ %50, %48 ]
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %28
%30 = bitcast i8* %29 to i32*
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, 0
br i1 %32, label %48, label %33
33: ; preds = %25
%34 = getelementptr inbounds i8, i8* %29, i64 8
%35 = bitcast i8* %34 to i8**
%36 = load i8*, i8** %35
%37 = call i32 @strcpy(i8* %24, i8* %36)
%38 = getelementptr inbounds i8, i8* %29, i64 16
%39 = bitcast i8* %38 to i32*
%40 = load i32, i32* %39
%41 = load i32, i32* @IN_CREATE
%42 = and i32 %41, %40
%43 = icmp eq i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %33
%45 = call i32 @open_device(i8* %7, i32 %2)
br label %48
46: ; preds = %33
%47 = call i32 @close_device(i8* %7, i32 %2)
br label %48
48: ; preds = %44, %46, %25
%49 = add i32 %31, 24
%50 = sub nsw i32 %27, %49
%51 = add nsw i32 %49, %26
%52 = icmp sgt i32 %50, 23
br i1 %52, label %25, label %53
53: ; preds = %48, %11, %15
%54 = phi i32 [ 1, %15 ], [ 0, %11 ], [ 0, %48 ]
ret i32 %54
}