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

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
scalar_t__ flock(int, int);
int /*<<< orphan*/ free(char *);
char *get_pidfile_path();
int getpid();
int /*<<< orphan*/ log_error_and_exit(char *, ...);
int open(char *, int, int);
int snprintf(char *, int, char *, int);
int write(int, char **, int);
void save_pid(void) {
  char pid[5 + 1];
  int length = snprintf(pid, sizeof(pid), "%d", getpid());
  pid[length] = '\n';
  char *pid_file = get_pidfile_path();
  int fd = open(pid_file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    log_error_and_exit("Cannot open PID file %s", pid_file);
  }
  free(pid_file);
  if (flock(fd, LOCK_EX | LOCK_NB)) {
    log_error_and_exit(
        "PID file locking is failed (another Procfiled instance is running?).");
  }
  if (write(fd, &pid, length + 1) == -1) {
    log_error_and_exit("Cannot write to PID file");
  }
}


x86 assembly (clang, -O0):
<save_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x6(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%ecx
mov    $0x6,%esi
lea    0xe57(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0xa,-0x6(%rbp,%rax,1)
mov    $0x0,%al
call   1090 <get_pidfile_path@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1211 <save_pid+0x91>
mov    -0x18(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error_and_exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
je     124c <save_pid+0xcc>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error_and_exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1275 <save_pid+0xf5>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error_and_exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe6a(%rip),%rdx
lea    0xa(%rsp),%rdi
mov    $0x6,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %eax,%r14d
cltq
movb   $0xa,0xa(%rsp,%rax,1)
xor    %eax,%eax
call   1090 <get_pidfile_path@plt>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df4(%rip),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fe <save_pid+0x7e>
lea    0xe0f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <log_error_and_exit@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2dd3(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
test   %rax,%rax
je     1232 <save_pid+0xb2>
lea    0xdf0(%rip),%rdi
xor    %eax,%eax
call   1060 <log_error_and_exit@plt>
add    $0x1,%r14d
lea    0xa(%rsp),%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1258 <save_pid+0xd8>
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1060 <log_error_and_exit@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Cannot open PID file %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [69 x i8] c"PID file locking is failed (another Procfiled instance is running?).\00"
@.str.3 = [25 x i8] c"Cannot write to PID file\00"
define void @save_pid() {
%1 = alloca [6 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%6 = call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %5, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 %9
store i8 10, i8* %10
%11 = call i8* (...) @get_pidfile_path()
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %12, i32 %15, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %0
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %0
%26 = load i8*, i8** %3
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %4
%29 = load i32, i32* @LOCK_EX
%30 = load i32, i32* @LOCK_NB
%31 = or i32 %29, %30
%32 = call i64 @flock(i32 %28, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %25
%37 = load i32, i32* %4
%38 = bitcast [6 x i8]* %1 to i8**
%39 = load i32, i32* %2
%40 = add nsw i32 %39, 1
%41 = call i32 @write(i32 %37, i8** %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Cannot open PID file %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [69 x i8] c"PID file locking is failed (another Procfiled instance is running?).\00"
@.str.3 = [25 x i8] c"Cannot write to PID file\00"
define void @save_pid() {
%1 = alloca [6 x i8]
%2 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 %5
store i8 10, i8* %6
%7 = call i8* (...) @get_pidfile_path()
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %7, i32 %10, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %18
18: ; preds = %16, %0
%19 = call i32 @free(i8* %7)
%20 = load i32, i32* @LOCK_EX
%21 = load i32, i32* @LOCK_NB
%22 = or i32 %21, %20
%23 = call i64 @flock(i32 %14, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = bitcast [6 x i8]* %1 to i8**
%29 = add nsw i32 %4, 1
%30 = call i32 @write(i32 %14, i8** %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
ret void
}