#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
scalar_t__ strlen(char const *);
int checkforrunning(const char *fname) {
  char buffer[64];
  int pidfile;
  pid_t pid;
  if (!fname || (strlen(fname) == 0))
    return -1;
  if ((pidfile = open(fname, O_RDONLY)) < 0)
    return 0;
  memset(buffer, 0, 64);
  if (read(pidfile, buffer, 63)) {
    if ((pid = atol(buffer)) > 0) {
      if (!kill(pid, 0)) {
        close(pidfile);
        return -2;
      }
    }
  }
  close(pidfile);
  return 0;
}


x86 assembly (clang, -O0):
<checkforrunning>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <checkforrunning+0x2a>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    11a6 <checkforrunning+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <checkforrunning+0xe0>
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    11d0 <checkforrunning+0x60>
movl   $0x0,-0x4(%rbp)
jmp    1250 <checkforrunning+0xe0>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x54(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     1241 <checkforrunning+0xd1>
lea    -0x50(%rbp),%rdi
call   1080 <atol@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
jle    123c <checkforrunning+0xcc>
mov    -0x58(%rbp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0x0,%eax
jne    1237 <checkforrunning+0xc7>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1250 <checkforrunning+0xe0>
jmp    123c <checkforrunning+0xcc>
jmp    1241 <checkforrunning+0xd1>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkforrunning>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11dc <checkforrunning+0x7c>
cmpb   $0x0,(%rdi)
je     11dc <checkforrunning+0x7c>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11da <checkforrunning+0x7a>
mov    %eax,%ebp
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x3f,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11d1 <checkforrunning+0x71>
mov    %rsp,%rdi
call   1070 <atol@plt>
test   %eax,%eax
jle    11d1 <checkforrunning+0x71>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <kill@plt>
test   %eax,%eax
jne    11d1 <checkforrunning+0x71>
mov    $0xfffffffe,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11dc <checkforrunning+0x7c>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @checkforrunning(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9, %1
store i32 -1, i32* %2
br label %43
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 0, i32* %2
br label %43
20: ; preds = %14
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 64)
%23 = load i32, i32* %5
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%25 = call i64 @read(i32 %23, i8* %24, i32 63)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %20
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%29 = call i32 @atol(i8* %28)
store i32 %29, i32* %6
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i32, i32* %6
%33 = call i32 @kill(i32 %32, i32 0)
%34 = icmp ne i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 -2, i32* %2
br label %43
38: ; preds = %31
br label %39
39: ; preds = %38, %27
br label %40
40: ; preds = %39, %20
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
store i32 0, i32* %2
br label %43
43: ; preds = %40, %35, %19, %13
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @checkforrunning(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %26, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %5
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = call i32 @memset(i8* %3, i32 0, i32 64)
%14 = call i64 @read(i32 %10, i8* %3, i32 63)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = call i32 @atol(i8* %3)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @kill(i32 %17, i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %16, %19, %12
br label %23
23: ; preds = %19, %22
%24 = phi i32 [ 0, %22 ], [ -2, %19 ]
%25 = call i32 @close(i32 %10)
br label %26
26: ; preds = %23, %8, %1, %5
%27 = phi i32 [ -1, %5 ], [ -1, %1 ], [ 0, %8 ], [ %24, %23 ]
ret i32 %27
}