#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*/ pid_t;
typedef int /*<<< orphan*/ pid;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ESRCH;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ getpid();
int kill(int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, ...);
int readData(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char const *);
int /*<<< orphan*/ writeData(int, int /*<<< orphan*/ *, int);
int spcLockFile(const char *lfpath) {
  int attempt;
  int fd;
  int result;
  pid_t pid;
  for (attempt = 0; attempt < 3; attempt++) {
    if ((fd = open(lfpath, O_RDWR | O_CREAT | O_EXCL, S_IRWXU)) == -1) {
      if (errno != EEXIST)
        return -1;
      if ((fd = open(lfpath, O_RDONLY)) == -1)
        return -1;
      result = readData(fd, &pid, sizeof(pid));
      close(fd);
      if (result) {
        if (pid == getpid())
          return 1;
        if (kill(pid, 0) == -1) {
          if (errno != ESRCH)
            return -1;
          attempt--;
          unlink(lfpath);
          continue;
        }
      }
      sleep(1);
      continue;
    }
    pid = getpid();
    if (!writeData(fd, &pid, sizeof(pid))) {
      close(fd);
      return -1;
    }
    close(fd);
    attempt--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spcLockFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1331 <spcLockFile+0x1b1>
mov    -0x10(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12da <spcLockFile+0x15a>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
je     11fe <spcLockFile+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <spcLockFile+0x1b8>
mov    -0x10(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122a <spcLockFile+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <spcLockFile+0x1b8>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <readData@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12cb <spcLockFile+0x14b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jne    1276 <spcLockFile+0xf6>
movl   $0x1,-0x4(%rbp)
jmp    1338 <spcLockFile+0x1b8>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0xffffffff,%eax
jne    12c6 <spcLockFile+0x146>
mov    0x2d20(%rip),%rax
mov    (%rax),%rax
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
je     12af <spcLockFile+0x12f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <spcLockFile+0x1b8>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
jmp    1323 <spcLockFile+0x1a3>
jmp    12cb <spcLockFile+0x14b>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1323 <spcLockFile+0x1a3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <writeData@plt>
cmp    $0x0,%eax
jne    1312 <spcLockFile+0x192>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <spcLockFile+0x1b8>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1193 <spcLockFile+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spcLockFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    0x2e43(%rip),%r14
mov    0x2e5c(%rip),%r15
xor    %r13d,%r13d
jmp    11cb <spcLockFile+0x4b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0x1(%r13),%eax
cmp    $0x2,%r13d
mov    %eax,%r13d
jge    12e1 <spcLockFile+0x161>
mov    (%r15),%esi
or     (%r14),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1230 <spcLockFile+0xb0>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1090 <writeData@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebx,%ebx
je     12da <spcLockFile+0x15a>
add    $0xffffffff,%r13d
jmp    11ba <spcLockFile+0x3a>
nopw   0x0(%rax,%rax,1)
mov    0x2d79(%rip),%rax
mov    (%rax),%rdx
mov    $0xffffffff,%eax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rdx
jne    12ea <spcLockFile+0x16a>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12da <spcLockFile+0x15a>
mov    %eax,%ebx
mov    %eax,%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1060 <readData@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
test   %ebp,%ebp
je     11b0 <spcLockFile+0x30>
mov    0x4(%rsp),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %eax,%ebx
je     12e5 <spcLockFile+0x165>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0xffffffff,%eax
jne    11b0 <spcLockFile+0x30>
mov    0x2cfb(%rip),%rax
mov    (%rax),%rax
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
mov    $0xffffffff,%eax
jne    12ea <spcLockFile+0x16a>
add    $0xffffffff,%r13d
mov    %r12,%rdi
call   1030 <unlink@plt>
jmp    11ba <spcLockFile+0x3a>
mov    $0xffffffff,%eax
jmp    12ea <spcLockFile+0x16a>
xor    %eax,%eax
jmp    12ea <spcLockFile+0x16a>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDONLY = global i32 0
@ESRCH = global i64 0
define i32 @spcLockFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
br label %8
8: ; preds = %74, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %77
11: ; preds = %8
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRWXU
%19 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %17, i32 %18)
store i32 %19, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %61
21: ; preds = %11
%22 = load i64, i64* @errno
%23 = load i64, i64* @EEXIST
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %2
br label %78
26: ; preds = %21
%27 = load i8*, i8** %3
%28 = load i32, i32* @O_RDONLY
%29 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %28)
store i32 %29, i32* %5
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %2
br label %78
32: ; preds = %26
%33 = load i32, i32* %5
%34 = call i32 @readData(i32 %33, i32* %7, i32 4)
store i32 %34, i32* %6
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %6
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %59
39: ; preds = %32
%40 = load i32, i32* %7
%41 = call i32 (...) @getpid()
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 1, i32* %2
br label %78
44: ; preds = %39
%45 = load i32, i32* %7
%46 = call i32 @kill(i32 %45, i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %58
48: ; preds = %44
%49 = load i64, i64* @errno
%50 = load i64, i64* @ESRCH
%51 = icmp ne i64 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 -1, i32* %2
br label %78
53: ; preds = %48
%54 = load i32, i32* %4
%55 = add nsw i32 %54, -1
store i32 %55, i32* %4
%56 = load i8*, i8** %3
%57 = call i32 @unlink(i8* %56)
br label %74
58: ; preds = %44
br label %59
59: ; preds = %58, %32
%60 = call i32 @sleep(i32 1)
br label %74
61: ; preds = %11
%62 = call i32 (...) @getpid()
store i32 %62, i32* %7
%63 = load i32, i32* %5
%64 = call i32 @writeData(i32 %63, i32* %7, i32 4)
%65 = icmp ne i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %61
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %2
br label %78
69: ; preds = %61
%70 = load i32, i32* %5
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %4
%73 = add nsw i32 %72, -1
store i32 %73, i32* %4
br label %74
74: ; preds = %69, %59, %53
%75 = load i32, i32* %4
%76 = add nsw i32 %75, 1
store i32 %76, i32* %4
br label %8
77: ; preds = %8
store i32 0, i32* %2
br label %78
78: ; preds = %77, %66, %52, %43, %31, %25
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDONLY = global i32 0
@ESRCH = global i64 0
define i32 @spcLockFile(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %1, %50
%5 = phi i32 [ 0, %1 ], [ %52, %50 ]
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRWXU
%12 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %43
14: ; preds = %4
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %54
18: ; preds = %14
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %54, label %22
22: ; preds = %18
%23 = call i32 @readData(i32 %20, i32* %2, i32 4)
%24 = call i32 @close(i32 %20)
%25 = icmp eq i32 %23, 0
br i1 %25, label %41, label %26
26: ; preds = %22
%27 = load i32, i32* %2
%28 = call i32 (...) @getpid()
%29 = icmp eq i32 %27, %28
br i1 %29, label %54, label %30
30: ; preds = %26
%31 = load i32, i32* %2
%32 = call i32 @kill(i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @ESRCH
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %54
38: ; preds = %34
%39 = add nsw i32 %5, -1
%40 = call i32 @unlink(i8* %0)
br label %50
41: ; preds = %30, %22
%42 = call i32 @sleep(i32 1)
br label %50
43: ; preds = %4
%44 = call i32 (...) @getpid()
store i32 %44, i32* %2
%45 = call i32 @writeData(i32 %12, i32* %2, i32 4)
%46 = icmp eq i32 %45, 0
%47 = call i32 @close(i32 %12)
br i1 %46, label %54, label %48
48: ; preds = %43
%49 = add nsw i32 %5, -1
br label %50
50: ; preds = %48, %41, %38
%51 = phi i32 [ %39, %38 ], [ %5, %41 ], [ %49, %48 ]
%52 = add nsw i32 %51, 1
%53 = icmp slt i32 %51, 2
br i1 %53, label %4, label %54
54: ; preds = %50, %34, %26, %18, %14, %43
%55 = phi i32 [ -1, %43 ], [ -1, %14 ], [ -1, %18 ], [ 1, %26 ], [ -1, %34 ], [ 0, %50 ]
ret i32 %55
}