#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*/ fname();
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int dcd_setpid(void) {
  int pid = getpid();
  FILE *fd = fopen(fname(), "w+");
  if (NULL == fd)
    return -1;
  fprintf(fd, "%i\n", pid);
  fclose(fd);
  return pid;
}


x86 assembly (clang, -O0):
<dcd_setpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1070 <fname@plt>
mov    %eax,%edi
lea    0xe8e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1193 <dcd_setpid+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <dcd_setpid+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dcd_setpid>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <fname@plt>
lea    0xe96(%rip),%rsi
mov    %eax,%edi
call   1060 <fopen@plt>
test   %rax,%rax
je     1194 <dcd_setpid+0x44>
mov    %rax,%rbx
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1199 <dcd_setpid+0x49>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%i\0A\00"
define i32 @dcd_setpid() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @fname()
%3 = tail call i32* @fopen(i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%7 = tail call i32 @fclose(i32* %3)
br label %8
8: ; preds = %0, %5
%9 = phi i32 [ %1, %5 ], [ -1, %0 ]
ret i32 %9
}