#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 mask;
  scalar_t__ len;
};

/* Variables and functions */
int IN_CREATE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int inotify_add_watch(int, char *, int);
int inotify_init();
int /*<<< orphan*/ inotify_rm_watch(int, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int main(int argc, char **argv) {
  struct inotify_event *event;
  int fd, wd, length, i = 0;
  char buffer[(1024 * ((sizeof(struct inotify_event)) + 16))], *dir;
  fd = inotify_init();
  if (fd < 0) {
    perror("inotify_init");
  }
  dir = (argc > 1) ? argv[1] : "../testcase/crash/";
  wd = inotify_add_watch(fd, dir, IN_CREATE);
  length = read(fd, buffer, (1024 * ((sizeof(struct inotify_event)) + 16)));
  if (length < 0) {
    perror("read");
  }
  while (i < length) {
    event = (struct inotify_event *)&buffer[i];
    if (event->len && event->mask & IN_CREATE) {
      if (fork() == 0) {
        execlp("xmessage", "xmessage", "-center", "New crash file !", NULL);
        perror("execlp");
        exit(0);
      }
    }
    i += (sizeof(struct inotify_event)) + event->len;
  }
  inotify_rm_watch(fd, wd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1050 <inotify_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d0 <main+0x40>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11ee <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8040(%rbp)
jmp    1201 <main+0x71>
lea    0xe18(%rip),%rax
mov    %rax,-0x8040(%rbp)
jmp    1201 <main+0x71>
mov    -0x8040(%rbp),%rax
mov    %rax,-0x8038(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x8038(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
call   1060 <inotify_add_watch@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x8030(%rbp),%rsi
mov    $0x8000,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1257 <main+0xc7>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
jmp    125c <main+0xcc>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1307 <main+0x177>
movslq -0x28(%rbp),%rcx
lea    -0x8030(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     12ec <main+0x15c>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2d4a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ec <main+0x15c>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12e7 <main+0x157>
lea    0xd6c(%rip),%rsi
lea    0xd6e(%rip),%rdx
lea    0xd6f(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1090 <execlp@plt>
lea    0xd6c(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    12ec <main+0x15c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x10,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
jmp    125c <main+0xcc>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10a0 <inotify_rm_watch@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x8040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8008,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <inotify_init@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11bf <main+0x2f>
cmp    $0x2,%ebp
jl     11d0 <main+0x40>
mov    0x8(%r14),%rsi
jmp    11d7 <main+0x47>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
cmp    $0x2,%ebp
jge    11b9 <main+0x29>
lea    0xe36(%rip),%rsi
mov    0x2e02(%rip),%r12
mov    (%r12),%edx
mov    %r15d,%edi
call   1060 <inotify_add_watch@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x8000,%edx
call   1040 <read@plt>
test   %eax,%eax
js     126f <main+0xdf>
je     127b <main+0xeb>
mov    %eax,%r13d
xor    %ebx,%ebx
jmp    121a <main+0x8a>
nopw   0x0(%rax,%rax,1)
add    %ebp,%ebx
add    $0x10,%ebx
cmp    %r13d,%ebx
jge    127b <main+0xeb>
movslq %ebx,%rax
mov    0x8(%rsp,%rax,1),%rbp
test   %rbp,%rbp
je     1210 <main+0x80>
mov    (%r12),%ecx
test   %ecx,(%rsp,%rax,1)
je     1210 <main+0x80>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1210 <main+0x80>
lea    0xde2(%rip),%rdi
lea    0xde4(%rip),%rdx
lea    0xde5(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   1090 <execlp@plt>
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdaa(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
mov    %r14d,%esi
call   10a0 <inotify_rm_watch@plt>
mov    %r15d,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x8008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32, i64 }
@.str = [13 x i8] c"inotify_init\00"
@.str.1 = [19 x i8] c"../testcase/crash/\00"
@IN_CREATE = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [9 x i8] c"xmessage\00"
@.str.4 = [8 x i8] c"-center\00"
@.str.5 = [17 x i8] c"New crash file !\00"
@.str.6 = [7 x i8] c"execlp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.inotify_event*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [32768 x i8]
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%13 = call i32 (...) @inotify_init()
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
br label %26
25: ; preds = %18
br label %26
26: ; preds = %25, %21
%27 = phi i8* [ %24, %21 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %25 ]
store i8* %27, i8** %12
%28 = load i32, i32* %7
%29 = load i8*, i8** %12
%30 = load i32, i32* @IN_CREATE
%31 = call i32 @inotify_add_watch(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %7
%33 = getelementptr inbounds [32768 x i8], [32768 x i8]* %11, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 32768)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
br label %40
40: ; preds = %68, %39
%41 = load i32, i32* %10
%42 = load i32, i32* %9
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %77
44: ; preds = %40
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [32768 x i8], [32768 x i8]* %11, i64 0, i64 %46
%48 = bitcast i8* %47 to %struct.inotify_event*
store %struct.inotify_event* %48, %struct.inotify_event** %6
%49 = load %struct.inotify_event*, %struct.inotify_event** %6
%50 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %68
53: ; preds = %44
%54 = load %struct.inotify_event*, %struct.inotify_event** %6
%55 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32, i32* @IN_CREATE
%58 = and i32 %56, %57
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %53
%61 = call i64 (...) @fork()
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32* null)
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @exit(i32 0)
unreachable
67: ; preds = %60
br label %68
68: ; preds = %67, %53, %44
%69 = load %struct.inotify_event*, %struct.inotify_event** %6
%70 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %69, i32 0, i32 1
%71 = load i64, i64* %70
%72 = add i64 16, %71
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = add i64 %74, %72
%76 = trunc i64 %75 to i32
store i32 %76, i32* %10
br label %40
77: ; preds = %40
%78 = load i32, i32* %7
%79 = load i32, i32* %8
%80 = call i32 @inotify_rm_watch(i32 %78, i32 %79)
%81 = load i32, i32* %7
%82 = call i32 @close(i32 %81)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"inotify_init\00"
@.str.1 = [19 x i8] c"../testcase/crash/\00"
@IN_CREATE = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [9 x i8] c"xmessage\00"
@.str.4 = [8 x i8] c"-center\00"
@.str.5 = [17 x i8] c"New crash file !\00"
@.str.6 = [7 x i8] c"execlp\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32768 x i8]
%4 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @inotify_init()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
br label %14
14: ; preds = %9, %11
%15 = phi i8* [ %13, %11 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %9 ]
%16 = load i32, i32* @IN_CREATE
%17 = tail call i32 @inotify_add_watch(i32 %5, i8* %15, i32 %16)
%18 = call i32 @read(i32 %5, i8* %4, i32 32768)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
22: ; preds = %14
%23 = icmp eq i32 %18, 0
br i1 %23, label %50, label %24
24: ; preds = %22, %45
%25 = phi i32 [ %48, %45 ], [ 0, %22 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 8
%29 = bitcast i8* %28 to i64*
%30 = load i64, i64* %29
%31 = icmp eq i64 %30, 0
br i1 %31, label %45, label %32
32: ; preds = %24
%33 = bitcast i8* %27 to i32*
%34 = load i32, i32* %33
%35 = load i32, i32* @IN_CREATE
%36 = and i32 %35, %34
%37 = icmp eq i32 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %32
%39 = tail call i64 (...) @fork()
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = tail call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32* null)
%43 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%44 = tail call i32 @exit(i32 0)
unreachable
45: ; preds = %38, %32, %24
%46 = trunc i64 %30 to i32
%47 = add i32 %25, 16
%48 = add i32 %47, %46
%49 = icmp slt i32 %48, %18
br i1 %49, label %24, label %50
50: ; preds = %45, %20, %22
%51 = tail call i32 @inotify_rm_watch(i32 %5, i32 %17)
%52 = tail call i32 @close(i32 %5)
ret i32 0
}