#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_CRIT;
int asprintf(char **, char *, long long);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ getpid();
int strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
void write_pidfile(const char *pidfile) {
  pid_t pid = getpid();
  char *pid_s;
  FILE *file;
  int len;
  if (asprintf(&pid_s, "%lld", (long long)pid) == -1)
    fatal("Failed to allocate pidfile string");
  len = strlen(pid_s);
  file = fopen(pidfile, "w");
  if (file == NULL) {
    syslog(LOG_CRIT, "Failed to open pidfile %s", pidfile);
    exit(1);
  }
  if (fwrite(pid_s, 1, len, file) != len)
    fatal("Failed to write pid to pidfile");
  fclose(file);
  free(pid_s);
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11e0 <write_pidfile+0x40>
lea    0xe2a(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122e <write_pidfile+0x8e>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe0a(%rip),%rsi
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
cmp    -0x24(%rbp),%eax
je     1258 <write_pidfile+0xb8>
lea    0xdf0(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe47(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
call   1080 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11d7 <write_pidfile+0x37>
lea    0xe33(%rip),%rdi
call   10c0 <fatal@plt>
mov    0x8(%rsp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0xe3d(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1239 <write_pidfile+0x99>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
cmp    %ebp,%eax
je     121e <write_pidfile+0x7e>
lea    0xe2a(%rip),%rdi
call   10c0 <fatal@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
mov    %r14,%rdx
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [34 x i8] c"Failed to allocate pidfile string\00"
@.str.2 = [2 x i8] c"w\00"
@LOG_CRIT = global i32 0
@.str.3 = [26 x i8] c"Failed to open pidfile %s\00"
@.str.4 = [31 x i8] c"Failed to write pid to pidfile\00"
define void @write_pidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i64 (...) @getpid()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %6
%16 = load i8*, i8** %2
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @LOG_CRIT
%22 = load i8*, i8** %2
%23 = call i32 @syslog(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = load i8*, i8** %4
%27 = load i32, i32* %6
%28 = load i32*, i32** %5
%29 = call i32 @fwrite(i8* %26, i32 1, i32 %27, i32* %28)
%30 = load i32, i32* %6
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [34 x i8] c"Failed to allocate pidfile string\00"
@.str.2 = [2 x i8] c"w\00"
@LOG_CRIT = global i32 0
@.str.3 = [26 x i8] c"Failed to open pidfile %s\00"
@.str.4 = [31 x i8] c"Failed to write pid to pidfile\00"
define void @write_pidfile(i8* %0) {
%2 = alloca i8*
%3 = tail call i64 (...) @getpid()
%4 = bitcast i8** %2 to i8*
%5 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @LOG_CRIT
%16 = call i32 @syslog(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = load i8*, i8** %2
%20 = call i32 @fwrite(i8* %19, i32 1, i32 %11, i32* %12)
%21 = icmp eq i32 %20, %11
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 @fclose(i32* %12)
%26 = load i8*, i8** %2
%27 = call i32 @free(i8* %26)
ret void
}