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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ENOENT;
scalar_t__ ESRCH;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int isAlreadyRunning(const char *pidFile) {
  FILE *fp = fopen(pidFile, "r");
  if (!fp) {
    if (errno == ENOENT) {
      return 0;
    }
    return -1;
  }
  pid_t pid;
  if (fscanf(fp, "%i", &pid) != 1) {
    fclose(fp);
    errno = EINVAL;
    return -1;
  }
  fclose(fp);
  if (kill(pid, 0) < 0) {
    if (errno == ESRCH) {
      return 0;
    }
    return -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<isAlreadyRunning>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119d <isAlreadyRunning+0x5d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <isAlreadyRunning+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1239 <isAlreadyRunning+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <isAlreadyRunning+0xf9>
mov    -0x18(%rbp),%rdi
lea    0xe5a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11e3 <isAlreadyRunning+0xa3>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rcx
mov    0x2e0c(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <isAlreadyRunning+0xf9>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
jge    1232 <isAlreadyRunning+0xf2>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <isAlreadyRunning+0xe6>
movl   $0x0,-0x4(%rbp)
jmp    1239 <isAlreadyRunning+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <isAlreadyRunning+0xf9>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isAlreadyRunning>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a5 <isAlreadyRunning+0x65>
mov    %rax,%rbx
lea    0xea4(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
jne    11c2 <isAlreadyRunning+0x82>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    11db <isAlreadyRunning+0x9b>
mov    0x2e47(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1d(%rip),%rdx
jmp    11b6 <isAlreadyRunning+0x76>
mov    0x2e34(%rip),%rax
mov    (%rax),%rcx
mov    0x2e12(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    11db <isAlreadyRunning+0x9b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [3 x i8] c"%i\00"
@EINVAL = global i64 0
@ESRCH = global i64 0
define i32 @isAlreadyRunning(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %2
br label %37
15: ; preds = %10
store i32 -1, i32* %2
br label %37
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
%23 = load i64, i64* @EINVAL
store i64 %23, i64* @errno
store i32 -1, i32* %2
br label %37
24: ; preds = %16
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %5
%28 = call i64 @kill(i32 %27, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load i64, i64* @errno
%32 = load i64, i64* @ESRCH
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 0, i32* %2
br label %37
35: ; preds = %30
store i32 -1, i32* %2
br label %37
36: ; preds = %24
store i32 1, i32* %2
br label %37
37: ; preds = %36, %35, %34, %20, %15, %14
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [3 x i8] c"%i\00"
@EINVAL = global i64 0
@ESRCH = global i64 0
define i32 @isAlreadyRunning(i8* nocapture %0) {
%2 = alloca i32
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp ne i64 %6, %7
%9 = sext i1 %8 to i32
br label %28
10: ; preds = %1
%11 = bitcast i32* %2 to i8*
%12 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%13 = icmp eq i32 %12, 1
%14 = call i32 @fclose(i32* %3)
br i1 %13, label %17, label %15
15: ; preds = %10
%16 = load i64, i64* @EINVAL
store i64 %16, i64* @errno
br label %26
17: ; preds = %10
%18 = load i32, i32* %2
%19 = call i64 @kill(i32 %18, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @ESRCH
%24 = icmp ne i64 %22, %23
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %17, %21, %15
%27 = phi i32 [ -1, %15 ], [ %25, %21 ], [ 1, %17 ]
br label %28
28: ; preds = %5, %26
%29 = phi i32 [ %27, %26 ], [ %9, %5 ]
ret i32 %29
}