#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 */
typedef int /*<<< orphan*/ uint32_t;
struct inotify_event {
  int wd;
  int mask;
  scalar_t__ len;
};

/* Variables and functions */
int IN_CLOSE_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int inotify_add_watch(int, char *, int);
int inotify_init();
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main() {
  int fd;
  if (-1 == (fd = inotify_init())) {
    fprintf(stderr, "inotify init failed. %s\n", strerror(errno));
    return -1;
  }
  int wd;
  if (-1 == (wd = inotify_add_watch(fd, "./tmp_dir", IN_CLOSE_WRITE))) {
    fprintf(stderr, "inotify add watch failed. %s\n", strerror(errno));
    return -1;
  }
  char buf[1024];
  int len, i;
  struct inotify_event *e;
  while (-1 != (len = read(fd, buf, 1024))) {
    for (i = 0; i < len;) {
      e = (struct inotify_event *)buf + i;
      fprintf(stderr, "wd: %d events : 0x%x\n", e->wd, e->mask);
      if (e->mask & IN_CLOSE_WRITE) {
        fprintf(stderr, "file was modified.\n");
      }
      i += sizeof(int);
      i += 3 * sizeof(uint32_t);
      i += e->len;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <inotify_init@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11bb <main+0x6b>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x424(%rbp),%edi
mov    %rax,%rdx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <main+0x1e6>
mov    -0x8(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0xe4b(%rip),%rsi
call   1060 <inotify_add_watch@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1225 <main+0xd5>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x428(%rbp),%edi
mov    %rax,%rdx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <main+0x1e6>
jmp    122a <main+0xda>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     132f <main+0x1df>
movl   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    132a <main+0x1da>
lea    -0x410(%rbp),%rax
movslq -0x418(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x420(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    -0x420(%rbp),%rax
mov    (%rax),%edx
mov    -0x420(%rbp),%rax
mov    0x4(%rax),%ecx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x420(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2d22(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12e8 <main+0x198>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movslq -0x418(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x418(%rbp)
movslq -0x418(%rbp),%rax
add    $0xc,%rax
mov    %eax,-0x418(%rbp)
mov    -0x420(%rbp),%rax
mov    0x8(%rax),%rcx
movslq -0x418(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x418(%rbp)
jmp    125d <main+0x10d>
jmp    122a <main+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
xor    %eax,%eax
call   1050 <inotify_init@plt>
cmp    $0xffffffff,%eax
je     1253 <main+0x103>
mov    %eax,%ebx
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
lea    0xe96(%rip),%rsi
mov    %ebx,%edi
call   1060 <inotify_add_watch@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0x123>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xff>
mov    %eax,%ebp
mov    0x2e22(%rip),%r14
lea    0xe84(%rip),%r13
mov    %ebx,0xc(%rsp)
jmp    11ea <main+0x9a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     124f <main+0xff>
test   %ebp,%ebp
jle    11d0 <main+0x80>
xor    %r15d,%r15d
jmp    1211 <main+0xc1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp,%r12,1),%eax
add    %eax,%r15d
add    $0x10,%r15d
cmp    %ebp,%r15d
jge    11d0 <main+0x80>
movslq %r15d,%r12
mov    (%r14),%edi
shl    $0x4,%r12
mov    0x10(%rsp,%r12,1),%edx
mov    0x14(%rsp,%r12,1),%ebx
mov    %r13,%rsi
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2db0(%rip),%rax
test   %ebx,(%rax)
je     1200 <main+0xb0>
mov    (%r14),%edi
lea    0xe11(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1200 <main+0xb0>
xor    %eax,%eax
jmp    12a2 <main+0x152>
mov    0x2d7e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd8f(%rip),%rsi
jmp    1291 <main+0x141>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd92(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32, i32, i64 }
@stderr = global i32 0
@.str = [25 x i8] c"inotify init failed. %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"./tmp_dir\00"
@IN_CLOSE_WRITE = global i32 0
@.str.2 = [30 x i8] c"inotify add watch failed. %s\0A\00"
@.str.3 = [22 x i8] c"wd: %d events : 0x%x\0A\00"
@.str.4 = [20 x i8] c"file was modified.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.inotify_event*
store i32 0, i32* %1
%8 = call i32 (...) @inotify_init()
store i32 %8, i32* %2
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %1
br label %77
15: ; preds = %0
%16 = load i32, i32* %2
%17 = load i32, i32* @IN_CLOSE_WRITE
%18 = call i32 @inotify_add_watch(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 %18, i32* %3
%19 = icmp eq i32 -1, %18
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i32 -1, i32* %1
br label %77
25: ; preds = %15
br label %26
26: ; preds = %75, %25
%27 = load i32, i32* %2
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 1024)
store i32 %29, i32* %5
%30 = icmp ne i32 -1, %29
br i1 %30, label %31, label %76
31: ; preds = %26
store i32 0, i32* %6
br label %32
32: ; preds = %59, %31
%33 = load i32, i32* %6
%34 = load i32, i32* %5
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %75
36: ; preds = %32
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%38 = bitcast i8* %37 to %struct.inotify_event*
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %38, i64 %40
store %struct.inotify_event* %41, %struct.inotify_event** %7
%42 = load i32, i32* @stderr
%43 = load %struct.inotify_event*, %struct.inotify_event** %7
%44 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load %struct.inotify_event*, %struct.inotify_event** %7
%47 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %48)
%50 = load %struct.inotify_event*, %struct.inotify_event** %7
%51 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = load i32, i32* @IN_CLOSE_WRITE
%54 = and i32 %52, %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %36
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %56, %36
%60 = load i32, i32* %6
%61 = sext i32 %60 to i64
%62 = add i64 %61, 4
%63 = trunc i64 %62 to i32
store i32 %63, i32* %6
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = add i64 %65, 12
%67 = trunc i64 %66 to i32
store i32 %67, i32* %6
%68 = load %struct.inotify_event*, %struct.inotify_event** %7
%69 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %68, i32 0, i32 2
%70 = load i64, i64* %69
%71 = load i32, i32* %6
%72 = sext i32 %71 to i64
%73 = add nsw i64 %72, %70
%74 = trunc i64 %73 to i32
store i32 %74, i32* %6
br label %32
75: ; preds = %32
br label %26
76: ; preds = %26
store i32 0, i32* %1
br label %77
77: ; preds = %76, %20, %10
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
%struct.inotify_event = type { i32, i32, i64 }
@stderr = global i32 0
@.str = [25 x i8] c"inotify init failed. %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"./tmp_dir\00"
@IN_CLOSE_WRITE = global i32 0
@.str.2 = [30 x i8] c"inotify add watch failed. %s\0A\00"
@.str.3 = [22 x i8] c"wd: %d events : 0x%x\0A\00"
@.str.4 = [20 x i8] c"file was modified.\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = tail call i32 (...) @inotify_init()
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %53
9: ; preds = %0
%10 = load i32, i32* @IN_CLOSE_WRITE
%11 = tail call i32 @inotify_add_watch(i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %53
18: ; preds = %9
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%20 = call i32 @read(i32 %2, i8* %19, i32 1024)
%21 = icmp eq i32 %20, -1
br i1 %21, label %52, label %22
22: ; preds = %18
%23 = bitcast [1024 x i8]* %1 to %struct.inotify_event*
br label %27
24: ; preds = %45, %27
%25 = call i32 @read(i32 %2, i8* %19, i32 1024)
%26 = icmp eq i32 %25, -1
br i1 %26, label %52, label %27
27: ; preds = %22, %24
%28 = phi i32 [ %20, %22 ], [ %25, %24 ]
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %24
30: ; preds = %27, %45
%31 = phi i32 [ %50, %45 ], [ 0, %27 ]
%32 = sext i32 %31 to i64
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %23, i64 %32, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %23, i64 %32, i32 1
%37 = load i32, i32* %36
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %37)
%39 = load i32, i32* @IN_CLOSE_WRITE
%40 = and i32 %39, %37
%41 = icmp eq i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %30
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %42, %30
%46 = add i32 %31, 16
%47 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %23, i64 %32, i32 2
%48 = load i64, i64* %47
%49 = trunc i64 %48 to i32
%50 = add i32 %46, %49
%51 = icmp slt i32 %50, %28
br i1 %51, label %30, label %24
52: ; preds = %24, %18
br label %53
53: ; preds = %13, %52, %4
%54 = phi i32 [ -1, %4 ], [ -1, %13 ], [ 0, %52 ]
ret i32 %54
}