#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 {
  scalar_t__ len;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ IN_CLOSE_WRITE;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_NOTICE;
int LOG_NOWAIT;
int LOG_PID;
int /*<<< orphan*/ LOG_USER;
int NAME_MAX;
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ daemon(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int inotify_add_watch(int, char *, int /*<<< orphan*/);
int inotify_init();
scalar_t__ isNotifydRunning();
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ trapCrashFile(struct inotify_event *);
int main() {
  int inotifyFd, wd;
  char buf[(10 * (sizeof(struct inotify_event) + NAME_MAX + 1))]
      __attribute__((aligned(4)));
  ssize_t numRead;
  char *p;
  struct inotify_event *event;
  if (isNotifydRunning()) {
    exit(-1);
  }
  daemon(0, 0);
  openlog("apport-notifyd", LOG_NOWAIT | LOG_PID, LOG_USER);
  syslog(LOG_NOTICE, "Successfully started daemon\n");
  inotifyFd = inotify_init();
  if (inotifyFd == -1)
    syslog(LOG_NOTICE, "inotify_init");
  wd = inotify_add_watch(inotifyFd, "/var/crash/", IN_CLOSE_WRITE);
  if (wd == -1)
    syslog(LOG_NOTICE, "inotify_add_watch");
  for (;;) {
    numRead = read(inotifyFd, buf,
                   (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)));
    if (numRead == 0)
      syslog(LOG_NOTICE, "read() from inotify fd returned 0!");
    if (numRead == -1)
      syslog(LOG_DEBUG, "read");
    syslog(LOG_DEBUG, "Read %ld bytes from inotify fd\n", (long)numRead);
    for (p = buf; p < buf + numRead;) {
      event = (struct inotify_event *)p;
      trapCrashFile(event);
      syslog(LOG_NOTICE, "Got invoked\n");
      p += sizeof(struct inotify_event) + event->len + 1;
    }
  }
  closelog();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e02(%rip),%rax
movslq (%rax),%rcx
add    %rcx,%rcx
lea    0x5a(%rcx,%rcx,4),%rax
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
lea    0x69(%rcx,%rcx,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <isNotifydRunning@plt>
cmp    $0x0,%rax
je     11ed <main+0x5d>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1080 <daemon@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0xde8(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x0,%al
call   1050 <inotify_init@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    125f <main+0xcf>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x8(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
lea    0xdc7(%rip),%rsi
call   1060 <inotify_add_watch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    129b <main+0x10b>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
jmp    12a0 <main+0x110>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2cfa(%rip),%rax
movslq (%rax),%rax
add    $0x8,%rax
add    $0x1,%rax
imul   $0xa,%rax,%rax
mov    %eax,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12e8 <main+0x158>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1309 <main+0x179>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
movslq -0x24(%rbp),%rdx
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1388 <main+0x1f8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <trapCrashFile@plt>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
add    $0x8,%rax
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    132c <main+0x19c>
jmp    12a0 <main+0x110>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e03(%rip),%rax
movslq (%rax),%rax
lea    (%rax,%rax,4),%rax
mov    %rsp,%rcx
add    %rax,%rax
add    $0x69,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
xor    %eax,%eax
call   1090 <isNotifydRunning@plt>
test   %rax,%rax
jne    132a <main+0x19a>
xor    %edi,%edi
xor    %esi,%esi
call   1080 <daemon@plt>
mov    0x2de4(%rip),%rax
mov    0x2de5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
lea    0xe01(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x2db5(%rip),%r12
mov    (%r12),%edi
lea    0xdf9(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
xor    %eax,%eax
call   1050 <inotify_init@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1243 <main+0xb3>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
lea    0xde6(%rip),%rsi
mov    %r15d,%edi
call   1060 <inotify_add_watch@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xe7>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
lea    0xe21(%rip),%r13
xchg   %ax,%ax
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
lea    (%rax,%rax,4),%eax
lea    (%rax,%rax,1),%edx
add    $0x5a,%edx
mov    %r15d,%ebx
mov    %r15d,%edi
mov    -0x30(%rbp),%rsi
call   1040 <read@plt>
mov    %eax,%r15d
mov    %r12,%rax
lea    0xda9(%rip),%rsi
test   %r15d,%r15d
je     12c7 <main+0x137>
cmp    $0xffffffff,%r15d
jne    12d0 <main+0x140>
mov    0x2cf8(%rip),%rax
lea    0xdb3(%rip),%rsi
mov    (%rax),%edi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
movslq %r15d,%r14
lea    0xd9c(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
test   %r15d,%r15d
mov    %ebx,%r15d
jle    1280 <main+0xf0>
mov    -0x30(%rbp),%rbx
add    %rbx,%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <trapCrashFile@plt>
mov    (%r12),%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    (%rbx),%rax
add    %rax,%rbx
add    $0x9,%rbx
cmp    %r14,%rbx
jb     1300 <main+0x170>
jmp    1280 <main+0xf0>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.inotify_event = type { i64 }
@NAME_MAX = global i32 0
@.str = [15 x i8] c"apport-notifyd\00"
@LOG_NOWAIT = global i32 0
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [29 x i8] c"Successfully started daemon\0A\00"
@.str.2 = [13 x i8] c"inotify_init\00"
@.str.3 = [12 x i8] c"/var/crash/\00"
@IN_CLOSE_WRITE = global i32 0
@.str.4 = [18 x i8] c"inotify_add_watch\00"
@.str.5 = [35 x i8] c"read() from inotify fd returned 0!\00"
@LOG_DEBUG = global i32 0
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [32 x i8] c"Read %ld bytes from inotify fd\0A\00"
@.str.8 = [13 x i8] c"Got invoked\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.inotify_event*
store i32 0, i32* %1
%9 = load i32, i32* @NAME_MAX
%10 = sext i32 %9 to i64
%11 = add i64 8, %10
%12 = add i64 %11, 1
%13 = mul i64 10, %12
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8, i64 %13
store i64 %13, i64* %5
%16 = call i64 (...) @isNotifydRunning()
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %0
%21 = call i32 @daemon(i32 0, i32 0)
%22 = load i32, i32* @LOG_NOWAIT
%23 = load i32, i32* @LOG_PID
%24 = or i32 %22, %23
%25 = load i32, i32* @LOG_USER
%26 = call i32 @openlog(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* @LOG_NOTICE
%28 = call i32 (i32, i8*, ...) @syslog(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @inotify_init()
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = load i32, i32* @LOG_NOTICE
%34 = call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %20
%36 = load i32, i32* %2
%37 = load i32, i32* @IN_CLOSE_WRITE
%38 = call i32 @inotify_add_watch(i32 %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %37)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i32, i32* @LOG_NOTICE
%43 = call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %41, %35
br label %45
45: ; preds = %90, %44
%46 = load i32, i32* %2
%47 = load i32, i32* @NAME_MAX
%48 = sext i32 %47 to i64
%49 = add i64 8, %48
%50 = add i64 %49, 1
%51 = mul i64 10, %50
%52 = trunc i64 %51 to i32
%53 = call i32 @read(i32 %46, i8* %15, i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %45
%57 = load i32, i32* @LOG_NOTICE
%58 = call i32 (i32, i8*, ...) @syslog(i32 %57, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %56, %45
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* @LOG_DEBUG
%64 = call i32 (i32, i8*, ...) @syslog(i32 %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %62, %59
%66 = load i32, i32* @LOG_DEBUG
%67 = load i32, i32* %6
%68 = sext i32 %67 to i64
%69 = call i32 (i32, i8*, ...) @syslog(i32 %66, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i64 %68)
store i8* %15, i8** %7
br label %70
70: ; preds = %76, %65
%71 = load i8*, i8** %7
%72 = load i32, i32* %6
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %15, i64 %73
%75 = icmp ult i8* %71, %74
br i1 %75, label %76, label %90
76: ; preds = %70
%77 = load i8*, i8** %7
%78 = bitcast i8* %77 to %struct.inotify_event*
store %struct.inotify_event* %78, %struct.inotify_event** %8
%79 = load %struct.inotify_event*, %struct.inotify_event** %8
%80 = call i32 @trapCrashFile(%struct.inotify_event* %79)
%81 = load i32, i32* @LOG_NOTICE
%82 = call i32 (i32, i8*, ...) @syslog(i32 %81, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%83 = load %struct.inotify_event*, %struct.inotify_event** %8
%84 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %83, i32 0, i32 0
%85 = load i64, i64* %84
%86 = add i64 8, %85
%87 = add i64 %86, 1
%88 = load i8*, i8** %7
%89 = getelementptr inbounds i8, i8* %88, i64 %87
store i8* %89, i8** %7
br label %70
90: ; preds = %70
br label %45
}

LLVM IR (-O2):
%struct.inotify_event = type { i64 }
@NAME_MAX = global i32 0
@.str = [15 x i8] c"apport-notifyd\00"
@LOG_NOWAIT = global i32 0
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [29 x i8] c"Successfully started daemon\0A\00"
@.str.2 = [13 x i8] c"inotify_init\00"
@.str.3 = [12 x i8] c"/var/crash/\00"
@IN_CLOSE_WRITE = global i32 0
@.str.4 = [18 x i8] c"inotify_add_watch\00"
@.str.5 = [35 x i8] c"read() from inotify fd returned 0!\00"
@LOG_DEBUG = global i32 0
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [32 x i8] c"Read %ld bytes from inotify fd\0A\00"
@.str.8 = [13 x i8] c"Got invoked\0A\00"
define i32 @main() {
%1 = load i32, i32* @NAME_MAX
%2 = sext i32 %1 to i64
%3 = mul nsw i64 %2, 10
%4 = add nsw i64 %3, 90
%5 = alloca i8, i64 %4
%6 = tail call i64 (...) @isNotifydRunning()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = tail call i32 @daemon(i32 0, i32 0)
%12 = load i32, i32* @LOG_NOWAIT
%13 = load i32, i32* @LOG_PID
%14 = or i32 %13, %12
%15 = load i32, i32* @LOG_USER
%16 = tail call i32 @openlog(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* @LOG_NOTICE
%18 = tail call i32 (i32, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 (...) @inotify_init()
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %10
%22 = load i32, i32* @LOG_NOTICE
%23 = tail call i32 (i32, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %21, %10
%25 = load i32, i32* @IN_CLOSE_WRITE
%26 = tail call i32 @inotify_add_watch(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @LOG_NOTICE
%30 = tail call i32 (i32, i8*, ...) @syslog(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %28, %24
br label %32
32: ; preds = %49, %31
%33 = load i32, i32* @NAME_MAX
%34 = mul i32 %33, 10
%35 = add i32 %34, 90
%36 = call i32 @read(i32 %19, i8* %5, i32 %35)
switch i32 %36, label %43 [
i32 0, label %38
i32 -1, label %37
]
37: ; preds = %32
br label %38
38: ; preds = %32, %37
%39 = phi i32* [ @LOG_DEBUG, %37 ], [ @LOG_NOTICE, %32 ]
%40 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), %37 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), %32 ]
%41 = load i32, i32* %39
%42 = call i32 (i32, i8*, ...) @syslog(i32 %41, i8* %40)
br label %43
43: ; preds = %38, %32
%44 = load i32, i32* @LOG_DEBUG
%45 = sext i32 %36 to i64
%46 = call i32 (i32, i8*, ...) @syslog(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i64 %45)
%47 = getelementptr inbounds i8, i8* %5, i64 %45
%48 = icmp sgt i32 %36, 0
br i1 %48, label %50, label %49
49: ; preds = %50, %43
br label %32
50: ; preds = %43, %50
%51 = phi i8* [ %59, %50 ], [ %5, %43 ]
%52 = bitcast i8* %51 to %struct.inotify_event*
%53 = call i32 @trapCrashFile(%struct.inotify_event* %52)
%54 = load i32, i32* @LOG_NOTICE
%55 = call i32 (i32, i8*, ...) @syslog(i32 %54, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%56 = bitcast i8* %51 to i64*
%57 = load i64, i64* %56
%58 = add i64 %57, 9
%59 = getelementptr inbounds i8, i8* %51, i64 %58
%60 = icmp ult i8* %59, %47
br i1 %60, label %50, label %49
}