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

/* Variables and functions */
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void save_pid(pid_t pid, char *pid_file) {
  FILE *fp;
  if (!pid_file)
    return;
  if (!(fp = fopen(pid_file, "w"))) {
    fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file);
    return;
  }
  fprintf(fp, "%ld\n", (long)pid);
  if (fclose(fp) == -1) {
    fprintf(stderr, "Could not close the pid file %s.\n", pid_file);
    return;
  }
}


x86 assembly (clang, -O0):
<save_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1150 <save_pid+0x20>
jmp    11d3 <save_pid+0xa3>
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118e <save_pid+0x5e>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11d3 <save_pid+0xa3>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x8(%rbp),%rdx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0xffffffff,%eax
jne    11d3 <save_pid+0xa3>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pid>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1177 <save_pid+0x47>
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xeb9(%rip),%rsi
mov    %r15,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     117d <save_pid+0x4d>
mov    %rax,%rbx
lea    0xed0(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0xffffffff,%eax
je     1186 <save_pid+0x56>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe7e(%rip),%rsi
jmp    118d <save_pid+0x5d>
lea    0xea6(%rip),%rsi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rdx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Could not open the pid file %s for writing\0A\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [34 x i8] c"Could not close the pid file %s.\0A\00"
define void @save_pid(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
store i64 %0, i64* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
br label %29
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %29
17: ; preds = %9
%18 = load i32*, i32** %5
%19 = ptrtoint i32* %18 to i32
%20 = load i64, i64* %3
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %20)
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %4
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %8, %13, %25, %17
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Could not open the pid file %s for writing\0A\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [34 x i8] c"Could not close the pid file %s.\0A\00"
define void @save_pid(i64 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %17, label %4
4: ; preds = %2
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = ptrtoint i32* %5 to i64
%9 = trunc i64 %8 to i32
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %0)
%11 = tail call i32 @fclose(i32* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %7, %4
%14 = phi i8* [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), %7 ]
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* %14, i8* %1)
br label %17
17: ; preds = %13, %7, %2
ret void
}