#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ printf(char *, char *, char *);
char *strerror(int /*<<< orphan*/);
pid_t sld_daemon_write_pid_file(char *pid_file_name) {
  FILE *pid_file;
  pid_file = fopen(pid_file_name, "w");
  if (pid_file == NULL) {
    printf("Unable to open %s for writing: %s\n", pid_file_name,
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  fprintf(pid_file, "%d\n", getpid());
  fclose(pid_file);
  return getpid();
}


x86 assembly (clang, -O0):
<sld_daemon_write_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <sld_daemon_write_pid_file+0x62>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sld_daemon_write_pid_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b9 <sld_daemon_write_pid_file+0x49>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <getpid@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [35 x i8] c"Unable to open %s for writing: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @sld_daemon_write_pid_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = call i32 (...) @getpid()
%18 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
%21 = call i32 (...) @getpid()
ret i32 %21
}

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