#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*/ LOG_ERR;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ printlog(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
pid_t write_pid(char *pidfile_path) {
  int fd;
  FILE *file;
  pid_t pid;
  fd = open(pidfile_path, O_WRONLY | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    printlog(LOG_ERR, "Cannot open or create pidfile(%s): %s", pidfile_path,
             strerror(errno));
    goto fail;
  }
  if ((file = fdopen(fd, "w")) == NULL) {
    ;
    printlog(LOG_ERR, "Cannot open or create pidfile(%s): %s", pidfile_path,
             strerror(errno));
    goto fail;
  }
  pid = getpid();
  if (fprintf(file, "%d\n", pid) == 0) {
    printlog(LOG_ERR, "Cannot write pid: %s", strerror(errno));
    goto fail;
  }
  (void)fflush(file);
  (void)fclose(file);
  return (pid);
fail:
  (void)close(fd);
  (void)unlink(pidfile_path);
  return (-1);
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
mov    0x2dcb(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    123f <write_pid+0x9f>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   10b0 <printlog@plt>
jmp    1311 <write_pid+0x171>
mov    -0x14(%rbp),%edi
lea    0xddd(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    129a <write_pid+0xfa>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   10b0 <printlog@plt>
jmp    1311 <write_pid+0x171>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xd76(%rip),%rsi
call   1070 <fprintf@plt>
cmp    $0x0,%rax
jne    12f4 <write_pid+0x154>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%edi
movslq %eax,%rdx
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   10b0 <printlog@plt>
jmp    1311 <write_pid+0x171>
mov    -0x20(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1329 <write_pid+0x189>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2df2(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2de7(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
mov    %rdi,%r14
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     123e <write_pid+0x9e>
lea    0xe29(%rip),%rsi
mov    %r15d,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     123e <write_pid+0x9e>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <fprintf@plt>
test   %rax,%rax
je     126c <write_pid+0xcc>
mov    %rbx,%rdi
call   1080 <fflush@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    12ab <write_pid+0x10b>
mov    0x2d53(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda4(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10b0 <printlog@plt>
jmp    1296 <write_pid+0xf6>
mov    0x2d25(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
movslq %eax,%rdx
lea    0xd9f(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   10b0 <printlog@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOG_ERR = global i32 0
@.str = [38 x i8] c"Cannot open or create pidfile(%s): %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [21 x i8] c"Cannot write pid: %s\00"
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %7, i32 %12, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %1
%24 = load i32, i32* @LOG_ERR
%25 = load i8*, i8** %3
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i32, i8*, i8*, ...) @printlog(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %27)
br label %58
29: ; preds = %1
%30 = load i32, i32* %4
%31 = call i32* @fdopen(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %5
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* @LOG_ERR
%35 = load i8*, i8** %3
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 (i32, i8*, i8*, ...) @printlog(i32 %34, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %35, i32 %37)
br label %58
39: ; preds = %29
%40 = call i32 (...) @getpid()
store i32 %40, i32* %6
%41 = load i32*, i32** %5
%42 = load i32, i32* %6
%43 = call i64 @fprintf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %39
%46 = load i32, i32* @LOG_ERR
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = sext i32 %48 to i64
%50 = inttoptr i64 %49 to i8*
%51 = call i32 (i32, i8*, i8*, ...) @printlog(i32 %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %50)
br label %58
52: ; preds = %39
%53 = load i32*, i32** %5
%54 = call i32 @fflush(i32* %53)
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
%57 = load i32, i32* %6
store i32 %57, i32* %2
br label %63
58: ; preds = %45, %33, %23
%59 = load i32, i32* %4
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %3
%62 = call i32 @unlink(i8* %61)
store i32 -1, i32* %2
br label %63
63: ; preds = %58, %52
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOG_ERR = global i32 0
@.str = [38 x i8] c"Cannot open or create pidfile(%s): %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [21 x i8] c"Cannot write pid: %s\00"
define i32 @write_pid(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %6, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* @LOG_ERR
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 (i32, i8*, i8*, ...) @printlog(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %19)
br label %43
21: ; preds = %1
%22 = tail call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @LOG_ERR
%26 = load i32, i32* @errno
%27 = tail call i32 @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, i8*, ...) @printlog(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %27)
br label %43
29: ; preds = %21
%30 = tail call i32 (...) @getpid()
%31 = tail call i64 @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i32, i32* @LOG_ERR
%35 = load i32, i32* @errno
%36 = tail call i32 @strerror(i32 %35)
%37 = sext i32 %36 to i64
%38 = inttoptr i64 %37 to i8*
%39 = tail call i32 (i32, i8*, i8*, ...) @printlog(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %43
40: ; preds = %29
%41 = tail call i32 @fflush(i32* %22)
%42 = tail call i32 @fclose(i32* %22)
br label %46
43: ; preds = %33, %24, %16
%44 = tail call i32 @close(i32 %14)
%45 = tail call i32 @unlink(i8* %0)
br label %46
46: ; preds = %43, %40
%47 = phi i32 [ -1, %43 ], [ %30, %40 ]
ret i32 %47
}