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

/* Variables and functions */
int /*<<< orphan*/ die(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void write_pid_file(const char *path) {
  FILE *fp;
  char buf[8192];
  fp = fopen(path, "w");
  if (fp == NULL)
    die("Cannot open %s", path);
  snprintf(buf, sizeof(buf), "%d", getpid());
  fputs(buf, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119e <write_pid_file+0x3e>
mov    -0x8(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1080 <die@plt>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2018(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2018(%rbp),%rdi
mov    %eax,%ecx
mov    $0x2000,%esi
lea    0xe49(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x2010(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid_file>:
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rdi,%r14
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <write_pid_file+0x30>
lea    0xe7a(%rip),%rdi
mov    %r14,%rsi
call   1080 <die@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe73(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x2000,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x2008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"Cannot open %s\00"
@.str.2 = [3 x i8] c"%d\00"
define void @write_pid_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [8192 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %13, i32 8192, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%17 = load i32*, i32** %3
%18 = call i32 @fputs(i8* %16, i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"Cannot open %s\00"
@.str.2 = [3 x i8] c"%d\00"
define void @write_pid_file(i8* %0) {
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%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 %6, label %8
6: ; preds = %1
%7 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %1
%9 = tail call i32 (...) @getpid()
%10 = call i32 @snprintf(i8* %3, i32 8192, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i32 @fputs(i8* %3, i32* %4)
%12 = call i32 @fclose(i32* %4)
ret void
}