#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 */
struct stat {
  int dummy;
};
typedef size_t ssize_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
char *config_item(char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int open(char const *, int, ...);
size_t read(int, char *, int);
size_t snprintf(char *, int, char *, int);
int /*<<< orphan*/ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
scalar_t__ write(int, char *, size_t);
int check_pid_file(void) {
  const char *pid_file = config_item("global", "pidfile");
  struct stat info;
  char pid_buffer[32];
  int pid, pid_fd;
  ssize_t pid_length;
  if (!pid_file)
    return 0;
  if (!stat(pid_file, &info)) {
    pid_fd = open(pid_file, O_RDONLY);
    if (pid_fd) {
      pid_length = read(pid_fd, pid_buffer, 32 - 1);
      close(pid_fd);
      if (pid_length >= 0) {
        pid_buffer[pid_length] = 0;
        pid = atoi(pid_buffer);
        if (!kill(pid, 0))
          return pid;
      }
    }
    unlink(pid_file);
  }
  pid_fd = open(pid_file, O_CREAT | O_TRUNC | O_WRONLY, 0644);
  pid_length = snprintf(pid_buffer, 32, "%d\n", getpid());
  if (pid_fd >= 0) {
    if (pid_length >= 0)
      if (write(pid_fd, pid_buffer, (size_t)pid_length) < 0)
        fprintf(stderr, "Failed to write pid file %s: %s\n", pid_file,
                strerror(errno));
    close(pid_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe21(%rip),%rdi
lea    0xe21(%rip),%rsi
call   10c0 <config_item@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1206 <check_pid_file+0x36>
movl   $0x0,-0x4(%rbp)
jmp    137e <check_pid_file+0x1ae>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%eax
jne    12ab <check_pid_file+0xdb>
mov    -0x10(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10d0 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     12a2 <check_pid_file+0xd2>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1080 <read@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
cmpq   $0x0,-0x50(%rbp)
jb     129d <check_pid_file+0xcd>
mov    -0x50(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   10b0 <kill@plt>
cmp    $0x0,%eax
jne    1298 <check_pid_file+0xc8>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137e <check_pid_file+0x1ae>
jmp    129d <check_pid_file+0xcd>
jmp    12a2 <check_pid_file+0xd2>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    0x2d31(%rip),%rax
or     (%rax),%esi
mov    0x2ce8(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   10d0 <open@plt>
mov    %eax,-0x48(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xd15(%rip),%rdx
call   1060 <snprintf@plt>
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x48(%rbp)
jl     1377 <check_pid_file+0x1a7>
cmpq   $0x0,-0x50(%rbp)
jb     136f <check_pid_file+0x19f>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    136a <check_pid_file+0x19a>
mov    0x2c8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xcae(%rip),%rsi
call   1090 <fprintf@plt>
jmp    136f <check_pid_file+0x19f>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xe1f(%rip),%rdi
lea    0xe1f(%rip),%rsi
call   10c0 <config_item@plt>
test   %rax,%rax
je     1307 <check_pid_file+0x137>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   10a0 <stat@plt>
test   %eax,%eax
jne    126b <check_pid_file+0x9b>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10d0 <open@plt>
test   %eax,%eax
je     1263 <check_pid_file+0x93>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1f,%edx
call   1080 <read@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1070 <close@plt>
movb   $0x0,0x10(%rsp,%r15,1)
mov    %r14,%rdi
call   10e0 <atoi@plt>
mov    %eax,%r14d
mov    %eax,%edi
xor    %esi,%esi
call   10b0 <kill@plt>
test   %eax,%eax
je     130a <check_pid_file+0x13a>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2d86(%rip),%rax
mov    0x2d77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d2c(%rip),%rax
or     (%rax),%esi
xor    %r14d,%r14d
mov    %rbx,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10d0 <open@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd67(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x20,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
test   %ebp,%ebp
js     130a <check_pid_file+0x13a>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1040 <write@plt>
test   %rax,%rax
jns    12fe <check_pid_file+0x12e>
mov    0x2cf0(%rip),%rax
mov    (%rax),%r15d
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd23(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    130a <check_pid_file+0x13a>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"global\00"
@.str.1 = [8 x i8] c"pidfile\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Failed to write pid file %s: %s\0A\00"
@errno = global i32 0
define i32 @check_pid_file() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = call i8* @config_item(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %0
store i32 0, i32* %1
br label %78
12: ; preds = %0
%13 = load i8*, i8** %2
%14 = call i32 @stat(i8* %13, %struct.stat* %3)
%15 = icmp ne i32 %14, 0
br i1 %15, label %45, label %16
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = load i32, i32* @O_RDONLY
%19 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %42
22: ; preds = %16
%23 = load i32, i32* %6
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%25 = call i64 @read(i32 %23, i8* %24, i32 31)
store i64 %25, i64* %7
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i64, i64* %7
%29 = icmp uge i64 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %22
%31 = load i64, i64* %7
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %31
store i8 0, i8* %32
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = call i32 @kill(i32 %35, i32 0)
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %30
%39 = load i32, i32* %5
store i32 %39, i32* %1
br label %78
40: ; preds = %30
br label %41
41: ; preds = %40, %22
br label %42
42: ; preds = %41, %16
%43 = load i8*, i8** %2
%44 = call i32 @unlink(i8* %43)
br label %45
45: ; preds = %42, %12
%46 = load i8*, i8** %2
%47 = load i32, i32* @O_CREAT
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = load i32, i32* @O_WRONLY
%51 = or i32 %49, %50
%52 = call i32 (i8*, i32, ...) @open(i8* %46, i32 %51, i32 420)
store i32 %52, i32* %6
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%54 = call i32 (...) @getpid()
%55 = call i64 @snprintf(i8* %53, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %54)
store i64 %55, i64* %7
%56 = load i32, i32* %6
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %77
58: ; preds = %45
%59 = load i64, i64* %7
%60 = icmp uge i64 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %58
%62 = load i32, i32* %6
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%64 = load i64, i64* %7
%65 = call i64 @write(i32 %62, i8* %63, i64 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %2
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %69, i8* %71)
br label %73
73: ; preds = %67, %61
br label %74
74: ; preds = %73, %58
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74, %45
store i32 0, i32* %1
br label %78
78: ; preds = %77, %38, %11
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"global\00"
@.str.1 = [8 x i8] c"pidfile\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Failed to write pid file %s: %s\0A\00"
@errno = global i32 0
define i32 @check_pid_file() {
%1 = alloca %struct.stat
%2 = alloca [32 x i8]
%3 = tail call i8* @config_item(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast %struct.stat* %1 to i8*
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = icmp eq i8* %3, null
br i1 %6, label %43, label %7
7: ; preds = %0
%8 = call i32 @stat(i8* %3, %struct.stat* %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %3, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %10
%15 = call i64 @read(i32 %12, i8* %5, i32 31)
%16 = tail call i32 @close(i32 %12)
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %17
%18 = call i32 @atoi(i8* %5)
%19 = tail call i32 @kill(i32 %18, i32 0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %43, label %21
21: ; preds = %14, %10
%22 = tail call i32 @unlink(i8* %3)
br label %23
23: ; preds = %21, %7
%24 = load i32, i32* @O_CREAT
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %25, %24
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %26, %27
%29 = tail call i32 (i8*, i32, ...) @open(i8* %3, i32 %28, i32 420)
%30 = tail call i32 (...) @getpid()
%31 = call i64 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = icmp sgt i32 %29, -1
br i1 %32, label %33, label %43
33: ; preds = %23
%34 = call i64 @write(i32 %29, i8* %5, i64 %31)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* %39)
br label %41
41: ; preds = %33, %36
%42 = call i32 @close(i32 %29)
br label %43
43: ; preds = %23, %41, %14, %0
%44 = phi i32 [ 0, %0 ], [ %18, %14 ], [ 0, %41 ], [ 0, %23 ]
ret i32 %44
}