#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 */

/* Variables and functions */
scalar_t__ ESRCH;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
scalar_t__ kill(int, int /*<<< orphan*/);
int read_pid(char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char const *);
int already_running(const char *pidfile) {
  int pid = read_pid(pidfile);
  if (!pid)
    return 0;
  if (kill(pid, 0) < 0) {
    if (errno == ESRCH) {
      unlink(pidfile);
      return 0;
    }
    fprintf(stderr, "Failed to signal process %d: %s\n", pid, strerror(errno));
  }
  return pid;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <read_pid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    117e <already_running+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    11fd <already_running+0xad>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
jge    11f7 <already_running+0xa7>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <already_running+0x71>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <already_running+0xad>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    %rax,%rcx
lea    0xe0e(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <read_pid@plt>
test   %eax,%eax
je     118e <already_running+0x3e>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
jns    11b3 <already_running+0x63>
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
mov    0x2e47(%rip),%rax
cmp    (%rax),%rdi
jne    1192 <already_running+0x42>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %ebx,%ebx
jmp    11b3 <already_running+0x63>
mov    0x2e47(%rip),%rax
mov    (%rax),%ebp
call   1070 <strerror@plt>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ESRCH = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to signal process %d: %s\0A\00"
define i32 @already_running(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @read_pid(i8* %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %29
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i64 @kill(i32 %11, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i64, i64* @errno
%16 = load i64, i64* @ESRCH
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = call i32 @unlink(i8* %19)
store i32 0, i32* %2
br label %29
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = load i32, i32* %4
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %23, i8* %25)
br label %27
27: ; preds = %21, %10
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %18, %9
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@errno = global i64 0
@ESRCH = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to signal process %d: %s\0A\00"
define i32 @already_running(i8* %0) {
%2 = tail call i32 @read_pid(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i64 @kill(i32 %2, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @ESRCH
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @unlink(i8* %0)
br label %17
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i8* @strerror(i64 %8)
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %2, i8* %15)
br label %17
17: ; preds = %4, %13, %1, %11
%18 = phi i32 [ 0, %11 ], [ 0, %1 ], [ %2, %13 ], [ %2, %4 ]
ret i32 %18
}