#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*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int dcd_getpid(void) {
  int pid;
  FILE *fd = fopen(fname(), "r");
  if (NULL == fd)
    return 0;
  fscanf(fd, "%i", &pid);
  fclose(fd);
  return pid;
}


x86 assembly (clang, -O0):
<dcd_getpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fname@plt>
mov    %eax,%edi
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1179 <dcd_getpid+0x39>
movl   $0x0,-0x4(%rbp)
jmp    119c <dcd_getpid+0x5c>
mov    -0x10(%rbp),%rdi
lea    0xe7e(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <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_getpid>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fname@plt>
lea    0xead(%rip),%rsi
mov    %eax,%edi
call   1050 <fopen@plt>
test   %rax,%rax
je     1182 <dcd_getpid+0x42>
mov    %rax,%rbx
lea    0xe99(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%i\00"
define i32 @dcd_getpid() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fname()
%4 = tail call i32* @fopen(i32 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%8 = call i32 @fclose(i32* %4)
%9 = load i32, i32* %1
br label %10
10: ; preds = %0, %6
%11 = phi i32 [ %9, %6 ], [ 0, %0 ]
ret i32 %11
}