#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ printf(char *);
void set_pid_file(void) {
  FILE *fd;
  fd = fopen("/run/test.pid", "w+");
  if (!fd) {
    printf("Could not open "
           "/run/test.pid"
           " for writing!\n");
    return;
  }
  fprintf(fd, "%i", getpid());
  fclose(fd);
}


x86 assembly (clang, -O0):
<set_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118b <set_pid_file+0x3b>
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
jmp    11b5 <set_pid_file+0x65>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
lea    0xe95(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pid_file>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     118d <set_pid_file+0x3d>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <fclose@plt>
lea    0xe80(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/run/test.pid\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [43 x i8] c"Could not open /run/test.pid for writing!\0A\00"
@.str.3 = [3 x i8] c"%i\00"
define void @set_pid_file() {
%1 = alloca i32*
%2 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %2, i32** %1
%3 = load i32*, i32** %1
%4 = icmp ne i32* %3, null
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %13
7: ; preds = %0
%8 = load i32*, i32** %1
%9 = call i32 (...) @getpid()
%10 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = load i32*, i32** %1
%12 = call i32 @fclose(i32* %11)
br label %13
13: ; preds = %7, %5
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/run/test.pid\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.3 = [3 x i8] c"%i\00"
@str = [42 x i8] c"Could not open /run/test.pid for writing!\00"
define void @set_pid_file() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %9
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %6)
%8 = tail call i32 @fclose(i32* %1)
br label %9
9: ; preds = %5, %3
ret void
}