#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ assert(char const *);
scalar_t__ errno;
int /*<<< orphan*/ flclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ flopen(char const *, char *, int /*<<< orphan*/ **);
int fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ kill(int, int /*<<< orphan*/);
pid_t pidlock_get(const char *path) {
  assert(path);
  FILE *f;
  if (!flopen(path, "r", &f)) {
    return (ENOENT == errno ? 0 : -1);
  }
  pid_t pid;
  if (fscanf(f, "%16d\n", &pid) != 1) {
    pid = -1;
  }
  flclose(f);
  if (pid > 0 && kill(pid, 0)) {
    return (0);
  }
  return (pid);
}


x86 assembly (clang, -O0):
<pidlock_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe90(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <flopen@plt>
cmp    $0x0,%eax
jne    11ab <pidlock_get+0x5b>
mov    0x2e47(%rip),%rax
mov    (%rax),%rdx
mov    0x2e4d(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <pidlock_get+0xb8>
mov    -0x18(%rbp),%rdi
lea    0xe4c(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11cf <pidlock_get+0x7f>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <flclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1202 <pidlock_get+0xb2>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1202 <pidlock_get+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    1208 <pidlock_get+0xb8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidlock_get>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1050 <assert@plt>
lea    0xe9c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <flopen@plt>
test   %eax,%eax
je     11c2 <pidlock_get+0x72>
mov    0x8(%rsp),%rdi
lea    0xe81(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     1198 <pidlock_get+0x48>
movl   $0xffffffff,0x4(%rsp)
ff
mov    0x8(%rsp),%rdi
call   1070 <flclose@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    11dd <pidlock_get+0x8d>
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
jne    11be <pidlock_get+0x6e>
mov    0x4(%rsp),%ebx
mov    %ebx,%eax
jmp    11dd <pidlock_get+0x8d>
mov    0x2e07(%rip),%rax
mov    (%rax),%rcx
mov    0x2e0d(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [6 x i8] c"%16d\0A\00"
define i32 @pidlock_get(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 @assert(i8* %6)
%8 = load i8*, i8** %3
%9 = call i32 @flopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32** %4)
%10 = icmp ne i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = load i64, i64* @ENOENT
%13 = load i64, i64* @errno
%14 = icmp eq i64 %12, %13
%15 = zext i1 %14 to i64
%16 = select i1 %14, i32 0, i32 -1
store i32 %16, i32* %2
br label %34
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %5
br label %22
22: ; preds = %21, %17
%23 = load i32*, i32** %4
%24 = call i32 @flclose(i32* %23)
%25 = load i32, i32* %5
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* %5
%29 = call i64 @kill(i32 %28, i32 0)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 0, i32* %2
br label %34
32: ; preds = %27, %22
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %31, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [6 x i8] c"%16d\0A\00"
define i32 @pidlock_get(i8* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = tail call i32 @assert(i8* %0)
%5 = bitcast i32** %2 to i8*
%6 = call i32 @flopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32** %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i64, i64* @ENOENT
%10 = load i64, i64* @errno
%11 = icmp ne i64 %9, %10
%12 = sext i1 %11 to i32
br label %31
13: ; preds = %1
%14 = bitcast i32* %3 to i8*
%15 = load i32*, i32** %2
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%17 = icmp eq i32 %16, 1
br i1 %17, label %19, label %18
18: ; preds = %13
store i32 -1, i32* %3
br label %19
19: ; preds = %18, %13
%20 = load i32*, i32** %2
%21 = call i32 @flclose(i32* %20)
%22 = load i32, i32* %3
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = call i64 @kill(i32 %22, i32 0)
%26 = icmp eq i64 %25, 0
%27 = load i32, i32* %3
%28 = select i1 %26, i32 %27, i32 0
br label %29
29: ; preds = %24, %19
%30 = phi i32 [ %22, %19 ], [ %28, %24 ]
br label %31
31: ; preds = %29, %8
%32 = phi i32 [ %30, %29 ], [ %12, %8 ]
ret i32 %32
}