#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 ssize_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ errno;
int getpid();
int /*<<< orphan*/ logging(char *, char const *const, ...);
int open(char const *const, int, int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int const, char *, int const);
void write_pid_file(char const *const pid_file) {
  logging("Writing pid file [%s]: [%d]", pid_file, getpid());
  char pbuf[20];
  int const plen = snprintf(pbuf, 20, "%d\n", getpid());
  int const fd =
      open(pid_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    logging("Cannot open pid file, reason [%s]", strerror(errno));
    close(fd);
    return;
  }
  ssize_t const written = write(fd, pbuf, plen);
  if (written != plen) {
    logging("Write error [%s]", strerror(errno));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<write_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1070 <logging@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%ecx
mov    $0x14,%esi
lea    0xe5c(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2e0b(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1240 <write_pid_file+0xd0>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1070 <logging@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
jmp    1285 <write_pid_file+0x115>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     127d <write_pid_file+0x10d>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1070 <logging@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid_file>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <logging@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7b(%rip),%rdx
mov    %rsp,%rdi
mov    $0x14,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    0x2df1(%rip),%rcx
mov    0x2e3a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e27(%rip),%rcx
or     (%rcx),%esi
mov    0x2de6(%rip),%rcx
mov    0x2def(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%ebp
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1210 <write_pid_file+0xa0>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1237 <write_pid_file+0xc7>
lea    0xe34(%rip),%rbp
jmp    121c <write_pid_file+0xac>
mov    $0xffffffff,%ebx
lea    0xe04(%rip),%rbp
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <logging@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Writing pid file [%s]: [%d]\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [34 x i8] c"Cannot open pid file, reason [%s]\00"
@errno = global i32 0
@.str.3 = [17 x i8] c"Write error [%s]\00"
define void @write_pid_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, i8*, ...) @logging(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %8)
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %10, i32 20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i8*, i8** %2
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %13, i32 %18, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %35
27: ; preds = %1
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = call i32 (i8*, i8*, ...) @logging(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
br label %52
35: ; preds = %1
%36 = load i32, i32* %5
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = load i32, i32* %4
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %35
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = sext i32 %45 to i64
%47 = inttoptr i64 %46 to i8*
%48 = call i32 (i8*, i8*, ...) @logging(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %43, %35
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %27
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"Writing pid file [%s]: [%d]\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [34 x i8] c"Cannot open pid file, reason [%s]\00"
@errno = global i32 0
@.str.3 = [17 x i8] c"Write error [%s]\00"
define void @write_pid_file(i8* %0) {
%2 = alloca [20 x i8]
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, i8*, ...) @logging(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %3)
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %5, i32 20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %0, i32 %12, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = call i32 @write(i32 %18, i8* %5, i32 %7)
%22 = icmp eq i32 %21, %7
br i1 %22, label %31, label %23
23: ; preds = %20, %1
%24 = phi i8* [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), %1 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %20 ]
%25 = phi i32 [ -1, %1 ], [ %18, %20 ]
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = sext i32 %27 to i64
%29 = inttoptr i64 %28 to i8*
%30 = call i32 (i8*, i8*, ...) @logging(i8* %24, i8* %29)
br label %31
31: ; preds = %23, %20
%32 = phi i32 [ %18, %20 ], [ %25, %23 ]
%33 = call i32 @close(i32 %32)
ret void
}