#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
void log_pid() {
  FILE *f;
  pid_t pid;
  char *pidfile = "/var/run/wdg.pid";
  pid = getpid();
  if ((f = fopen(pidfile, "w")) == NULL)
    return;
  fprintf(f, "%d\n", pid);
  fclose(f);
}


x86 assembly (clang, -O0):
<log_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1180 <log_pid+0x40>
jmp    119c <log_pid+0x5c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe85(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_pid>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xead(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1186 <log_pid+0x46>
mov    %rax,%rbx
lea    0xea5(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/var/run/wdg.pid\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @log_pid() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = call i32 (...) @getpid()
store i32 %4, i32* %2
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %1
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
br label %15
9: ; preds = %0
%10 = load i32*, i32** %1
%11 = load i32, i32* %2
%12 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %1
%14 = call i32 @fclose(i32* %13)
br label %15
15: ; preds = %9, %8
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/var/run/wdg.pid\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @log_pid() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%6 = tail call i32 @fclose(i32* %2)
br label %7
7: ; preds = %0, %4
ret void
}