#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 /*<<< orphan*/ LOG_ERR;
char *build_file_name(char *, char const *, char const *);
int /*<<< orphan*/ cleanup_file_name(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int create_pid_file(const char *pid_file_basepath, const char *identify_name) {
  char *pid_file =
      build_file_name("%s/%s.pid", pid_file_basepath, identify_name);
  FILE *file_handle;
  if ((file_handle = fopen(pid_file, "w")) == NULL) {
    syslog(LOG_ERR, "failed to create pid file %s, code=%d (%s)", pid_file,
           errno, strerror(errno));
    cleanup_file_name(pid_file);
    return 0;
  }
  pid_t pid = getpid();
  fprintf(file_handle, "%i\n", (int)pid);
  fclose(file_handle);
  cleanup_file_name(pid_file);
  return 1;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe61(%rip),%rdi
call   1040 <build_file_name@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1222 <create_pid_file+0xa2>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
lea    0xe04(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x0,-0x4(%rbp)
jmp    125c <create_pid_file+0xdc>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rdx
mov    %rdi,%rsi
lea    0xe6f(%rip),%rdi
call   1040 <build_file_name@plt>
mov    %rax,%r14
lea    0xe6a(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11d7 <create_pid_file+0x57>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    $0x1,%ebx
jmp    1208 <create_pid_file+0x88>
mov    0x2df2(%rip),%rax
mov    (%rax),%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xe15(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1080 <syslog@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <cleanup_file_name@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%s/%s.pid\00"
@.str.1 = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.2 = [43 x i8] c"failed to create pid file %s, code=%d (%s)\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%i\0A\00"
define i32 @create_pid_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call i8* @build_file_name(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i32, i32* @LOG_ERR
%17 = load i8*, i8** %6
%18 = load i32, i32* @errno
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @syslog(i32 %16, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 %18, i32 %20)
%22 = load i8*, i8** %6
%23 = call i32 @cleanup_file_name(i8* %22)
store i32 0, i32* %3
br label %34
24: ; preds = %2
%25 = call i64 (...) @getpid()
store i64 %25, i64* %8
%26 = load i32*, i32** %7
%27 = load i64, i64* %8
%28 = trunc i64 %27 to i32
%29 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %6
%33 = call i32 @cleanup_file_name(i8* %32)
store i32 1, i32* %3
br label %34
34: ; preds = %24, %15
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s/%s.pid\00"
@.str.1 = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.2 = [43 x i8] c"failed to create pid file %s, code=%d (%s)\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%i\0A\00"
define i32 @create_pid_file(i8* %0, i8* %1) {
%3 = tail call i8* @build_file_name(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @LOG_ERR
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @syslog(i32 %7, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %3, i32 %8, i32 %9)
br label %16
11: ; preds = %2
%12 = tail call i64 (...) @getpid()
%13 = trunc i64 %12 to i32
%14 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %11, %6
%17 = phi i32 [ 0, %6 ], [ 1, %11 ]
%18 = tail call i32 @cleanup_file_name(i8* %3)
ret i32 %17
}