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

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ LOG_ERR;
int PATH_MAX;
int /*<<< orphan*/ create_dirs(char *);
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
scalar_t__ get_dir_name(char const *, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ log_msg(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int umask(int);
int /*<<< orphan*/ unlink(char const *);
int write_pidfile(const char *pidfile) {
  mode_t mask;
  char dirname[PATH_MAX];
  FILE *fp;
  if (pidfile == NULL) {
    return (0);
  }
  if (pidfile[0] != '/') {
    log_msg(0, "Unable to create relative-path pidfile \"%s\"", pidfile);
    return (-1);
  }
  (void)unlink(pidfile);
  mask = umask(0);
  umask(mask | 022);
  if (get_dir_name(pidfile, dirname, sizeof(dirname))) {
    (void)create_dirs(dirname);
  }
  fp = fopen(pidfile, "w");
  umask(mask);
  if (!fp) {
    log_msg(LOG_ERR, "Unable to open pidfile \"%s\": %s", pidfile,
            strerror(errno));
  } else if (fprintf(fp, "%d\n", (int)getpid()) == EOF) {
    log_msg(LOG_ERR, "Unable to write to pidfile \"%s\": %s", pidfile,
            strerror(errno));
    (void)fclose(fp);
  } else if (fclose(fp) == EOF) {
    log_msg(LOG_ERR, "Unable to close pidfile \"%s\": %s", pidfile,
            strerror(errno));
  } else {
    return (0);
  }
  (void)unlink(pidfile);
  return (-1);
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fd <write_pidfile+0x5d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    13e0 <write_pidfile+0x240>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1234 <write_pidfile+0x94>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1090 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    13e0 <write_pidfile+0x240>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
or     $0x12,%edi
call   1080 <umask@plt>
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   10a0 <get_dir_name@plt>
cmp    $0x0,%rax
je     1278 <write_pidfile+0xd8>
mov    -0x40(%rbp),%rdi
call   1060 <create_dirs@plt>
mov    -0x10(%rbp),%rdi
lea    0xda9(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <umask@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12dd <write_pidfile+0x13d>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1090 <log_msg@plt>
jmp    13c9 <write_pidfile+0x229>
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xd55(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cdb(%rip),%rcx
cmp    (%rcx),%rax
jne    1355 <write_pidfile+0x1b5>
mov    0x2cab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %eax,%ecx
lea    0xd12(%rip),%rsi
mov    $0x0,%al
call   1090 <log_msg@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
jmp    13c4 <write_pidfile+0x224>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2c7b(%rip),%rcx
cmp    (%rcx),%rax
jne    13ac <write_pidfile+0x20c>
mov    0x2c4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %eax,%ecx
lea    0xcd6(%rip),%rsi
mov    $0x0,%al
call   1090 <log_msg@plt>
jmp    13bf <write_pidfile+0x21f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    13e0 <write_pidfile+0x240>
jmp    13c4 <write_pidfile+0x224>
jmp    13c9 <write_pidfile+0x229>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e3e(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     128e <write_pidfile+0xee>
mov    %rdi,%r15
cmpb   $0x2f,(%rdi)
jne    1292 <write_pidfile+0xf2>
mov    %r15,%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,%r12d
mov    %eax,%edi
or     $0x12,%edi
call   1080 <umask@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   10a0 <get_dir_name@plt>
test   %rax,%rax
je     1212 <write_pidfile+0x72>
mov    %rbx,%rdi
call   1060 <create_dirs@plt>
lea    0xe13(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    %r12d,%edi
call   1080 <umask@plt>
test   %r14,%r14
je     12a7 <write_pidfile+0x107>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0d(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    0x2d8e(%rip),%r12
cmp    (%r12),%rax
jne    12c7 <write_pidfile+0x127>
mov    0x2d61(%rip),%rax
mov    (%rax),%ebx
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xddc(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <log_msg@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1301 <write_pidfile+0x161>
xor    %ebx,%ebx
jmp    130e <write_pidfile+0x16e>
lea    0xd67(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <log_msg@plt>
jmp    1309 <write_pidfile+0x169>
mov    0x2d12(%rip),%rax
mov    (%rax),%ebx
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd69(%rip),%rsi
jmp    12f3 <write_pidfile+0x153>
mov    %r14,%rdi
call   1050 <fclose@plt>
cmp    (%r12),%rax
jne    130e <write_pidfile+0x16e>
mov    0x2ce4(%rip),%rax
mov    (%rax),%ebx
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd83(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <log_msg@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [44 x i8] c"Unable to create relative-path pidfile \22%s\22\00"
@.str.1 = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.2 = [32 x i8] c"Unable to open pidfile \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.4 = [36 x i8] c"Unable to write to pidfile \22%s\22: %s\00"
@.str.5 = [33 x i8] c"Unable to close pidfile \22%s\22: %s\00"
define i32 @write_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %8
br label %83
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 47
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %83
25: ; preds = %16
%26 = load i8*, i8** %3
%27 = call i32 @unlink(i8* %26)
%28 = call i32 @umask(i32 0)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = or i32 %29, 18
%31 = call i32 @umask(i32 %30)
%32 = load i8*, i8** %3
%33 = trunc i64 %10 to i32
%34 = call i64 @get_dir_name(i8* %32, i8* %12, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @create_dirs(i8* %12)
br label %38
38: ; preds = %36, %25
%39 = load i8*, i8** %3
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %7
%41 = load i32, i32* %4
%42 = call i32 @umask(i32 %41)
%43 = load i32*, i32** %7
%44 = icmp ne i32* %43, null
br i1 %44, label %51, label %45
45: ; preds = %38
%46 = load i32, i32* @LOG_ERR
%47 = load i8*, i8** %3
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %46, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %49)
br label %80
51: ; preds = %38
%52 = load i32*, i32** %7
%53 = call i64 (...) @getpid()
%54 = trunc i64 %53 to i32
%55 = call i64 @fprintf(i32* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i64, i64* @EOF
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %66
58: ; preds = %51
%59 = load i32, i32* @LOG_ERR
%60 = load i8*, i8** %3
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %59, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %60, i32 %62)
%64 = load i32*, i32** %7
%65 = call i64 @fclose(i32* %64)
br label %79
66: ; preds = %51
%67 = load i32*, i32** %7
%68 = call i64 @fclose(i32* %67)
%69 = load i64, i64* @EOF
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %77
71: ; preds = %66
%72 = load i32, i32* @LOG_ERR
%73 = load i8*, i8** %3
%74 = load i32, i32* @errno
%75 = call i32 @strerror(i32 %74)
%76 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %72, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %73, i32 %75)
br label %78
77: ; preds = %66
store i32 0, i32* %2
store i32 1, i32* %8
br label %83
78: ; preds = %71
br label %79
79: ; preds = %78, %58
br label %80
80: ; preds = %79, %45
%81 = load i8*, i8** %3
%82 = call i32 @unlink(i8* %81)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %83
83: ; preds = %80, %77, %22, %15
%84 = load i8*, i8** %5
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [44 x i8] c"Unable to create relative-path pidfile \22%s\22\00"
@.str.1 = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.2 = [32 x i8] c"Unable to open pidfile \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.4 = [36 x i8] c"Unable to write to pidfile \22%s\22: %s\00"
@.str.5 = [33 x i8] c"Unable to close pidfile \22%s\22: %s\00"
define i32 @write_pidfile(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq i8* %0, null
br i1 %5, label %52, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 47
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i32, i8*, i8*, ...) @log_msg(i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
11: ; preds = %6
%12 = tail call i32 @unlink(i8* %0)
%13 = tail call i32 @umask(i32 0)
%14 = or i32 %13, 18
%15 = tail call i32 @umask(i32 %14)
%16 = call i64 @get_dir_name(i8* %0, i8* %4, i32 %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = call i32 @create_dirs(i8* %4)
br label %20
20: ; preds = %18, %11
%21 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @umask(i32 %13)
%23 = icmp eq i32* %21, null
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @LOG_ERR
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %27)
br label %50
29: ; preds = %20
%30 = call i64 (...) @getpid()
%31 = trunc i64 %30 to i32
%32 = call i64 @fprintf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i64, i64* @EOF
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i32, i32* @LOG_ERR
%37 = load i32, i32* @errno
%38 = call i32 @strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %38)
%40 = call i64 @fclose(i32* %21)
br label %50
41: ; preds = %29
%42 = call i64 @fclose(i32* %21)
%43 = load i64, i64* @EOF
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i32, i32* @LOG_ERR
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %48)
br label %50
50: ; preds = %35, %45, %24
%51 = call i32 @unlink(i8* %0)
br label %52
52: ; preds = %41, %1, %50, %9
%53 = phi i32 [ -1, %9 ], [ -1, %50 ], [ 0, %1 ], [ 0, %41 ]
ret i32 %53
}