#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*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *create_file_name(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
pid_t check_pid_file() {
  FILE *file;
  pid_t pid;
  char *fname = create_file_name("pid");
  if ((file = fopen(fname, "r")) == NULL)
    return 0;
  if (fscanf(file, "%d", &pid) != 1) {
    fclose(file);
    return 0;
  }
  fclose(file);
  return pid;
}


x86 assembly (clang, -O0):
<check_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1050 <create_file_name@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1182 <check_pid_file+0x42>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <check_pid_file+0x83>
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11b4 <check_pid_file+0x74>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <check_pid_file+0x83>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid_file>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rdi
call   1050 <create_file_name@plt>
lea    0xeae(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1191 <check_pid_file+0x51>
mov    %rax,%rbx
lea    0xe99(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0x1,%ebp
jne    1193 <check_pid_file+0x53>
mov    0x4(%rsp),%eax
jmp    1193 <check_pid_file+0x53>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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