#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*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
void writepidfile(char *pidfile) {
  FILE *pidstream;
  if (!pidfile)
    return;
  if ((pidstream = fopen(pidfile, "w"))) {
    fprintf(pidstream, "%d\n", getpid());
    fclose(pidstream);
  } else
    error(1, errno, "fopen %s", pidfile);
}


x86 assembly (clang, -O0):
<writepidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116c <writepidfile+0x1c>
jmp    11d7 <writepidfile+0x87>
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11b9 <writepidfile+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%edx
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11d7 <writepidfile+0x87>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe34(%rip),%rdx
call   1060 <error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepidfile>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1197 <writepidfile+0x47>
mov    %rdi,%rbx
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     119f <writepidfile+0x4f>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <error@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"fopen %s\00"
define void @writepidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %7, label %6
6: ; preds = %1
br label %21
7: ; preds = %1
%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 = icmp ne i32* %9, null
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32*, i32** %3
%13 = call i32 (...) @getpid()
%14 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32*, i32** %3
%16 = call i32 @fclose(i32* %15)
br label %21
17: ; preds = %7
%18 = load i32, i32* @errno
%19 = load i8*, i8** %2
%20 = call i32 @error(i32 1, i32 %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %6, %17, %11
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"fopen %s\00"
define void @writepidfile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32 @fclose(i32* %4)
br label %13
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = tail call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %6, %10, %1
ret void
}