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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
scalar_t__ getpid();
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ rename(char *, char const *);
scalar_t__ safe_strtoul(char *, unsigned int *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ vperror(char *, char *, ...);
void save_pid(const char *pid_file) {
  FILE *fp;
  if (access(pid_file, F_OK) == 0) {
    if ((fp = fopen(pid_file, "r")) != NULL) {
      char buffer[1024];
      if (fgets(buffer, sizeof(buffer), fp) != NULL) {
        unsigned int pid;
        if (safe_strtoul(buffer, &pid) && kill((pid_t)pid, 0) == 0) {
          fprintf(stderr,
                  "WARNING: The pid file contained the following (running) "
                  "pid: %u\n",
                  pid);
        }
      }
      fclose(fp);
    }
  }
  char tmp_pid_file[1024];
  snprintf(tmp_pid_file, sizeof(tmp_pid_file), "%s.tmp", pid_file);
  if ((fp = fopen(tmp_pid_file, "w")) == NULL) {
    vperror("Could not open the pid file %s for writing", tmp_pid_file);
    return;
  }
  fprintf(fp, "%ld\n", (long)getpid());
  if (fclose(fp) == -1) {
    vperror("Could not close the pid file %s", tmp_pid_file);
  }
  if (rename(tmp_pid_file, pid_file) != 0) {
    vperror("Could not rename the pid file from %s to %s", tmp_pid_file,
            pid_file);
  }
}


x86 assembly (clang, -O0):
<save_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%rax
jne    127d <save_pid+0xcd>
mov    -0x8(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1278 <save_pid+0xc8>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     126f <save_pid+0xbf>
lea    -0x410(%rbp),%rdi
lea    -0x414(%rbp),%rsi
call   1030 <safe_strtoul@plt>
cmp    $0x0,%rax
je     126a <save_pid+0xba>
mov    -0x414(%rbp),%edi
xor    %esi,%esi
call   10a0 <kill@plt>
cmp    $0x0,%rax
jne    126a <save_pid+0xba>
mov    0x2d8d(%rip),%rax
mov    (%rax),%rdi
mov    -0x414(%rbp),%eax
mov    %eax,%edx
lea    0xd9d(%rip),%rsi
call   1090 <fprintf@plt>
jmp    126f <save_pid+0xbf>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    127d <save_pid+0xcd>
lea    -0x820(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xdaf(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    12d4 <save_pid+0x124>
lea    -0x820(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1070 <vperror@plt>
jmp    1356 <save_pid+0x1a6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x828(%rbp),%rdi
mov    %rax,%rdx
lea    0xd80(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
jne    1323 <save_pid+0x173>
lea    -0x820(%rbp),%rsi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1070 <vperror@plt>
lea    -0x820(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10d0 <rename@plt>
cmp    $0x0,%rax
je     1356 <save_pid+0x1a6>
lea    -0x820(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1070 <vperror@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pid>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%r15
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
test   %rax,%rax
jne    1246 <save_pid+0x96>
lea    0xe27(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1246 <save_pid+0x96>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     123e <save_pid+0x8e>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1030 <safe_strtoul@plt>
test   %rax,%rax
je     123e <save_pid+0x8e>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   10a0 <kill@plt>
test   %rax,%rax
jne    123e <save_pid+0x8e>
mov    0x2db5(%rip),%rax
mov    (%rax),%rdi
mov    0xc(%rsp),%edx
lea    0xdc9(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xdf6(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
call   1060 <snprintf@plt>
lea    0xde1(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12dc <save_pid+0x12c>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
jne    12b2 <save_pid+0x102>
lea    0xdd6(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <vperror@plt>
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
call   10d0 <rename@plt>
test   %rax,%rax
je     12ef <save_pid+0x13f>
lea    0xdd1(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <vperror@plt>
jmp    12ef <save_pid+0x13f>
lea    0xd69(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <vperror@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@F_OK = global i32 0
@.str = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.1 = [65 x i8] c"WARNING: The pid file contained the following (running) pid: %u\0A\00"
@.str.2 = [7 x i8] c"%s.tmp\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [43 x i8] c"Could not open the pid file %s for writing\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
@.str.6 = [32 x i8] c"Could not close the pid file %s\00"
@.str.7 = [44 x i8] c"Could not rename the pid file from %s to %s\00"
define void @save_pid(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %3
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %37
15: ; preds = %11
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = load i32*, i32** %3
%18 = call i32* @fgets(i8* %16, i32 1024, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %34
20: ; preds = %15
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = call i64 @safe_strtoul(i8* %21, i32* %5)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i32, i32* %5
%26 = call i64 @kill(i32 %25, i32 0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32*, i32** @stderr
%30 = load i32, i32* %5
%31 = zext i32 %30 to i64
%32 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i64 %31)
br label %33
33: ; preds = %28, %24, %20
br label %34
34: ; preds = %33, %15
%35 = load i32*, i32** %3
%36 = call i32 @fclose(i32* %35)
br label %37
37: ; preds = %34, %11
br label %38
38: ; preds = %37, %1
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = load i8*, i8** %2
%41 = call i32 @snprintf(i8* %39, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %43, i32** %3
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %67
48: ; preds = %38
%49 = load i32*, i32** %3
%50 = call i64 (...) @getpid()
%51 = call i32 @fprintf(i32* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 %50)
%52 = load i32*, i32** %3
%53 = call i32 @fclose(i32* %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%57 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %55, %48
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%60 = load i8*, i8** %2
%61 = call i64 @rename(i8* %59, i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %58
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = load i8*, i8** %2
%66 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %64, i8* %65)
br label %67
67: ; preds = %45, %63, %58
ret void
}

LLVM IR (-O2):
@F_OK = global i32 0
@.str = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.1 = [65 x i8] c"WARNING: The pid file contained the following (running) pid: %u\0A\00"
@.str.2 = [7 x i8] c"%s.tmp\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [43 x i8] c"Could not open the pid file %s for writing\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
@.str.6 = [32 x i8] c"Could not close the pid file %s\00"
@.str.7 = [44 x i8] c"Could not rename the pid file from %s to %s\00"
define void @save_pid(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = load i32, i32* @F_OK
%6 = tail call i64 @access(i8* %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %1
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %31, label %11
11: ; preds = %8
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%13 = call i32* @fgets(i8* %12, i32 1024, i32* %9)
%14 = icmp eq i32* %13, null
br i1 %14, label %29, label %15
15: ; preds = %11
%16 = bitcast i32* %3 to i8*
%17 = call i64 @safe_strtoul(i8* %12, i32* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %15
%20 = load i32, i32* %3
%21 = call i64 @kill(i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32*, i32** @stderr
%25 = load i32, i32* %3
%26 = zext i32 %25 to i64
%27 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i64 %26)
br label %28
28: ; preds = %23, %19, %15
br label %29
29: ; preds = %28, %11
%30 = call i32 @fclose(i32* %9)
br label %31
31: ; preds = %8, %29, %1
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @snprintf(i8* %32, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%34 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %50
38: ; preds = %31
%39 = call i64 (...) @getpid()
%40 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 %39)
%41 = call i32 @fclose(i32* %34)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %32)
br label %45
45: ; preds = %43, %38
%46 = call i64 @rename(i8* %32, i8* %0)
%47 = icmp eq i64 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = call i32 (i8*, i8*, ...) @vperror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %32, i8* %0)
br label %50
50: ; preds = %45, %48, %36
ret void
}