#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 pid_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
char *rindex(char *, char);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int check_process_running(pid_t pid, const char *program_name, int *running) {
  char filename[32];
  char cmdline[1024];
  size_t bytes = 0;
  FILE *fp;
  char *idx;
  char final[1024];
  *running = 0;
  snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
  if ((fp = fopen(filename, "r")) == NULL) {
    if (errno != ENOENT) {
      printf("Failed to open '%s' for reading\n", filename);
      return 0;
    }
    return 1;
  }
  bytes = fread((char *)cmdline, sizeof(char), 1000, fp);
  if (!bytes)
    return 0;
  if (bytes != 1000 && !feof(fp))
    return 0;
  fclose(fp);
  if ((idx = rindex(cmdline, '/')) != NULL)
    strcpy(final, ++idx);
  else
    strcpy(final, cmdline);
  if (strcmp(final, program_name) == 0)
    *running = 1;
  return 1;
}


x86 assembly (clang, -O0):
<check_process_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x448(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe32(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x450(%rbp)
cmp    $0x0,%rax
jne    1236 <check_process_running+0xa6>
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%rax
je     122a <check_process_running+0x9a>
lea    -0x40(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1335 <check_process_running+0x1a5>
movl   $0x1,-0x4(%rbp)
jmp    1335 <check_process_running+0x1a5>
lea    -0x440(%rbp),%rdi
mov    -0x450(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1040 <fread@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jne    1274 <check_process_running+0xe4>
movl   $0x0,-0x4(%rbp)
jmp    1335 <check_process_running+0x1a5>
cmpq   $0x3e8,-0x448(%rbp)
je     12a6 <check_process_running+0x116>
mov    -0x450(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
jne    12a6 <check_process_running+0x116>
movl   $0x0,-0x4(%rbp)
jmp    1335 <check_process_running+0x1a5>
mov    -0x450(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x440(%rbp),%rdi
mov    $0x2f,%esi
call   10b0 <rindex@plt>
mov    %rax,-0x458(%rbp)
cmp    $0x0,%rax
je     12f7 <check_process_running+0x167>
lea    -0x860(%rbp),%rdi
mov    -0x458(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x458(%rbp)
call   1030 <strcpy@plt>
jmp    130a <check_process_running+0x17a>
lea    -0x860(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x860(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    132e <check_process_running+0x19e>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_process_running>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x820,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%ecx
movl   $0x0,(%rdx)
lea    0xe4c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <snprintf@plt>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1267 <check_process_running+0xd7>
mov    %rax,%rbx
lea    0x420(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1293 <check_process_running+0x103>
cmp    $0x3e8,%rax
je     121b <check_process_running+0x8b>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     1293 <check_process_running+0x103>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   10b0 <rindex@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r15,%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcmp@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1293 <check_process_running+0x103>
movl   $0x1,(%r12)
jmp    1293 <check_process_running+0x103>
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    0x2d58(%rip),%rcx
mov    $0x1,%ebp
cmp    (%rcx),%rax
je     1293 <check_process_running+0x103>
lea    0xd8a(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x820,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [33 x i8] c"Failed to open '%s' for reading\0A\00"
define i32 @check_process_running(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [32 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca [1024 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i64 0, i64* %10
%14 = load i32*, i32** %7
store i32 0, i32* %14
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @snprintf(i8* %15, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %11
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %29
21: ; preds = %3
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOENT
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 0, i32* %4
br label %67
28: ; preds = %21
store i32 1, i32* %4
br label %67
29: ; preds = %3
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%31 = load i32*, i32** %11
%32 = call i64 @fread(i8* %30, i32 1, i32 1000, i32* %31)
store i64 %32, i64* %10
%33 = load i64, i64* %10
%34 = icmp ne i64 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
store i32 0, i32* %4
br label %67
36: ; preds = %29
%37 = load i64, i64* %10
%38 = icmp ne i64 %37, 1000
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32*, i32** %11
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %39
store i32 0, i32* %4
br label %67
44: ; preds = %39, %36
%45 = load i32*, i32** %11
%46 = call i32 @fclose(i32* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = call i8* @rindex(i8* %47, i8 signext 47)
store i8* %48, i8** %12
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%52 = load i8*, i8** %12
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %12
%54 = call i32 @strcpy(i8* %51, i8* %53)
br label %59
55: ; preds = %44
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%58 = call i32 @strcpy(i8* %56, i8* %57)
br label %59
59: ; preds = %55, %50
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%61 = load i8*, i8** %6
%62 = call i64 @strcmp(i8* %60, i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = load i32*, i32** %7
store i32 1, i32* %65
br label %66
66: ; preds = %64, %59
store i32 1, i32* %4
br label %67
67: ; preds = %66, %43, %35, %28, %25
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [33 x i8] c"Failed to open '%s' for reading\0A\00"
define i32 @check_process_running(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [32 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i32 0, i32* %2
%10 = call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %34, label %17
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %34
19: ; preds = %3
%20 = call i64 @fread(i8* %8, i32 1, i32 1000, i32* %11)
switch i64 %20, label %21 [
i64 0, label %34
i64 1000, label %24
]
21: ; preds = %19
%22 = call i32 @feof(i32* %11)
%23 = icmp eq i32 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %19, %21
%25 = call i32 @fclose(i32* %11)
%26 = call i8* @rindex(i8* %8, i8 signext 47)
%27 = icmp eq i8* %26, null
%28 = getelementptr inbounds i8, i8* %26, i64 1
%29 = select i1 %27, i8* %8, i8* %28
%30 = call i32 @strcpy(i8* %9, i8* %29)
%31 = call i64 @strcmp(i8* %9, i8* %1)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %24
store i32 1, i32* %2
br label %34
34: ; preds = %24, %33, %21, %19, %13, %17
%35 = phi i32 [ 0, %17 ], [ 1, %13 ], [ 0, %19 ], [ 0, %21 ], [ 1, %33 ], [ 1, %24 ]
ret i32 %35
}