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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int save_pid_to_file(char *pid_file, pid_t pid) {
  if (pid_file == NULL) {
    return -1;
  }
  FILE *file = fopen(pid_file, "w");
  if (file == NULL) {
    return -2;
  }
  fprintf(file, "%u\n", pid);
  fflush(file);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<save_pid_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1166 <save_pid_to_file+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <save_pid_to_file+0x7d>
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <save_pid_to_file+0x51>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11bd <save_pid_to_file+0x7d>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe63(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pid_to_file>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1183 <save_pid_to_file+0x43>
mov    %esi,%ebp
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118a <save_pid_to_file+0x4a>
mov    %rax,%rbx
lea    0xe9d(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fflush@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    118f <save_pid_to_file+0x4f>
mov    $0xffffffff,%eax
jmp    118f <save_pid_to_file+0x4f>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%u\0A\00"
define i32 @save_pid_to_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 -1, i32* %3
br label %24
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 -2, i32* %3
br label %24
16: ; preds = %10
%17 = load i32*, i32** %6
%18 = load i32, i32* %5
%19 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @fflush(i32* %20)
%22 = load i32*, i32** %6
%23 = call i32 @fclose(i32* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %16, %15, %9
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%u\0A\00"
define i32 @save_pid_to_file(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%9 = tail call i32 @fflush(i32* %5)
%10 = tail call i32 @fclose(i32* %5)
br label %11
11: ; preds = %7, %4, %2
%12 = phi i32 [ -1, %2 ], [ 0, %7 ], [ -2, %4 ]
ret i32 %12
}