#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 *);
scalar_t__ errno;
int /*<<< orphan*/ open_device(char *);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int read_notify(const char *dirname, int nfd) {
  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;
    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);
      } else {
        close_device(devname);
      }
    }
    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    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x250(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x238(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x18,-0x18(%rbp)
jge    1202 <read_notify+0xb2>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <read_notify+0x9c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x244(%rbp)
jmp    1311 <read_notify+0x1c1>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x244(%rbp)
jmp    1311 <read_notify+0x1c1>
mov    -0x250(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x250(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x250(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
cmpl   $0x18,-0x18(%rbp)
jl     1300 <read_notify+0x1b0>
lea    -0x230(%rbp),%rax
movslq -0x238(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rax
cmpl   $0x0,(%rax)
je     12c7 <read_notify+0x177>
mov    -0x30(%rbp),%rdi
mov    -0x240(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x240(%rbp),%rax
mov    0x10(%rax),%eax
mov    0x2d36(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b6 <read_notify+0x166>
mov    -0x250(%rbp),%rdi
call   1060 <open_device@plt>
jmp    12c2 <read_notify+0x172>
mov    -0x250(%rbp),%rdi
call   1070 <close_device@plt>
jmp    12c7 <read_notify+0x177>
mov    -0x240(%rbp),%rax
movslq (%rax),%rax
add    $0x18,%rax
mov    %eax,-0x234(%rbp)
mov    -0x234(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x234(%rbp),%eax
add    -0x238(%rbp),%eax
mov    %eax,-0x238(%rbp)
jmp    1243 <read_notify+0xf3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x244(%rbp)
mov    -0x20(%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    $0x208,%rsp
mov    %esi,%eax
mov    %rdi,%r15
mov    0x2e80(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r14
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
lea    -0x230(%rbp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0x17,%eax
jg     11b9 <read_notify+0x69>
mov    0x2e36(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1c(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
jmp    124b <read_notify+0xfb>
mov    %eax,%r12d
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
lea    (%r14,%rax,1),%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%r14,%rax,1)
xor    %ebx,%ebx
jmp    120b <read_notify+0xbb>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <close_device@plt>
lea    0x18(%r13),%eax
sub    %eax,%r12d
add    %r13d,%ebx
add    $0x18,%ebx
cmp    $0x17,%r12d
jle    1249 <read_notify+0xf9>
movslq %ebx,%r15
mov    -0x230(%rbp,%r15,1),%r13d
ff
test   %r13d,%r13d
je     11f8 <read_notify+0xa8>
mov    -0x228(%rbp,%r15,1),%rsi
ff
mov    -0x30(%rbp),%rdi
call   1030 <strcpy@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x220(%rbp,%r15,1)
ff
je     11f0 <read_notify+0xa0>
mov    %r14,%rdi
call   1060 <open_device@plt>
jmp    11f8 <read_notify+0xa8>
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
@IN_CREATE = global i32 0
define i32 @read_notify(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca [512 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.inotify_event*
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
store i32 0, i32* %12
%19 = load i32, i32* %5
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 512)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 24
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 0, i32* %3
store i32 1, i32* %14
br label %82
29: ; preds = %24
store i32 1, i32* %3
store i32 1, i32* %14
br label %82
30: ; preds = %2
%31 = load i8*, i8** %4
%32 = call i32 @strcpy(i8* %18, i8* %31)
%33 = call i32 @strlen(i8* %18)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %18, i64 %34
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %9
store i8 47, i8* %36
br label %38
38: ; preds = %68, %30
%39 = load i32, i32* %6
%40 = icmp sge i32 %39, 24
br i1 %40, label %41, label %81
41: ; preds = %38
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%43 = load i32, i32* %12
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = bitcast i8* %45 to %struct.inotify_event*
store %struct.inotify_event* %46, %struct.inotify_event** %13
%47 = load %struct.inotify_event*, %struct.inotify_event** %13
%48 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %68
51: ; preds = %41
%52 = load i8*, i8** %9
%53 = load %struct.inotify_event*, %struct.inotify_event** %13
%54 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %53, i32 0, i32 1
%55 = load i8*, i8** %54
%56 = call i32 @strcpy(i8* %52, i8* %55)
%57 = load %struct.inotify_event*, %struct.inotify_event** %13
%58 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %57, i32 0, i32 2
%59 = load i32, i32* %58
%60 = load i32, i32* @IN_CREATE
%61 = and i32 %59, %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %51
%64 = call i32 @open_device(i8* %18)
br label %67
65: ; preds = %51
%66 = call i32 @close_device(i8* %18)
br label %67
67: ; preds = %65, %63
br label %68
68: ; preds = %67, %41
%69 = load %struct.inotify_event*, %struct.inotify_event** %13
%70 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = sext i32 %71 to i64
%73 = add i64 24, %72
%74 = trunc i64 %73 to i32
store i32 %74, i32* %11
%75 = load i32, i32* %11
%76 = load i32, i32* %6
%77 = sub nsw i32 %76, %75
store i32 %77, i32* %6
%78 = load i32, i32* %11
%79 = load i32, i32* %12
%80 = add nsw i32 %79, %78
store i32 %80, i32* %12
br label %38
81: ; preds = %38
store i32 0, i32* %3
store i32 1, i32* %14
br label %82
82: ; preds = %81, %29, %28
%83 = load i8*, i8** %7
%84 = load i32, i32* %3
ret i32 %84
}

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