#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*/ fileno(int /*<<< orphan*/ *);
int flclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ flopen(char const *, char *, int /*<<< orphan*/ **);
int /*<<< orphan*/ fltryopen(char const *, char *, int /*<<< orphan*/ **);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ ftello(int /*<<< orphan*/ *);
scalar_t__ ftruncate(int /*<<< orphan*/, int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int pidlock_set(const char *path) {
  assert(path);
  FILE *f;
  while (!fltryopen(path, "r+", &f) && ENOENT == errno) {
    if (!flopen(path, "a", &f)) {
      break;
    }
    flclose(f);
  }
  if (!f) {
    return (-1);
  }
  pid_t pid;
  if (fscanf(f, "%16d\n", &pid) != 1) {
    pid = -1;
  }
  if (getpid() == pid) {
    flclose(f);
    return (0);
  }
  if (pid > 0 && !kill(pid, 0)) {
    flclose(f);
    return (-1);
  }
  rewind(f);
  if (fprintf(f, "%d\n", getpid()) <= 0) {
    flclose(f);
    return (-1);
  }
  if (ftruncate(fileno(f), ftello(f))) {
    flclose(f);
    return (-1);
  }
  return (flclose(f));
}


x86 assembly (clang, -O0):
<pidlock_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   10e0 <fltryopen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
jne    1213 <pidlock_set+0x53>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1223 <pidlock_set+0x63>
jmp    1253 <pidlock_set+0x93>
mov    -0x10(%rbp),%rdi
lea    0xdd5(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <flopen@plt>
cmp    $0x0,%eax
jne    1245 <pidlock_set+0x85>
jmp    1253 <pidlock_set+0x93>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
jmp    11d5 <pidlock_set+0x15>
cmpq   $0x0,-0x18(%rbp)
jne    126a <pidlock_set+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <pidlock_set+0x1b8>
mov    -0x18(%rbp),%rdi
lea    0xd90(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <fscanf@plt>
cmp    $0x1,%eax
je     128e <pidlock_set+0xce>
movl   $0xffffffff,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
cmp    -0x1c(%rbp),%eax
jne    12b3 <pidlock_set+0xf3>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1378 <pidlock_set+0x1b8>
cmpl   $0x0,-0x1c(%rbp)
jle    12e5 <pidlock_set+0x125>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   10b0 <kill@plt>
cmp    $0x0,%eax
jne    12e5 <pidlock_set+0x125>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <pidlock_set+0x1b8>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xd01(%rip),%rsi
call   10a0 <fprintf@plt>
cmp    $0x0,%rax
jg     132e <pidlock_set+0x16e>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <pidlock_set+0x1b8>
mov    -0x18(%rbp),%rdi
call   10c0 <fileno@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <ftello@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
je     136c <pidlock_set+0x1ac>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <pidlock_set+0x1b8>
mov    -0x18(%rbp),%rdi
call   10d0 <flclose@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidlock_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1090 <assert@plt>
lea    0xe23(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   10e0 <fltryopen@plt>
test   %eax,%eax
jne    1258 <pidlock_set+0x98>
mov    0x2ddb(%rip),%r13
mov    0x2de4(%rip),%rbp
mov    0x0(%rbp),%rax
cmp    %rax,0x0(%r13)
jne    1258 <pidlock_set+0x98>
lea    0xdf6(%rip),%r14
lea    0x8(%rsp),%r15
lea    0xde7(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <flopen@plt>
test   %eax,%eax
je     1258 <pidlock_set+0x98>
mov    0x8(%rsp),%rdi
call   10d0 <flclose@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10e0 <fltryopen@plt>
test   %eax,%eax
jne    1258 <pidlock_set+0x98>
mov    0x0(%rbp),%rax
cmp    %rax,0x0(%r13)
je     1220 <pidlock_set+0x60>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     131b <pidlock_set+0x15b>
lea    0xd98(%rip),%rsi
lea    0x14(%rsp),%rdx
call   1080 <fscanf@plt>
cmp    $0x1,%eax
je     1284 <pidlock_set+0xc4>
movl   $0xffffffff,0x14(%rsp)
ff
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x14(%rsp),%edi
cmp    %edi,%eax
jne    12a1 <pidlock_set+0xe1>
mov    0x8(%rsp),%rdi
call   10d0 <flclose@plt>
jmp    1320 <pidlock_set+0x160>
test   %edi,%edi
jle    12b0 <pidlock_set+0xf0>
xor    %esi,%esi
call   10b0 <kill@plt>
test   %eax,%eax
je     1311 <pidlock_set+0x151>
mov    0x8(%rsp),%rdi
call   1060 <rewind@plt>
mov    0x8(%rsp),%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd3e(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <fprintf@plt>
mov    0x8(%rsp),%rdi
test   %rax,%rax
jle    1316 <pidlock_set+0x156>
call   10c0 <fileno@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <ftello@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1070 <ftruncate@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   10d0 <flclose@plt>
neg    %rbx
sbb    %ebx,%ebx
or     %eax,%ebx
jmp    1320 <pidlock_set+0x160>
mov    0x8(%rsp),%rdi
call   10d0 <flclose@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [6 x i8] c"%16d\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @pidlock_set(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)
br label %8
8: ; preds = %23, %1
%9 = load i8*, i8** %3
%10 = call i32 @fltryopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32** %4)
%11 = icmp ne i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = load i64, i64* @ENOENT
%14 = load i64, i64* @errno
%15 = icmp eq i64 %13, %14
br label %16
16: ; preds = %12, %8
%17 = phi i1 [ false, %8 ], [ %15, %12 ]
br i1 %17, label %18, label %26
18: ; preds = %16
%19 = load i8*, i8** %3
%20 = call i32 @flopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32** %4)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %18
br label %26
23: ; preds = %18
%24 = load i32*, i32** %4
%25 = call i32 @flclose(i32* %24)
br label %8
26: ; preds = %22, %16
%27 = load i32*, i32** %4
%28 = icmp ne i32* %27, null
br i1 %28, label %30, label %29
29: ; preds = %26
store i32 -1, i32* %2
br label %75
30: ; preds = %26
%31 = load i32*, i32** %4
%32 = call i32 @fscanf(i32* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %5
br label %35
35: ; preds = %34, %30
%36 = call i32 (...) @getpid()
%37 = load i32, i32* %5
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %4
%41 = call i32 @flclose(i32* %40)
store i32 0, i32* %2
br label %75
42: ; preds = %35
%43 = load i32, i32* %5
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i32, i32* %5
%47 = call i32 @kill(i32 %46, i32 0)
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = load i32*, i32** %4
%51 = call i32 @flclose(i32* %50)
store i32 -1, i32* %2
br label %75
52: ; preds = %45, %42
%53 = load i32*, i32** %4
%54 = call i32 @rewind(i32* %53)
%55 = load i32*, i32** %4
%56 = call i32 (...) @getpid()
%57 = call i64 @fprintf(i32* %55, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = icmp sle i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = load i32*, i32** %4
%61 = call i32 @flclose(i32* %60)
store i32 -1, i32* %2
br label %75
62: ; preds = %52
%63 = load i32*, i32** %4
%64 = call i32 @fileno(i32* %63)
%65 = load i32*, i32** %4
%66 = call i32 @ftello(i32* %65)
%67 = call i64 @ftruncate(i32 %64, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %62
%70 = load i32*, i32** %4
%71 = call i32 @flclose(i32* %70)
store i32 -1, i32* %2
br label %75
72: ; preds = %62
%73 = load i32*, i32** %4
%74 = call i32 @flclose(i32* %73)
store i32 %74, i32* %2
br label %75
75: ; preds = %72, %69, %59, %49, %39, %29
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@ENOENT = global i64 0
@errno = global i64 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [6 x i8] c"%16d\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @pidlock_set(i8* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = tail call i32 @assert(i8* %0)
%5 = bitcast i32** %2 to i8*
%6 = call i32 @fltryopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32** %2)
%7 = icmp eq i32 %6, 0
%8 = load i64, i64* @ENOENT
%9 = load i64, i64* @errno
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %12, label %24
12: ; preds = %1, %15
%13 = call i32 @flopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32** %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = load i32*, i32** %2
%17 = call i32 @flclose(i32* %16)
%18 = call i32 @fltryopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32** %2)
%19 = icmp eq i32 %18, 0
%20 = load i64, i64* @ENOENT
%21 = load i64, i64* @errno
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %12, label %24
24: ; preds = %15, %12, %1
%25 = load i32*, i32** %2
%26 = icmp eq i32* %25, null
br i1 %26, label %68, label %27
27: ; preds = %24
%28 = bitcast i32* %3 to i8*
%29 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%30 = icmp eq i32 %29, 1
br i1 %30, label %32, label %31
31: ; preds = %27
store i32 -1, i32* %3
br label %32
32: ; preds = %31, %27
%33 = call i32 (...) @getpid()
%34 = load i32, i32* %3
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32*, i32** %2
%38 = call i32 @flclose(i32* %37)
br label %66
39: ; preds = %32
%40 = icmp sgt i32 %34, 0
br i1 %40, label %41, label %47
41: ; preds = %39
%42 = call i32 @kill(i32 %34, i32 0)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32*, i32** %2
%46 = call i32 @flclose(i32* %45)
br label %66
47: ; preds = %41, %39
%48 = load i32*, i32** %2
%49 = call i32 @rewind(i32* %48)
%50 = load i32*, i32** %2
%51 = call i32 (...) @getpid()
%52 = call i64 @fprintf(i32* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = icmp slt i64 %52, 1
%54 = load i32*, i32** %2
br i1 %53, label %55, label %57
55: ; preds = %47
%56 = call i32 @flclose(i32* %54)
br label %66
57: ; preds = %47
%58 = call i32 @fileno(i32* %54)
%59 = load i32*, i32** %2
%60 = call i32 @ftello(i32* %59)
%61 = call i64 @ftruncate(i32 %58, i32 %60)
%62 = icmp eq i64 %61, 0
%63 = load i32*, i32** %2
%64 = call i32 @flclose(i32* %63)
%65 = select i1 %62, i32 %64, i32 -1
br label %66
66: ; preds = %57, %55, %44, %36
%67 = phi i32 [ 0, %36 ], [ -1, %55 ], [ -1, %44 ], [ %65, %57 ]
br label %68
68: ; preds = %24, %66
%69 = phi i32 [ %67, %66 ], [ -1, %24 ]
ret i32 %69
}