#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;

/* Variables and functions */
int /*<<< orphan*/ check_pid_file_exists(char const *, int *,
                                         int /*<<< orphan*/ *);
int /*<<< orphan*/ check_process_running(int /*<<< orphan*/, char const *,
                                         int *);
int /*<<< orphan*/ create_pid_file(char const *);
int handle_pid_file_checks(const char *pid_file, const char *program_name) {
  int exists;
  int running;
  pid_t existing_pid;
  if (!check_pid_file_exists(pid_file, &exists, &existing_pid))
    return 0;
  if (!exists) {
    if (!create_pid_file(pid_file))
      return 0;
  } else {
    if (!check_process_running(existing_pid, program_name, &running))
      return 0;
    if (running)
      return 0;
    if (!create_pid_file(pid_file))
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<handle_pid_file_checks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <check_pid_file_exists@plt>
cmp    $0x0,%eax
jne    1166 <handle_pid_file_checks+0x36>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_pid_file_checks+0xc8>
cmpl   $0x0,-0x1c(%rbp)
jne    1193 <handle_pid_file_checks+0x63>
mov    -0x10(%rbp),%rdi
call   1040 <create_pid_file@plt>
cmp    $0x0,%eax
jne    118e <handle_pid_file_checks+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_pid_file_checks+0xc8>
jmp    11f1 <handle_pid_file_checks+0xc1>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <check_process_running@plt>
cmp    $0x0,%eax
jne    11b8 <handle_pid_file_checks+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_pid_file_checks+0xc8>
cmpl   $0x0,-0x20(%rbp)
je     11ce <handle_pid_file_checks+0x9e>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_pid_file_checks+0xc8>
mov    -0x10(%rbp),%rdi
call   1040 <create_pid_file@plt>
cmp    $0x0,%eax
jne    11ec <handle_pid_file_checks+0xbc>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_pid_file_checks+0xc8>
jmp    11f1 <handle_pid_file_checks+0xc1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_pid_file_checks>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1030 <check_pid_file_exists@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     1189 <handle_pid_file_checks+0x59>
cmpl   $0x0,0xc(%rsp)
je     1177 <handle_pid_file_checks+0x47>
mov    0x4(%rsp),%edi
lea    0x8(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <check_process_running@plt>
test   %eax,%eax
je     1189 <handle_pid_file_checks+0x59>
cmpl   $0x0,0x8(%rsp)
jne    1189 <handle_pid_file_checks+0x59>
mov    %rbx,%rdi
call   1040 <create_pid_file@plt>
test   %eax,%eax
je     1189 <handle_pid_file_checks+0x59>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @handle_pid_file_checks(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @check_pid_file_exists(i8* %9, i32* %6, i32* %8)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 0, i32* %3
br label %39
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32 @create_pid_file(i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i32 0, i32* %3
br label %39
21: ; preds = %16
br label %38
22: ; preds = %13
%23 = load i32, i32* %8
%24 = load i8*, i8** %5
%25 = call i32 @check_process_running(i32 %23, i8* %24, i32* %7)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %22
store i32 0, i32* %3
br label %39
28: ; preds = %22
%29 = load i32, i32* %7
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 0, i32* %3
br label %39
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = call i32 @create_pid_file(i8* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
store i32 0, i32* %3
br label %39
37: ; preds = %32
br label %38
38: ; preds = %37, %21
store i32 1, i32* %3
br label %39
39: ; preds = %38, %36, %31, %27, %20, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @handle_pid_file_checks(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @check_pid_file_exists(i8* %0, i32* %3, i32* %5)
%10 = icmp eq i32 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %2
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @create_pid_file(i8* %0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %28, label %27
17: ; preds = %11
%18 = load i32, i32* %5
%19 = call i32 @check_process_running(i32 %18, i8* %1, i32* %4)
%20 = icmp ne i32 %19, 0
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 0
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = call i32 @create_pid_file(i8* %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %24, %14
br label %28
28: ; preds = %24, %17, %14, %2, %27
%29 = phi i32 [ 1, %27 ], [ 0, %2 ], [ 0, %14 ], [ 0, %17 ], [ 0, %24 ]
ret i32 %29
}