#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 int /*<<< orphan*/ event_buf;

/* Variables and functions */
scalar_t__ EINTR;
int IN_CREATE;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
char *strerror(scalar_t__);
int read_process_inotify_fd(int fd) {
  int res;
  char event_buf[512];
  int event_size;
  int event_pos = 0;
  struct inotify_event *event;
  res = read(fd, event_buf, sizeof(event_buf));
  if (res < (int)sizeof(*event)) {
    if (errno == EINTR)
      return 0;
    printf("could not get event, %s\n", strerror(errno));
    return -1;
  }
  while (res >= (int)sizeof(*event)) {
    event = (struct inotify_event *)(event_buf + event_pos);
    if (event->len) {
      if (event->mask & IN_CREATE) {
        printf("create file: %s\n", event->name);
      } else {
        printf("delete file: %s\n", event->name);
      }
    }
    event_size = sizeof(*event) + event->len;
    res -= event_size;
    event_pos += event_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_process_inotify_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x218(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jge    11b9 <read_process_inotify_fd+0x89>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <read_process_inotify_fd+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    1284 <read_process_inotify_fd+0x154>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <read_process_inotify_fd+0x154>
jmp    11be <read_process_inotify_fd+0x8e>
cmpl   $0x10,-0xc(%rbp)
jl     127d <read_process_inotify_fd+0x14d>
lea    -0x210(%rbp),%rax
movslq -0x218(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
cmpl   $0x0,(%rax)
je     1244 <read_process_inotify_fd+0x114>
mov    -0x220(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2dd7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1228 <read_process_inotify_fd+0xf8>
mov    -0x220(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
jmp    123f <read_process_inotify_fd+0x10f>
mov    -0x220(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf0(%rip),%rdi
call   1030 <printf@plt>
jmp    1244 <read_process_inotify_fd+0x114>
mov    -0x220(%rbp),%rax
movslq (%rax),%rax
add    $0x10,%rax
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x214(%rbp),%eax
add    -0x218(%rbp),%eax
mov    %eax,-0x218(%rbp)
jmp    11be <read_process_inotify_fd+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_process_inotify_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
cmp    $0x10,%eax
jge    1186 <read_process_inotify_fd+0x56>
mov    0x2e86(%rip),%rax
mov    (%rax),%rdi
mov    0x2e6c(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdi
je     11e9 <read_process_inotify_fd+0xb9>
call   1050 <strerror@plt>
lea    0xe89(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
jmp    11e9 <read_process_inotify_fd+0xb9>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    0x2e47(%rip),%r14
lea    0xe92(%rip),%r15
lea    0xe7a(%rip),%r12
jmp    11c1 <read_process_inotify_fd+0x91>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x10(%r13),%eax
sub    %eax,%ebp
add    %r13d,%ebx
add    $0x10,%ebx
cmp    $0xf,%ebp
jle    11e7 <read_process_inotify_fd+0xb7>
movslq %ebx,%rax
mov    (%rsp,%rax,1),%r13d
test   %r13d,%r13d
je     11b0 <read_process_inotify_fd+0x80>
mov    (%r14),%ecx
and    0x4(%rsp,%rax,1),%ecx
mov    0x8(%rsp,%rax,1),%rsi
mov    %r12,%rdi
cmove  %r15,%rdi
call   1030 <printf@plt>
jmp    11b0 <read_process_inotify_fd+0x80>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32, i32, i8* }
@errno = global i64 0
@EINTR = global i64 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@IN_CREATE = global i32 0
@.str.1 = [17 x i8] c"create file: %s\0A\00"
@.str.2 = [17 x i8] c"delete file: %s\0A\00"
define i32 @read_process_inotify_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.inotify_event*
store i32 %0, i32* %3
store i32 0, i32* %7
%9 = load i32, i32* %3
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 512)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 16
br i1 %13, label %14, label %23
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %2
br label %69
19: ; preds = %14
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %2
br label %69
23: ; preds = %1
br label %24
24: ; preds = %55, %23
%25 = load i32, i32* %4
%26 = icmp sge i32 %25, 16
br i1 %26, label %27, label %68
27: ; preds = %24
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = bitcast i8* %31 to %struct.inotify_event*
store %struct.inotify_event* %32, %struct.inotify_event** %8
%33 = load %struct.inotify_event*, %struct.inotify_event** %8
%34 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %55
37: ; preds = %27
%38 = load %struct.inotify_event*, %struct.inotify_event** %8
%39 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load i32, i32* @IN_CREATE
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %37
%45 = load %struct.inotify_event*, %struct.inotify_event** %8
%46 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %45, i32 0, i32 2
%47 = load i8*, i8** %46
%48 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %54
49: ; preds = %37
%50 = load %struct.inotify_event*, %struct.inotify_event** %8
%51 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %50, i32 0, i32 2
%52 = load i8*, i8** %51
%53 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %49, %44
br label %55
55: ; preds = %54, %27
%56 = load %struct.inotify_event*, %struct.inotify_event** %8
%57 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = sext i32 %58 to i64
%60 = add i64 16, %59
%61 = trunc i64 %60 to i32
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = load i32, i32* %4
%64 = sub nsw i32 %63, %62
store i32 %64, i32* %4
%65 = load i32, i32* %6
%66 = load i32, i32* %7
%67 = add nsw i32 %66, %65
store i32 %67, i32* %7
br label %24
68: ; preds = %24
store i32 0, i32* %2
br label %69
69: ; preds = %68, %19, %18
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@IN_CREATE = global i32 0
@.str.1 = [17 x i8] c"create file: %s\0A\00"
@.str.2 = [17 x i8] c"delete file: %s\0A\00"
define i32 @read_process_inotify_fd(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 512)
%5 = icmp slt i32 %4, 16
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %38, label %10
10: ; preds = %6
%11 = tail call i8* @strerror(i64 %7)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %38
13: ; preds = %1, %33
%14 = phi i32 [ %36, %33 ], [ 0, %1 ]
%15 = phi i32 [ %35, %33 ], [ %4, %1 ]
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %16
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %17, i64 4
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %23
%25 = load i32, i32* @IN_CREATE
%26 = and i32 %25, %24
%27 = icmp eq i32 %26, 0
%28 = getelementptr inbounds i8, i8* %17, i64 8
%29 = bitcast i8* %28 to i8**
%30 = load i8*, i8** %29
%31 = select i1 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0)
%32 = tail call i32 @printf(i8* %31, i8* %30)
br label %33
33: ; preds = %21, %13
%34 = add i32 %19, 16
%35 = sub nsw i32 %15, %34
%36 = add nsw i32 %34, %14
%37 = icmp sgt i32 %35, 15
br i1 %37, label %13, label %38
38: ; preds = %33, %6, %10
%39 = phi i32 [ -1, %10 ], [ 0, %6 ], [ 0, %33 ]
ret i32 %39
}