#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 */
scalar_t__ ESRCH;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int kill(int, int /*<<< orphan*/);
scalar_t__ remove(char *);
int read_pid() {
  FILE *file = NULL;
  int pid = -1;
  file = fopen("/var/run/mbpfan.pid", "r");
  if (file != NULL) {
    fscanf(file, "%d", &pid);
    fclose(file);
    if (kill(pid, 0) == -1 && errno == ESRCH) {
      if (remove("/var/run/mbpfan.pid") == 0) {
        return -1;
      }
    }
    return pid;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    0xe92(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1205 <read_pid+0xb5>
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0xffffffff,%eax
jne    11fa <read_pid+0xaa>
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <read_pid+0xaa>
lea    0xe26(%rip),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
jne    11f5 <read_pid+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <read_pid+0xbc>
jmp    11fa <read_pid+0xaa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <read_pid+0xbc>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pid>:
push   %rbp
push   %rbx
push   %rax
movl   $0xffffffff,0x4(%rsp)
ff
lea    0xe9e(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11d2 <read_pid+0x82>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0xffffffff,%eax
jne    11ce <read_pid+0x7e>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11ce <read_pid+0x7e>
lea    0xe3c(%rip),%rdi
call   1030 <remove@plt>
test   %rax,%rax
je     11d2 <read_pid+0x82>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [20 x i8] c"/var/run/mbpfan.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @read_pid() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %20, label %5
5: ; preds = %0
%6 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%7 = call i32 @fclose(i32* %3)
%8 = load i32, i32* %1
%9 = call i32 @kill(i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @ESRCH
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i64 @remove(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15, %11, %5
%19 = load i32, i32* %1
br label %20
20: ; preds = %0, %15, %18
%21 = phi i32 [ %19, %18 ], [ -1, %15 ], [ -1, %0 ]
ret i32 %21
}