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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int check_pid_file_exists(const char *pid_file, int *exists,
                          pid_t *existing_pid) {
  FILE *fp;
  *exists = 0;
  if (access(pid_file, F_OK) == 0)
    *exists = 1;
  if (*exists) {
    if ((fp = fopen(pid_file, "r")) == NULL) {
      printf("Failed to open '%s' for reading\n", pid_file);
      return 0;
    }
    if (fscanf(fp, "%d", existing_pid) != 1) {
      printf("Failed to retrieve existing PID\n");
      return 0;
    }
    fclose(fp);
  }
  return 1;
}


x86 assembly (clang, -O0):
<check_pid_file_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    1194 <check_pid_file_exists+0x44>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     121d <check_pid_file_exists+0xcd>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11dd <check_pid_file_exists+0x8d>
mov    -0x10(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <check_pid_file_exists+0xd4>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1214 <check_pid_file_exists+0xc4>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <check_pid_file_exists+0xd4>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid_file_exists>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0x0,(%rsi)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
test   %rax,%rax
je     118e <check_pid_file_exists+0x2e>
cmpl   $0x0,(%rbx)
jne    1194 <check_pid_file_exists+0x34>
jmp    11ca <check_pid_file_exists+0x6a>
movl   $0x1,(%rbx)
lea    0xe65(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d1 <check_pid_file_exists+0x71>
mov    %rax,%rbx
lea    0xe71(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    11e6 <check_pid_file_exists+0x86>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%ebx
jmp    11f4 <check_pid_file_exists+0x94>
lea    0xe2a(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f4 <check_pid_file_exists+0x94>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@F_OK = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"Failed to open '%s' for reading\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@str = [32 x i8] c"Failed to retrieve existing PID\00"
define i32 @check_pid_file_exists(i8* %0, i32* nocapture %1, i32* %2) {
store i32 0, i32* %1
%4 = load i32, i32* @F_OK
%5 = tail call i64 @access(i8* %0, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %3
store i32 1, i32* %1
br label %11
8: ; preds = %3
%9 = load i32, i32* %1
%10 = icmp eq i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %7, %8
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
16: ; preds = %11
%17 = tail call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %23
21: ; preds = %16
%22 = tail call i32 @fclose(i32* %12)
br label %23
23: ; preds = %8, %21, %19, %14
%24 = phi i32 [ 0, %14 ], [ 0, %19 ], [ 1, %21 ], [ 1, %8 ]
ret i32 %24
}