#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 stat {
  scalar_t__ st_uid;
};
struct dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ procInfoDir;
typedef int /*<<< orphan*/ cmdFile;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ getpid();
scalar_t__ getuid();
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ *strstr(char *, char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int isNotifydRunning() {
  DIR *d;
  if ((d = opendir("/proc")) == NULL) {
    exit(-1);
  }
  struct dirent *dir;
  struct stat st;
  int cmdFileFd;
  char cmdFile[64];
  char procInfoDir[64];
  char buf[512];
  while ((dir = readdir(d)) != NULL) {
    if (dir->d_type != DT_DIR || strtol(dir->d_name, NULL, 10) == getpid())
      continue;
    memset(procInfoDir, '\0', sizeof(procInfoDir));
    memset(cmdFile, '\0', sizeof(cmdFile));
    memset(buf, '\0', sizeof(buf));
    snprintf(procInfoDir, sizeof(procInfoDir), "/proc/%s", dir->d_name);
    stat(procInfoDir, &st);
    if (st.st_uid == getuid()) {
      snprintf(cmdFile, sizeof(cmdFile), "%s/cmdline", procInfoDir);
      if ((cmdFileFd = open(cmdFile, O_RDONLY)) > 0) {
        read(cmdFileFd, buf, sizeof(buf));
        if (strstr(buf, "apport-notifyd") != NULL) {
          close(cmdFileFd);
          closedir(d);
          return 1;
        }
        close(cmdFileFd);
      }
    }
  }
  closedir(d);
  return 0;
}


x86 assembly (clang, -O0):
<isNotifydRunning>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
lea    0xe0e(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    120f <isNotifydRunning+0x2f>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
jmp    1214 <isNotifydRunning+0x34>
mov    -0x10(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     13a3 <isNotifydRunning+0x1c3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    1279 <isNotifydRunning+0x99>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,-0x2b8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x2b8(%rbp),%rax
cmp    %rcx,%rax
jne    127e <isNotifydRunning+0x9e>
jmp    1214 <isNotifydRunning+0x34>
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    -0x2b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x40,%esi
lea    0xd37(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x2c0(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %rax,%rcx
mov    -0x2c0(%rbp),%rax
cmp    %rcx,%rax
jne    139e <isNotifydRunning+0x1be>
lea    -0x70(%rbp),%rdi
lea    -0xb0(%rbp),%rcx
mov    $0x40,%esi
lea    0xcef(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1399 <isNotifydRunning+0x1b9>
mov    -0x24(%rbp),%edi
lea    -0x2b0(%rbp),%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
lea    -0x2b0(%rbp),%rdi
lea    0xcb5(%rip),%rsi
call   1100 <strstr@plt>
cmp    $0x0,%rax
je     1391 <isNotifydRunning+0x1b1>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b3 <isNotifydRunning+0x1d3>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    139e <isNotifydRunning+0x1be>
jmp    1214 <isNotifydRunning+0x34>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isNotifydRunning>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x298,%rsp
lea    0xe08(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1369 <isNotifydRunning+0x189>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     134b <isNotifydRunning+0x16b>
mov    %rax,%r13
mov    0x2db4(%rip),%r12
lea    0x10(%rsp),%r14
lea    0x50(%rsp),%r15
jmp    1244 <isNotifydRunning+0x64>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     134b <isNotifydRunning+0x16b>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1230 <isNotifydRunning+0x50>
mov    0x8(%r13),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rax,%rbp
je     1230 <isNotifydRunning+0x50>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    0x90(%rsp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    0x8(%r13),%rcx
mov    %r14,%rdi
mov    $0x40,%esi
lea    0xd54(%rip),%rdx
call   1060 <snprintf@plt>
mov    %r14,%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
mov    0x8(%rsp),%rbp
xor    %eax,%eax
call   1050 <getuid@plt>
cmp    %rax,%rbp
jne    1230 <isNotifydRunning+0x50>
mov    %r15,%rdi
mov    $0x40,%esi
lea    0xd27(%rip),%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10e0 <open@plt>
test   %eax,%eax
jle    1230 <isNotifydRunning+0x50>
mov    %eax,%ebp
mov    %eax,%edi
lea    0x90(%rsp),%r13
mov    %r13,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
mov    %r13,%rdi
lea    0xcee(%rip),%rsi
call   1100 <strstr@plt>
mov    %rax,%r13
mov    %ebp,%edi
call   1080 <close@plt>
test   %r13,%r13
je     1230 <isNotifydRunning+0x50>
mov    $0x1,%ebp
jmp    134d <isNotifydRunning+0x16d>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0x298,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
%struct.stat = type { i64 }
@.str = [6 x i8] c"/proc\00"
@DT_DIR = global i64 0
@.str.1 = [9 x i8] c"/proc/%s\00"
@.str.2 = [11 x i8] c"%s/cmdline\00"
@O_RDONLY = global i32 0
@.str.3 = [15 x i8] c"apport-notifyd\00"
define i32 @isNotifydRunning() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca [64 x i8]
%7 = alloca [64 x i8]
%8 = alloca [512 x i8]
%9 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %74, %31, %13
%15 = load i32*, i32** %2
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %3
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %75
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @DT_DIR
%23 = icmp ne i64 %21, %22
br i1 %23, label %31, label %24
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %3
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 1
%27 = load i8*, i8** %26
%28 = call i64 @strtol(i8* %27, i32* null, i32 10)
%29 = call i64 (...) @getpid()
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %24, %18
br label %14
32: ; preds = %24
%33 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i8 signext 0, i32 64)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%36 = call i32 @memset(i8* %35, i8 signext 0, i32 64)
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i8 signext 0, i32 512)
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%40 = load %struct.dirent*, %struct.dirent** %3
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 1
%42 = load i8*, i8** %41
%43 = call i32 @snprintf(i8* %39, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%45 = call i32 @stat(i8* %44, %struct.stat* %4)
%46 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%47 = load i64, i64* %46
%48 = call i64 (...) @getuid()
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %74
50: ; preds = %32
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%52 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%53 = call i32 @snprintf(i8* %51, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%55 = load i32, i32* @O_RDONLY
%56 = call i32 @open(i8* %54, i32 %55)
store i32 %56, i32* %5
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %73
58: ; preds = %50
%59 = load i32, i32* %5
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 512)
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%63 = call i32* @strstr(i8* %62, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %70
65: ; preds = %58
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
%68 = load i32*, i32** %2
%69 = call i32 @closedir(i32* %68)
store i32 1, i32* %1
br label %78
70: ; preds = %58
%71 = load i32, i32* %5
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %50
br label %74
74: ; preds = %73, %32
br label %14
75: ; preds = %14
%76 = load i32*, i32** %2
%77 = call i32 @closedir(i32* %76)
store i32 0, i32* %1
br label %78
78: ; preds = %75, %65
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i64, i8* }
@.str = [6 x i8] c"/proc\00"
@DT_DIR = global i64 0
@.str.1 = [9 x i8] c"/proc/%s\00"
@.str.2 = [11 x i8] c"%s/cmdline\00"
@O_RDONLY = global i32 0
@.str.3 = [15 x i8] c"apport-notifyd\00"
define i32 @isNotifydRunning() {
%1 = alloca %struct.stat
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = alloca [512 x i8]
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = bitcast %struct.stat* %1 to i8*
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%14 = call %struct.dirent* @readdir(i32* %5)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %53, label %16
16: ; preds = %9
%17 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %18
18: ; preds = %16, %30
%19 = phi %struct.dirent* [ %14, %16 ], [ %31, %30 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @DT_DIR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 1
%26 = load i8*, i8** %25
%27 = call i64 @strtol(i8* nocapture %26, i32* null, i32 10)
%28 = call i64 (...) @getpid()
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %48, %33, %43, %18, %24
%31 = call %struct.dirent* @readdir(i32* %5)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %53, label %18
33: ; preds = %24
%34 = call i32 @memset(i8* %12, i8 signext 0, i32 64)
%35 = call i32 @memset(i8* %11, i8 signext 0, i32 64)
%36 = call i32 @memset(i8* %13, i8 signext 0, i32 512)
%37 = load i8*, i8** %25
%38 = call i32 @snprintf(i8* %12, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @stat(i8* %12, %struct.stat* %1)
%40 = load i64, i64* %17
%41 = call i64 (...) @getuid()
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %30
43: ; preds = %33
%44 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* %11, i32 %45)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %30
48: ; preds = %43
%49 = call i32 @read(i32 %46, i8* %13, i32 512)
%50 = call i32* @strstr(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i32* %50, null
%52 = call i32 @close(i32 %46)
br i1 %51, label %30, label %53
53: ; preds = %30, %48, %9
%54 = phi i32 [ 0, %9 ], [ 0, %30 ], [ 1, %48 ]
%55 = call i32 @closedir(i32* %5)
ret i32 %54
}