#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 */
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
int /*<<< orphan*/ stderr;
void write_pid_to_file(char *fname) {
  FILE *fp;
  if (!(fp = fopen(fname, "w+")) || fprintf(fp, "%d\n", (int)getpid()) < 0 ||
      fclose(fp) == EOF)
    goto write_pid_to_file_error;
  return;
write_pid_to_file_error:
  (void)fclose(fp);
  fprintf(stderr, "Failed to write pid to file '%s'.\n", fname);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<write_pid_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11bd <write_pid_to_file+0x6d>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmp    $0x0,%eax
jl     11bd <write_pid_to_file+0x6d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11c2 <write_pid_to_file+0x72>
jmp    11c8 <write_pid_to_file+0x78>
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<write_pid_to_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a4 <write_pid_to_file+0x54>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8a(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %eax,%eax
js     11a4 <write_pid_to_file+0x54>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <write_pid_to_file+0x54>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@stderr = global i32 0
@.str.2 = [35 x i8] c"Failed to write pid to file '%s'.\0A\00"
@EXIT_FAILURE = global i32 0
define void @write_pid_to_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 ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %19
7: ; preds = %1
%8 = load i32*, i32** %3
%9 = ptrtoint i32* %8 to i32
%10 = call i64 (...) @getpid()
%11 = trunc i64 %10 to i32
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %7
%15 = load i32*, i32** %3
%16 = call i64 @fclose(i32* %15)
%17 = load i64, i64* @EOF
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %14, %7, %1
br label %21
20: ; preds = %14
ret void
21: ; preds = %19
%22 = load i32*, i32** %3
%23 = call i64 @fclose(i32* %22)
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %2
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@stderr = global i32 0
@.str.2 = [35 x i8] c"Failed to write pid to file '%s'.\0A\00"
@EXIT_FAILURE = global i32 0
define void @write_pid_to_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = ptrtoint i32* %2 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i64 (...) @getpid()
%8 = trunc i64 %7 to i32
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = tail call i64 @fclose(i32* %2)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %16, label %15
15: ; preds = %11
ret void
16: ; preds = %1, %4, %11
%17 = tail call i64 @fclose(i32* %2)
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
}