#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 */
int IN_CREATE;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int watch_inotify_events(int fd) {
  char event_buf[512];
  int ret;
  int event_pos = 0;
  int event_size = 0;
  struct inotify_event *event;
  ret = read(fd, event_buf, sizeof(event_buf));
  if (ret < (int)sizeof(struct inotify_event)) {
    printf("counld not get event!\n");
    return -1;
  }
  while (ret >= (int)sizeof(struct inotify_event)) {
    event = (struct inotify_event *)(event_buf + event_pos);
    if (event->len) {
      if (event->mask & IN_CREATE) {
        printf("zing --> create file: %s\n", event->name);
      } else {
        printf("zing --> delete file: %s\n", event->name);
      }
    }
    event_size = sizeof(struct inotify_event) + event->len;
    ret -= event_size;
    event_pos += event_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<watch_inotify_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x218(%rbp)
movl   $0x0,-0x21c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x10,-0x214(%rbp)
jge    1183 <watch_inotify_events+0x63>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <watch_inotify_events+0x13b>
jmp    1188 <watch_inotify_events+0x68>
cmpl   $0x10,-0x214(%rbp)
jl     1254 <watch_inotify_events+0x134>
lea    -0x210(%rbp),%rax
movslq -0x218(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rax
cmpl   $0x0,(%rax)
je     1215 <watch_inotify_events+0xf5>
mov    -0x228(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2e12(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f7 <watch_inotify_events+0xd7>
mov    -0x228(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1210 <watch_inotify_events+0xf0>
mov    -0x228(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1215 <watch_inotify_events+0xf5>
mov    -0x228(%rbp),%rax
movslq (%rax),%rax
add    $0x10,%rax
mov    %eax,-0x21c(%rbp)
mov    -0x21c(%rbp),%ecx
mov    -0x214(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x214(%rbp)
mov    -0x21c(%rbp),%eax
add    -0x218(%rbp),%eax
mov    %eax,-0x218(%rbp)
jmp    1188 <watch_inotify_events+0x68>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch_inotify_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jge    1166 <watch_inotify_events+0x36>
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11cb <watch_inotify_events+0x9b>
mov    %eax,%ebp
mov    0x2e71(%rip),%r14
lea    0xea4(%rip),%r15
lea    0xe83(%rip),%r12
xor    %ebx,%ebx
jmp    11a1 <watch_inotify_events+0x71>
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    11c9 <watch_inotify_events+0x99>
movslq %ebx,%rax
mov    (%rsp,%rax,1),%r13d
test   %r13d,%r13d
je     1190 <watch_inotify_events+0x60>
mov    (%r14),%ecx
and    0x4(%rsp,%rax,1),%ecx
mov    0x8(%rsp,%rax,1),%rsi
mov    %r12,%rdi
cmove  %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1190 <watch_inotify_events+0x60>
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* }
@.str = [23 x i8] c"counld not get event!\0A\00"
@IN_CREATE = global i32 0
@.str.1 = [26 x i8] c"zing --> create file: %s\0A\00"
@.str.2 = [26 x i8] c"zing --> delete file: %s\0A\00"
define i32 @watch_inotify_events(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.inotify_event*
store i32 %0, i32* %3
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %3
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 512)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 16
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
16: ; preds = %1
br label %17
17: ; preds = %48, %16
%18 = load i32, i32* %5
%19 = icmp sge i32 %18, 16
br i1 %19, label %20, label %61
20: ; preds = %17
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = bitcast i8* %24 to %struct.inotify_event*
store %struct.inotify_event* %25, %struct.inotify_event** %8
%26 = load %struct.inotify_event*, %struct.inotify_event** %8
%27 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %20
%31 = load %struct.inotify_event*, %struct.inotify_event** %8
%32 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i32, i32* @IN_CREATE
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load %struct.inotify_event*, %struct.inotify_event** %8
%39 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %38, i32 0, i32 2
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %47
42: ; preds = %30
%43 = load %struct.inotify_event*, %struct.inotify_event** %8
%44 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %43, i32 0, i32 2
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %42, %37
br label %48
48: ; preds = %47, %20
%49 = load %struct.inotify_event*, %struct.inotify_event** %8
%50 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
%53 = add i64 16, %52
%54 = trunc i64 %53 to i32
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = load i32, i32* %5
%57 = sub nsw i32 %56, %55
store i32 %57, i32* %5
%58 = load i32, i32* %7
%59 = load i32, i32* %6
%60 = add nsw i32 %59, %58
store i32 %60, i32* %6
br label %17
61: ; preds = %17
store i32 0, i32* %2
br label %62
62: ; preds = %61, %14
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@IN_CREATE = global i32 0
@.str.1 = [26 x i8] c"zing --> create file: %s\0A\00"
@.str.2 = [26 x i8] c"zing --> delete file: %s\0A\00"
@str = [22 x i8] c"counld not get event!\00"
define i32 @watch_inotify_events(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 %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %33
8: ; preds = %1, %28
%9 = phi i32 [ %31, %28 ], [ 0, %1 ]
%10 = phi i32 [ %30, %28 ], [ %4, %1 ]
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %11
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %8
%17 = getelementptr inbounds i8, i8* %12, i64 4
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
%20 = load i32, i32* @IN_CREATE
%21 = and i32 %20, %19
%22 = icmp eq i32 %21, 0
%23 = getelementptr inbounds i8, i8* %12, i64 8
%24 = bitcast i8* %23 to i8**
%25 = load i8*, i8** %24
%26 = select i1 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %26, i8* %25)
br label %28
28: ; preds = %16, %8
%29 = add i32 %14, 16
%30 = sub nsw i32 %10, %29
%31 = add nsw i32 %29, %9
%32 = icmp sgt i32 %30, 15
br i1 %32, label %8, label %33
33: ; preds = %28, %6
%34 = phi i32 [ -1, %6 ], [ 0, %28 ]
ret i32 %34
}