#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__ EOF;
scalar_t__ ESRCH;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int chkpid(char *pidfile) {
  FILE *file;
  int pid;
  if (!(file = fopen(pidfile, "r")))
    return 0;
  if (fscanf(file, "%d", &pid) == EOF) {
    syslog(LOG_ERR, "Error, reading %s: %m", pidfile);
    fclose(file);
    return 0;
  }
  fclose(file);
  if (pid == getpid())
    return 0;
  if (kill(pid, 0) && errno == ESRCH)
    return 0;
  return pid;
}


x86 assembly (clang, -O0):
<chkpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1196 <chkpid+0x36>
movl   $0x0,-0x4(%rbp)
jmp    1257 <chkpid+0xf7>
mov    -0x18(%rbp),%rdi
lea    0xe61(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <chkpid+0x88>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1257 <chkpid+0xf7>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jne    1217 <chkpid+0xb7>
movl   $0x0,-0x4(%rbp)
jmp    1257 <chkpid+0xf7>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1251 <chkpid+0xf1>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <chkpid+0xf1>
movl   $0x0,-0x4(%rbp)
jmp    1257 <chkpid+0xf7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chkpid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1210 <chkpid+0xb0>
mov    %rax,%rbp
lea    0xe75(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <chkpid+0x68>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdx
call   1070 <syslog@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    1210 <chkpid+0xb0>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %eax,%ebx
je     1210 <chkpid+0xb0>
mov    0xc(%rsp),%edi
xor    %r14d,%r14d
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     120b <chkpid+0xab>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
je     1210 <chkpid+0xb0>
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"
@EOF = global i64 0
@LOG_ERR = global i32 0
@.str.2 = [22 x i8] c"Error, reading %s: %m\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @chkpid(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 ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %39
10: ; preds = %1
%11 = load i32*, i32** %4
%12 = call i64 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* @LOG_ERR
%17 = load i8*, i8** %3
%18 = call i32 @syslog(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
store i32 0, i32* %2
br label %39
21: ; preds = %10
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
%24 = load i32, i32* %5
%25 = call i32 (...) @getpid()
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 0, i32* %2
br label %39
28: ; preds = %21
%29 = load i32, i32* %5
%30 = call i64 @kill(i32 %29, i32 0)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @ESRCH
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 0, i32* %2
br label %39
37: ; preds = %32, %28
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %36, %27, %15, %9
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i64 0
@LOG_ERR = global i32 0
@.str.2 = [22 x i8] c"Error, reading %s: %m\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @chkpid(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 %29, label %6
6: ; preds = %1
%7 = call i64 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = load i64, i64* @EOF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @LOG_ERR
%12 = call i32 @syslog(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%13 = call i32 @fclose(i32* %4)
br label %29
14: ; preds = %6
%15 = call i32 @fclose(i32* %4)
%16 = load i32, i32* %2
%17 = call i32 (...) @getpid()
%18 = icmp eq i32 %16, %17
br i1 %18, label %29, label %19
19: ; preds = %14
%20 = load i32, i32* %2
%21 = call i64 @kill(i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @ESRCH
%26 = icmp eq i64 %24, %25
br i1 %26, label %29, label %27
27: ; preds = %23, %19
%28 = load i32, i32* %2
br label %29
29: ; preds = %23, %14, %1, %27, %10
%30 = phi i32 [ 0, %10 ], [ %28, %27 ], [ 0, %1 ], [ 0, %14 ], [ 0, %23 ]
ret i32 %30
}