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

/* Variables and functions */
scalar_t__ ESRCH;
int atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ kill(int, int /*<<< orphan*/);
int pidfile_get(char *pidfile, pid_t *pid0) {
  FILE *f;
  char *ptr;
  char buf[32];
  pid_t pid;
  if ((f = fopen(pidfile, "r")) != NULL) {
    ptr = fgets(buf, sizeof(buf), f);
    fclose(f);
    if (ptr != NULL && (pid = atoi(ptr)) > 1) {
      if (kill(pid, 0) == 0 || errno != ESRCH) {
        *pid0 = pid;
        return 0;
      }
    }
  }
  *pid0 = 0;
  return 0;
}


x86 assembly (clang, -O0):
<pidfile_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     120a <pidfile_get+0xba>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x20,%esi
call   1040 <fgets@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1205 <pidfile_get+0xb5>
mov    -0x28(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x1,%eax
jle    1205 <pidfile_get+0xb5>
mov    -0x54(%rbp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
je     11eb <pidfile_get+0x9b>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
je     1200 <pidfile_get+0xb0>
mov    -0x54(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    121b <pidfile_get+0xcb>
jmp    1205 <pidfile_get+0xb5>
jmp    120a <pidfile_get+0xba>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c2 <pidfile_get+0x72>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     11c2 <pidfile_get+0x72>
mov    %rbx,%rdi
call   1070 <atoi@plt>
cmp    $0x2,%eax
jl     11c2 <pidfile_get+0x72>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1050 <kill@plt>
test   %rax,%rax
je     11c4 <pidfile_get+0x74>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    11c4 <pidfile_get+0x74>
xor    %ebp,%ebp
mov    %ebp,(%r14)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @pidfile_get(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [32 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %38
13: ; preds = %2
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i8* @fgets(i8* %14, i32 32, i32* %15)
store i8* %16, i8** %7
%17 = load i32*, i32** %6
%18 = call i32 @fclose(i32* %17)
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %37
21: ; preds = %13
%22 = load i8*, i8** %7
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %9
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = load i32, i32* %9
%27 = call i64 @kill(i32 %26, i32 0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @ESRCH
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29, %25
%34 = load i32, i32* %9
%35 = load i32*, i32** %5
store i32 %34, i32* %35
store i32 0, i32* %3
br label %40
36: ; preds = %29
br label %37
37: ; preds = %36, %21, %13
br label %38
38: ; preds = %37, %2
%39 = load i32*, i32** %5
store i32 0, i32* %39
store i32 0, i32* %3
br label %40
40: ; preds = %38, %33
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @pidfile_get(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %21, label %7
7: ; preds = %2
%8 = call i8* @fgets(i8* %4, i32 32, i32* %5)
%9 = call i32 @fclose(i32* %5)
%10 = icmp eq i8* %8, null
br i1 %10, label %21, label %11
11: ; preds = %7
%12 = call i32 @atoi(i8* %8)
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = call i64 @kill(i32 %12, i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @ESRCH
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %7, %11, %17, %2
br label %22
22: ; preds = %14, %17, %21
%23 = phi i32 [ 0, %21 ], [ %12, %17 ], [ %12, %14 ]
store i32 %23, i32* %1
ret i32 0
}