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

/* Variables and functions */
scalar_t__ ESRCH;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printlog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ strerror(scalar_t__);
pid_t check_pid(char *pidfile_path) {
  FILE *file;
  pid_t pid;
  if ((file = fopen(pidfile_path, "r")) == NULL) {
    printlog(LOG_DEBUG, "Cannot open the pidfile(%s): %s", pidfile_path,
             strerror(errno));
    goto fail;
  }
  (void)fscanf(file, "%d", &pid);
  (void)fclose(file);
  if (pid <= 0 || pid == getpid()) {
    printlog(LOG_ERR, "pid is invalid or myself(pid=%d)", pid);
    goto fail;
  }
  if (kill(pid, 0) && errno == ESRCH) {
    goto fail;
  }
  return (pid);
fail:
  return (-1);
}


x86 assembly (clang, -O0):
<check_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d9 <check_pid+0x69>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1080 <printlog@plt>
jmp    1277 <check_pid+0x107>
mov    -0x18(%rbp),%rdi
lea    0xe3e(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    121a <check_pid+0xaa>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jne    1239 <check_pid+0xc9>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1080 <printlog@plt>
jmp    1277 <check_pid+0x107>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     126c <check_pid+0xfc>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <check_pid+0xfc>
jmp    1277 <check_pid+0x107>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127e <check_pid+0x10e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11de <check_pid+0x6e>
mov    %rax,%rbx
lea    0xe90(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%ebx
test   %ebx,%ebx
jle    11be <check_pid+0x4e>
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %eax,%ebx
mov    0x4(%rsp),%ebx
jne    1212 <check_pid+0xa2>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <printlog@plt>
mov    $0xffffffff,%eax
jmp    123f <check_pid+0xcf>
mov    0x2df3(%rip),%rax
mov    (%rax),%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe05(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <printlog@plt>
mov    $0xffffffff,%eax
jmp    123f <check_pid+0xcf>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     123b <check_pid+0xcb>
mov    0x2db9(%rip),%rax
mov    (%rax),%rcx
mov    0x2d8f(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
je     123f <check_pid+0xcf>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@LOG_DEBUG = global i32 0
@.str.1 = [32 x i8] c"Cannot open the pidfile(%s): %s\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@LOG_ERR = global i32 0
@.str.3 = [33 x i8] c"pid is invalid or myself(pid=%d)\00"
@ESRCH = global i64 0
define i32 @check_pid(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 = icmp eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @LOG_DEBUG
%11 = load i8*, i8** %3
%12 = load i64, i64* @errno
%13 = call i32 @strerror(i64 %12)
%14 = call i32 (i32, i8*, ...) @printlog(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %13)
br label %41
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
%20 = load i32, i32* %5
%21 = icmp sle i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = load i32, i32* %5
%24 = call i32 (...) @getpid()
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22, %15
%27 = load i32, i32* @LOG_ERR
%28 = load i32, i32* %5
%29 = call i32 (i32, i8*, ...) @printlog(i32 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %41
30: ; preds = %22
%31 = load i32, i32* %5
%32 = call i64 @kill(i32 %31, i32 0)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @ESRCH
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %41
39: ; preds = %34, %30
%40 = load i32, i32* %5
store i32 %40, i32* %2
br label %42
41: ; preds = %38, %26, %9
store i32 -1, i32* %2
br label %42
42: ; preds = %41, %39
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@LOG_DEBUG = global i32 0
@.str.1 = [32 x i8] c"Cannot open the pidfile(%s): %s\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@LOG_ERR = global i32 0
@.str.3 = [33 x i8] c"pid is invalid or myself(pid=%d)\00"
@ESRCH = global i64 0
define i32 @check_pid(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @LOG_DEBUG
%8 = load i64, i64* @errno
%9 = tail call i32 @strerror(i64 %8)
%10 = tail call i32 (i32, i8*, ...) @printlog(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %9)
br label %33
11: ; preds = %1
%12 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%13 = call i32 @fclose(i32* %4)
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 1
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = call i32 (...) @getpid()
%18 = icmp eq i32 %14, %17
%19 = load i32, i32* %2
br i1 %18, label %20, label %24
20: ; preds = %16, %11
%21 = phi i32 [ %14, %11 ], [ %19, %16 ]
%22 = load i32, i32* @LOG_ERR
%23 = call i32 (i32, i8*, ...) @printlog(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %33
24: ; preds = %16
%25 = call i64 @kill(i32 %19, i32 0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @ESRCH
%30 = icmp eq i64 %28, %29
br i1 %30, label %33, label %31
31: ; preds = %27, %24
%32 = load i32, i32* %2
br label %33
33: ; preds = %6, %20, %27, %31
%34 = phi i32 [ %32, %31 ], [ -1, %27 ], [ -1, %20 ], [ -1, %6 ]
ret i32 %34
}