#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ yar_log_ex(int, char *, int /*<<< orphan*/);
int yar_record_pid(char *pfile) {
  FILE *fp;
  fp = fopen(pfile, "w+");
  if (!fp) {
    yar_log_ex(0x8, "Failed to write pid file '%s'", strerror(errno));
    return 0;
  }
  fprintf(fp, "%d", getpid());
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<yar_record_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <yar_record_pid+0x58>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x8,%edi
lea    0xe5c(%rip),%rsi
call   1050 <yar_log_ex@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e9 <yar_record_pid+0x89>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe4d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<yar_record_pid>:
push   %rbx
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     119c <yar_record_pid+0x3c>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe52(%rip),%rsi
mov    $0x8,%edi
mov    %eax,%edx
call   1050 <yar_log_ex@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [30 x i8] c"Failed to write pid file '%s'\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @yar_record_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 @yar_log_ex(i32 8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 0, i32* %2
br label %19
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = call i32 (...) @getpid()
%16 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
store i32 1, i32* %2
br label %19
19: ; preds = %13, %9
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [30 x i8] c"Failed to write pid file '%s'\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @yar_record_pid(i8* nocapture %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 %4, label %8
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
%7 = tail call i32 @yar_log_ex(i32 8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %12
8: ; preds = %1
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 @fclose(i32* %2)
br label %12
12: ; preds = %8, %4
%13 = phi i32 [ 1, %8 ], [ 0, %4 ]
ret i32 %13
}