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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ eprint(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
scalar_t__ kill(unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ strerror(scalar_t__);
int check_pidfile(void) {
  unsigned long pid;
  FILE *f;
  f = fopen("/run/cpacfstatsd.pid", "r");
  if (!f) {
    if (errno == ENOENT) {
      return 0;
    }
    eprint("Unknown error on pid file check '%s', errno=%d [%s]\n",
           "/run/cpacfstatsd.pid", errno, strerror(errno));
    return -1;
  }
  if (fscanf(f, "%lu", &pid) != 1) {
    fclose(f);
    remove("/run/cpacfstatsd.pid");
    return 0;
  }
  fclose(f);
  if (kill(pid, 0) != 0) {
    remove("/run/cpacfstatsd.pid");
    return 0;
  }
  eprint("Looks like there is another cpacfstatsd (pid=%lu) running\n", pid);
  eprint("Please check and maybe remove stale pid file '%s'\n",
         "/run/cpacfstatsd.pid");
  return -1;
}


x86 assembly (clang, -O0):
<check_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <check_pidfile+0x94>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <check_pidfile+0x50>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <check_pidfile+0x136>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
lea    0xe2d(%rip),%rdi
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1090 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <check_pidfile+0x136>
mov    -0x18(%rbp),%rdi
lea    0xe3d(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1242 <check_pidfile+0xd2>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdcf(%rip),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <check_pidfile+0x136>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1278 <check_pidfile+0x108>
lea    0xd99(%rip),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <check_pidfile+0x136>
mov    -0x10(%rbp),%rsi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1090 <eprint@plt>
lea    0xdfa(%rip),%rdi
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1090 <eprint@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pidfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d7 <check_pidfile+0x67>
mov    %rax,%rbx
lea    0xeb2(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    1213 <check_pidfile+0xa3>
mov    0x8(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     1223 <check_pidfile+0xb3>
lea    0xe30(%rip),%rdi
call   1030 <remove@plt>
jmp    1250 <check_pidfile+0xe0>
mov    0x2e02(%rip),%rax
mov    (%rax),%r14
mov    0x2de8(%rip),%rax
xor    %ebx,%ebx
cmp    (%rax),%r14
je     1250 <check_pidfile+0xe0>
mov    %r14,%rdi
call   1080 <strerror@plt>
lea    0xe19(%rip),%rdi
lea    0xdfb(%rip),%rsi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <eprint@plt>
jmp    124b <check_pidfile+0xdb>
lea    0xde6(%rip),%rdi
call   1030 <remove@plt>
xor    %ebx,%ebx
jmp    1250 <check_pidfile+0xe0>
mov    0x8(%rsp),%rsi
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1090 <eprint@plt>
lea    0xe4e(%rip),%rdi
lea    0xdbc(%rip),%rsi
xor    %eax,%eax
call   1090 <eprint@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/run/cpacfstatsd.pid\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [53 x i8] c"Unknown error on pid file check '%s', errno=%d [%s]\0A\00"
@.str.3 = [4 x i8] c"%lu\00"
@.str.4 = [59 x i8] c"Looks like there is another cpacfstatsd (pid=%lu) running\0A\00"
@.str.5 = [51 x i8] c"Please check and maybe remove stale pid file '%s'\0A\00"
define i32 @check_pidfile() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32*
%4 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 0, i32* %1
br label %37
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @errno
%15 = call i32 @strerror(i64 %14)
%16 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %13, i32 %15)
store i32 -1, i32* %1
br label %37
17: ; preds = %0
%18 = load i32*, i32** %3
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32*, i32** %3
%23 = call i32 @fclose(i32* %22)
%24 = call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %37
25: ; preds = %17
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
%28 = load i64, i64* %2
%29 = call i64 @kill(i64 %28, i32 0)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %37
33: ; preds = %25
%34 = load i64, i64* %2
%35 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i64 %34)
%36 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %37
37: ; preds = %33, %31, %21, %12, %11
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [21 x i8] c"/run/cpacfstatsd.pid\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [53 x i8] c"Unknown error on pid file check '%s', errno=%d [%s]\0A\00"
@.str.3 = [4 x i8] c"%lu\00"
@.str.4 = [59 x i8] c"Looks like there is another cpacfstatsd (pid=%lu) running\0A\00"
@.str.5 = [51 x i8] c"Please check and maybe remove stale pid file '%s'\0A\00"
define i32 @check_pidfile() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %12
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %28, label %9
9: ; preds = %5
%10 = tail call i32 @strerror(i64 %6)
%11 = tail call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %6, i32 %10)
br label %28
12: ; preds = %0
%13 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %1)
%14 = icmp eq i32 %13, 1
%15 = call i32 @fclose(i32* %3)
br i1 %14, label %18, label %16
16: ; preds = %12
%17 = call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %28
18: ; preds = %12
%19 = load i64, i64* %1
%20 = call i64 @kill(i64 %19, i32 0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %28
24: ; preds = %18
%25 = load i64, i64* %1
%26 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i64 %25)
%27 = call i32 (i8*, ...) @eprint(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %5, %24, %22, %16, %9
%29 = phi i32 [ 0, %16 ], [ 0, %22 ], [ -1, %24 ], [ -1, %9 ], [ 0, %5 ]
ret i32 %29
}