#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ logError(char *);
void writePID(const char *path) {
  pid_t pid = getpid();
  FILE *fp = fopen(path, "w");
  if (fp == NULL) {
    logError("Error opening PID file.\n");
    return;
  }
  if (fprintf(fp, "%d\n", pid) < 0) {
    logError("Error writing to PID file.\n");
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<writePID>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <writePID+0x46>
lea    0xe76(%rip),%rdi
call   1050 <logError@plt>
jmp    11c8 <writePID+0x78>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe77(%rip),%rsi
call   1060 <fprintf@plt>
cmp    $0x0,%rax
jge    11bf <writePID+0x6f>
lea    0xe65(%rip),%rdi
call   1050 <logError@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writePID>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a6 <writePID+0x56>
mov    %rax,%rbx
lea    0xe9e(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <fprintf@plt>
test   %rax,%rax
jns    1198 <writePID+0x48>
lea    0xe8c(%rip),%rdi
call   1050 <logError@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe55(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <logError@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"Error opening PID file.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [28 x i8] c"Error writing to PID file.\0A\00"
define void @writePID(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i8* %0, i8** %2
%5 = call i32 (...) @getpid()
store i32 %5, i32* %3
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @logError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = load i32, i32* %3
%15 = call i64 @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @logError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
br label %22
22: ; preds = %19, %10
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"Error opening PID file.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [28 x i8] c"Error writing to PID file.\0A\00"
define void @writePID(i8* nocapture %0) {
%2 = tail call i32 (...) @getpid()
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @logError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %14
7: ; preds = %1
%8 = tail call i64 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @logError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @fclose(i32* %3)
br label %14
14: ; preds = %12, %5
ret void
}