#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
pid_t pidfile_read(const char *filename) {
  FILE *file;
  pid_t pid;
  int rval;
  file = fopen(filename, "r");
  if (file == NULL)
    return 0;
  rval = fscanf(file, "%d", &pid);
  if (rval < 1 || rval == EOF)
    pid = -1;
  fclose(file);
  return pid;
}


x86 assembly (clang, -O0):
<pidfile_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1167 <pidfile_read+0x37>
movl   $0x0,-0x4(%rbp)
jmp    11b0 <pidfile_read+0x80>
mov    -0x18(%rbp),%rdi
lea    0xe90(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jl     119a <pidfile_read+0x6a>
mov    -0x20(%rbp),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
jne    11a1 <pidfile_read+0x71>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_read>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1182 <pidfile_read+0x52>
mov    %rax,%rbx
lea    0xeb2(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
test   %eax,%eax
jle    116c <pidfile_read+0x3c>
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%eax
jne    1174 <pidfile_read+0x44>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    1184 <pidfile_read+0x54>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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