#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned long);
scalar_t__ getpid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *,
                          int /*<<< orphan*/);
void write_pidfile(char const *fname) {
  FILE *fp;
  assert(fname != NULL);
  fp = fopen(fname, "w");
  if (fp) {
    fprintf(fp, "%lu\n", (unsigned long)getpid());
    fclose(fp);
  } else
    syslog(LOG_ERR, "fopen(%s,WRITE) = %s", fname, strerror(errno));
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dd <write_pidfile+0x6d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%rdi
mov    %rax,%rdx
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1214 <write_pidfile+0xa4>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
lea    0xdf8(%rip),%rsi
call   1070 <syslog@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
lea    0xe74(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c4 <write_pidfile+0x54>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe58(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe25(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <syslog@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%lu\0A\00"
@LOG_ERR = global i32 0
@.str.2 = [21 x i8] c"fopen(%s,WRITE) = %s\00"
@errno = global i32 0
define void @write_pidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = call i64 (...) @getpid()
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = load i32*, i32** %3
%17 = call i32 @fclose(i32* %16)
br label %24
18: ; preds = %1
%19 = load i32, i32* @LOG_ERR
%20 = load i8*, i8** %2
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %20, i32 %22)
br label %24
24: ; preds = %18, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%lu\0A\00"
@LOG_ERR = global i32 0
@.str.2 = [21 x i8] c"fopen(%s,WRITE) = %s\00"
@errno = global i32 0
define void @write_pidfile(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = tail call i64 (...) @getpid()
%9 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %8)
%10 = tail call i32 @fclose(i32* %5)
br label %16
11: ; preds = %1
%12 = load i32, i32* @LOG_ERR
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @syslog(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %14)
br label %16
16: ; preds = %11, %7
ret void
}