#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 fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int echo_pidfile_check(char *pathname) {
  FILE *f;
  int pid;
  f = fopen(pathname, "r");
  if (!f)
    return 0;
  if (fscanf(f, "%d", &pid) != 1)
    pid = 0;
  fclose(f);
  if (pid == getpid())
    return 0;
  if (kill(pid, 0) && errno == ESRCH)
    return 0;
  return pid;
}


x86 assembly (clang, -O0):
<echo_pidfile_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1187 <echo_pidfile_check+0x37>
movl   $0x0,-0x4(%rbp)
jmp    121a <echo_pidfile_check+0xca>
mov    -0x18(%rbp),%rdi
lea    0xe70(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11ab <echo_pidfile_check+0x5b>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jne    11da <echo_pidfile_check+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    121a <echo_pidfile_check+0xca>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1214 <echo_pidfile_check+0xc4>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <echo_pidfile_check+0xc4>
movl   $0x0,-0x4(%rbp)
jmp    121a <echo_pidfile_check+0xca>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_pidfile_check>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cf <echo_pidfile_check+0x7f>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1188 <echo_pidfile_check+0x38>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %eax,%ebp
je     11d1 <echo_pidfile_check+0x81>
mov    0x4(%rsp),%edi
xor    %ebx,%ebx
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     11c9 <echo_pidfile_check+0x79>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
je     11d1 <echo_pidfile_check+0x81>
mov    0x4(%rsp),%ebx
jmp    11d1 <echo_pidfile_check+0x81>
xor    %ebx,%ebx
mov    %ebx,%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"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @echo_pidfile_check(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 0, i32* %2
br label %34
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 0, i32* %5
br label %16
16: ; preds = %15, %11
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %5
%20 = call i32 (...) @getpid()
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 0, i32* %2
br label %34
23: ; preds = %16
%24 = load i32, i32* %5
%25 = call i64 @kill(i32 %24, i32 0)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @ESRCH
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 0, i32* %2
br label %34
32: ; preds = %27, %23
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %31, %22, %10
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @echo_pidfile_check(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 %25, 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 eq i32 %7, 1
br i1 %8, label %10, label %9
9: ; preds = %6
store i32 0, i32* %2
br label %10
10: ; preds = %9, %6
%11 = call i32 @fclose(i32* %4)
%12 = load i32, i32* %2
%13 = call i32 (...) @getpid()
%14 = icmp eq i32 %12, %13
br i1 %14, label %25, label %15
15: ; preds = %10
%16 = load i32, i32* %2
%17 = call i64 @kill(i32 %16, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @ESRCH
%22 = icmp eq i64 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %19, %15
%24 = load i32, i32* %2
br label %25
25: ; preds = %19, %10, %1, %23
%26 = phi i32 [ %24, %23 ], [ 0, %1 ], [ 0, %10 ], [ 0, %19 ]
ret i32 %26
}