#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*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int create_pid_file(char *path) {
  FILE *pid_file = NULL;
  int ret = -1;
  if (!path)
    goto out;
  pid_file = fopen(path, "w");
  if (!pid_file)
    goto out;
  if (fprintf(pid_file, "%d\n", getpid()) > 0)
    ret = 0;
out:
  if (pid_file && fclose(pid_file))
    return -1;
  return ret;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <create_pid_file+0x2b>
jmp    11c6 <create_pid_file+0x86>
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118f <create_pid_file+0x4f>
jmp    11c6 <create_pid_file+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jle    11c1 <create_pid_file+0x81>
movl   $0x0,-0x1c(%rbp)
jmp    11c6 <create_pid_file+0x86>
cmpq   $0x0,-0x18(%rbp)
je     11f0 <create_pid_file+0xb0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     11f0 <create_pid_file+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <create_pid_file+0xb6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     119f <create_pid_file+0x5f>
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119f <create_pid_file+0x5f>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
test   %rax,%rax
setle  %r15b
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    119f <create_pid_file+0x5f>
mov    %r15b,%bpl
neg    %ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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