#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__ ESRCH;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int check_pid(const char *pidfile) {
  int pid = 0;
  FILE *f = fopen(pidfile, "r");
  if (f == NULL)
    return 0;
  int n = fscanf(f, "%d", &pid);
  fclose(f);
  if (n != 1 || pid == 0 || pid == getpid()) {
    return 0;
  }
  if (kill(pid, 0) && errno == ESRCH)
    return 0;
  return pid;
}


x86 assembly (clang, -O0):
<check_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <check_pid+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    1228 <check_pid+0xd8>
mov    -0x20(%rbp),%rdi
lea    0xe69(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x24(%rbp)
jne    11dc <check_pid+0x8c>
cmpl   $0x0,-0x14(%rbp)
je     11dc <check_pid+0x8c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jne    11e8 <check_pid+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1228 <check_pid+0xd8>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1222 <check_pid+0xd2>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <check_pid+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    1228 <check_pid+0xd8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11de <check_pid+0x8e>
mov    %rax,%rbx
lea    0xe84(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    11de <check_pid+0x8e>
mov    0xc(%rsp),%ebx
test   %ebx,%ebx
je     11de <check_pid+0x8e>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %eax,%ebx
je     11de <check_pid+0x8e>
mov    0xc(%rsp),%edi
xor    %r14d,%r14d
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     11d9 <check_pid+0x89>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
je     11de <check_pid+0x8e>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @check_pid(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%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 %26, label %6
6: ; preds = %1
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = call i32 @fclose(i32* %4)
%9 = icmp ne i32 %7, 1
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %26, label %13
13: ; preds = %6
%14 = call i32 (...) @getpid()
%15 = icmp eq i32 %10, %14
br i1 %15, label %26, label %16
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i64 @kill(i32 %17, i32 0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @ESRCH
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20, %16
%25 = load i32, i32* %2
br label %26
26: ; preds = %24, %13, %6, %20, %1
%27 = phi i32 [ 0, %1 ], [ %25, %24 ], [ 0, %13 ], [ 0, %6 ], [ 0, %20 ]
ret i32 %27
}