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

/* Variables and functions */
int EOF;
int PATH_MAX;
char *basename(char *);
scalar_t__ check_pid_valid(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int stat(char *, struct stat *);
int check_process_running(const char *prog, pid_t *pid) {
  pid_t oldpid;
  FILE *fp;
  char filename[PATH_MAX];
  char *cmd;
  int ret;
  struct stat st;
  *pid = -1;
  fp = NULL;
  memset(filename, 0, PATH_MAX);
  cmd = basename((char *)prog);
  snprintf(filename, sizeof(filename), "/var/run/%s.pid", cmd);
  ret = stat(filename, &st);
  if ((ret < 0) || (!st.st_size))
    return 0;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    return 0;
  }
  ret = fscanf(fp, "%d\n", &oldpid);
  fclose(fp);
  if ((ret == EOF) || (ret != 1))
    return 0;
  if (check_pid_valid(oldpid, cmd)) {
    *pid = oldpid;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_process_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
movq   $0x0,-0x28(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1070 <basename@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,-0x40(%rbp)
mov    %eax,%esi
mov    -0x40(%rbp),%rcx
lea    0xdf9(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x58(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jl     1230 <check_process_running+0xb0>
cmpl   $0x0,-0x48(%rbp)
jne    1243 <check_process_running+0xc3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <check_process_running+0x184>
mov    -0x58(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1275 <check_process_running+0xf5>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <check_process_running+0x184>
mov    -0x28(%rbp),%rdi
lea    0xd92(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    0x2d41(%rip),%rcx
cmp    (%rcx),%eax
je     12b1 <check_process_running+0x131>
cmpl   $0x1,-0x44(%rbp)
je     12c4 <check_process_running+0x144>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <check_process_running+0x184>
mov    -0x1c(%rbp),%edi
mov    -0x40(%rbp),%rsi
call   1090 <check_pid_valid@plt>
cmp    $0x0,%rax
je     12f6 <check_process_running+0x176>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1304 <check_process_running+0x184>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_process_running>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsp,-0x30(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
movl   $0xffffffff,(%rsi)
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r12,%rdi
call   1070 <basename@plt>
mov    %rax,%r12
lea    0xe20(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    -0x38(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %eax,%eax
js     126d <check_process_running+0xed>
cmpl   $0x0,-0x38(%rbp)
je     126d <check_process_running+0xed>
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     126d <check_process_running+0xed>
mov    %rax,%rbx
lea    0xdf0(%rip),%rsi
lea    -0x34(%rbp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%r13d
jne    126d <check_process_running+0xed>
mov    0x2d9a(%rip),%rax
cmpl   $0x1,(%rax)
je     126d <check_process_running+0xed>
mov    -0x34(%rbp),%edi
mov    %r12,%rsi
call   1090 <check_pid_valid@plt>
test   %rax,%rax
mov    -0x30(%rbp),%rcx
je     1271 <check_process_running+0xf1>
mov    -0x34(%rbp),%eax
mov    %eax,(%r14)
mov    $0x1,%r15d
jmp    1271 <check_process_running+0xf1>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/var/run/%s.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @check_process_running(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i32*, i32** %5
store i32 -1, i32* %18
store i32* null, i32** %7
%19 = load i32, i32* @PATH_MAX
%20 = call i32 @memset(i8* %17, i32 0, i32 %19)
%21 = load i8*, i8** %4
%22 = call i8* @basename(i8* %21)
store i8* %22, i8** %10
%23 = trunc i64 %15 to i32
%24 = load i8*, i8** %10
%25 = call i32 @snprintf(i8* %17, i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @stat(i8* %17, %struct.stat* %12)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp slt i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %2
%30 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%31 = load i32, i32* %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %29, %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %60
34: ; preds = %29
%35 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %7
%36 = load i32*, i32** %7
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 0, i32* %3
store i32 1, i32* %13
br label %60
39: ; preds = %34
%40 = load i32*, i32** %7
%41 = call i32 @fscanf(i32* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
store i32 %41, i32* %11
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %11
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
br i1 %46, label %50, label %47
47: ; preds = %39
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %47, %39
store i32 0, i32* %3
store i32 1, i32* %13
br label %60
51: ; preds = %47
%52 = load i32, i32* %6
%53 = load i8*, i8** %10
%54 = call i64 @check_pid_valid(i32 %52, i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i32, i32* %6
%58 = load i32*, i32** %5
store i32 %57, i32* %58
store i32 1, i32* %3
store i32 1, i32* %13
br label %60
59: ; preds = %51
store i32 0, i32* %3
store i32 1, i32* %13
br label %60
60: ; preds = %59, %56, %50, %38, %33
%61 = load i8*, i8** %8
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/var/run/%s.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @check_process_running(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = bitcast %struct.stat* %4 to i8*
store i32 -1, i32* %1
%11 = load i32, i32* @PATH_MAX
%12 = call i32 @memset(i8* %9, i32 0, i32 %11)
%13 = call i8* @basename(i8* %0)
%14 = call i32 @snprintf(i8* %9, i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @stat(i8* %9, %struct.stat* %4)
%16 = icmp sgt i32 %15, -1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 0
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %21, label %37
21: ; preds = %2
%22 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %37, label %24
24: ; preds = %21
%25 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%26 = call i32 @fclose(i32* %22)
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %25, 1
%29 = icmp eq i32 %27, 1
%30 = or i1 %28, %29
br i1 %30, label %37, label %31
31: ; preds = %24
%32 = load i32, i32* %3
%33 = call i64 @check_pid_valid(i32 %32, i8* %13)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = load i32, i32* %3
store i32 %36, i32* %1
br label %37
37: ; preds = %31, %24, %21, %2, %35
%38 = phi i32 [ 1, %35 ], [ 0, %2 ], [ 0, %21 ], [ 0, %24 ], [ 0, %31 ]
ret i32 %38
}