#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ printf(char *, char const *);
int create_pid_file(const char *pid_file) {
  pid_t cur_pid;
  FILE *fp;
  cur_pid = getpid();
  if ((fp = fopen(pid_file, "w")) == NULL) {
    printf("Failed to open '%s' for writing\n", pid_file);
    return 0;
  }
  fprintf(fp, "%d\n", cur_pid);
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a0 <create_pid_file+0x50>
mov    -0x10(%rbp),%rsi
lea    0xe73(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <create_pid_file+0x73>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe75(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
lea    0xe92(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a0 <create_pid_file+0x50>
mov    %rax,%rbp
lea    0xe9e(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    $0x1,%r15d
jmp    11af <create_pid_file+0x5f>
lea    0xe5b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r15d,%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 = [33 x i8] c"Failed to open '%s' for writing\0A\00"
@.str.2 = [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
%6 = call i32 (...) @getpid()
store i32 %6, i32* %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 0, i32* %2
br label %19
13: ; preds = %1
%14 = load i32*, i32** %5
%15 = load i32, i32* %4
%16 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %5
%18 = call i32 @fclose(i32* %17)
store i32 1, i32* %2
br label %19
19: ; preds = %13, %10
%20 = load i32, i32* %2
ret i32 %20
}

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