#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*/ tmp_file;
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ lockfile;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EROFS;
scalar_t__ check_var(char *, char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int getlockpid(char *);
int getpid();
int /*<<< orphan*/ link(char *, char *);
int /*<<< orphan*/ logger(int, scalar_t__, char *, char *);
scalar_t__ make_dir(char *, int);
int mkstemp(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stat_file(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int vps_lock(unsigned int veid, char *dir, char *status) {
  int fd, pid;
  char buf[512];
  char lockfile[512];
  char tmp_file[512];
  struct stat st;
  int retry = 0;
  int ret = -1;
  if (check_var(dir, "lockdir is not set"))
    return -1;
  if (!stat_file(dir))
    if (make_dir(dir, 1))
      return -1;
  snprintf(lockfile, sizeof(lockfile), "%s/%d.lck", dir, veid);
  snprintf(tmp_file, sizeof(tmp_file), "%sXXXXXX", lockfile);
  if ((fd = mkstemp(tmp_file)) < 0) {
    if (errno == EROFS)
      logger(-1, errno,
             "Unable to create"
             " lock file %s, use --skiplock option",
             tmp_file);
    else
      logger(-1, errno,
             "Unable to create"
             " temporary lock file: %s",
             tmp_file);
    return -1;
  }
  snprintf(buf, sizeof(buf), "%d\n%s\n", getpid(),
           status == NULL ? "" : status);
  write(fd, buf, strlen(buf));
  close(fd);
  while (retry < 3) {
    if (!link(tmp_file, lockfile)) {
      ret = 0;
      break;
    }
    pid = getlockpid(lockfile);
    if (pid < 0) {
      usleep(500000);
      retry++;
      continue;
    } else if (pid == 0) {
      unlink(lockfile);
    } else {
      snprintf(buf, sizeof(buf), "/proc/%d", pid);
      if (!stat(buf, &st)) {
        ret = 1;
        break;
      } else {
        logger(0, 0,
               "Removing stale lock"
               " file %s",
               lockfile);
        unlink(lockfile);
      }
    }
    retry++;
  }
  unlink(tmp_file);
  return ret;
}


x86 assembly (clang, -O0):
<vps_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x660,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x62c(%rbp)
movl   $0xffffffff,-0x630(%rbp)
ff ff ff
mov    -0x10(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   10f0 <check_var@plt>
cmp    $0x0,%rax
je     1240 <vps_lock+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1519 <vps_lock+0x329>
mov    -0x10(%rbp),%rdi
call   10a0 <stat_file@plt>
cmp    $0x0,%eax
jne    127b <vps_lock+0x8b>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   10e0 <make_dir@plt>
cmp    $0x0,%rax
je     1276 <vps_lock+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    1519 <vps_lock+0x329>
jmp    127b <vps_lock+0x8b>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8d
mov    $0x200,%esi
lea    0xd7d(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x620(%rbp),%rdi
lea    -0x420(%rbp),%rcx
mov    $0x200,%esi
lea    0xd66(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x620(%rbp),%rdi
call   10d0 <mkstemp@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1345 <vps_lock+0x155>
mov    0x2cfb(%rip),%rax
mov    (%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
jne    1317 <vps_lock+0x127>
mov    0x2ce1(%rip),%rax
mov    (%rax),%rsi
lea    -0x620(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd19(%rip),%rdx
call   10c0 <logger@plt>
jmp    1339 <vps_lock+0x149>
mov    0x2cba(%rip),%rax
mov    (%rax),%rsi
lea    -0x620(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd27(%rip),%rdx
call   10c0 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1519 <vps_lock+0x329>
lea    -0x220(%rbp),%rax
mov    %rax,-0x640(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x634(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    137e <vps_lock+0x18e>
lea    0xd18(%rip),%rax
mov    %rax,-0x648(%rbp)
jmp    1389 <vps_lock+0x199>
mov    -0x18(%rbp),%rax
mov    %rax,-0x648(%rbp)
mov    -0x634(%rbp),%ecx
mov    -0x640(%rbp),%rdi
mov    -0x648(%rbp),%r8
mov    $0x200,%esi
lea    0xcdb(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x654(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x650(%rbp)
lea    -0x220(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x654(%rbp),%edi
mov    -0x650(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x3,-0x62c(%rbp)
jge    1504 <vps_lock+0x314>
lea    -0x620(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1100 <link@plt>
cmp    $0x0,%eax
jne    1427 <vps_lock+0x237>
movl   $0x0,-0x630(%rbp)
jmp    1504 <vps_lock+0x314>
lea    -0x420(%rbp),%rdi
call   1090 <getlockpid@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    145e <vps_lock+0x26e>
mov    $0x7a120,%edi
call   1110 <usleep@plt>
mov    -0x62c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x62c(%rbp)
jmp    13ef <vps_lock+0x1ff>
cmpl   $0x0,-0x20(%rbp)
jne    1479 <vps_lock+0x289>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
jmp    14eb <vps_lock+0x2fb>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%ecx
mov    $0x200,%esi
lea    0xbfc(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x628(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
jne    14c1 <vps_lock+0x2d1>
movl   $0x1,-0x630(%rbp)
jmp    1504 <vps_lock+0x314>
lea    -0x420(%rbp),%rcx
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    0xbbf(%rip),%rdx
call   10c0 <logger@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
jmp    14eb <vps_lock+0x2fb>
jmp    14f0 <vps_lock+0x300>
mov    -0x62c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x62c(%rbp)
jmp    13ef <vps_lock+0x1ff>
lea    -0x620(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x630(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x660,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vps_lock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x610,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdi
call   10f0 <check_var@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    150f <vps_lock+0x31f>
mov    %rbx,%rdi
call   10a0 <stat_file@plt>
test   %eax,%eax
je     1360 <vps_lock+0x170>
lea    0xdd7(%rip),%rdx
xor    %r14d,%r14d
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x200,%esi
mov    %rbx,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0xdbd(%rip),%rdx
lea    0x210(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r12,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   10d0 <mkstemp@plt>
test   %eax,%eax
js     137b <vps_lock+0x18b>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
test   %r15,%r15
lea    0xded(%rip),%r8
cmovne %r15,%r8
lea    0xddc(%rip),%rdx
lea    0x410(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0x210(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1100 <link@plt>
test   %eax,%eax
je     1502 <vps_lock+0x312>
lea    0x10(%rsp),%rdi
call   1090 <getlockpid@plt>
test   %eax,%eax
js     13be <vps_lock+0x1ce>
je     13ca <vps_lock+0x1da>
lea    0xd76(%rip),%rdx
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     14de <vps_lock+0x2ee>
lea    0xd4a(%rip),%rdx
lea    0x10(%rsp),%rbx
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   10c0 <logger@plt>
mov    %rbx,%rdi
jmp    13cf <vps_lock+0x1df>
mov    %rbx,%rdi
mov    $0x1,%esi
call   10e0 <make_dir@plt>
test   %rax,%rax
jne    150f <vps_lock+0x31f>
jmp    1235 <vps_lock+0x45>
mov    0x2c56(%rip),%rax
mov    (%rax),%rsi
mov    0x2c5c(%rip),%rax
cmp    (%rax),%rsi
jne    139a <vps_lock+0x1aa>
lea    0xc8e(%rip),%rdx
jmp    13a1 <vps_lock+0x1b1>
lea    0xcba(%rip),%rdx
mov    $0xffffffff,%r14d
lea    0x210(%rsp),%rcx
mov    $0xffffffff,%edi
call   10c0 <logger@plt>
jmp    150f <vps_lock+0x31f>
mov    $0x7a120,%edi
call   1110 <usleep@plt>
jmp    13d4 <vps_lock+0x1e4>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x210(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1100 <link@plt>
test   %eax,%eax
je     1502 <vps_lock+0x312>
lea    0x10(%rsp),%rdi
call   1090 <getlockpid@plt>
test   %eax,%eax
js     1450 <vps_lock+0x260>
je     145c <vps_lock+0x26c>
lea    0xc86(%rip),%rdx
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     14de <vps_lock+0x2ee>
lea    0xc5a(%rip),%rdx
lea    0x10(%rsp),%rbx
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   10c0 <logger@plt>
mov    %rbx,%rdi
jmp    1461 <vps_lock+0x271>
mov    $0x7a120,%edi
call   1110 <usleep@plt>
jmp    1466 <vps_lock+0x276>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x210(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1100 <link@plt>
test   %eax,%eax
je     1502 <vps_lock+0x312>
lea    0x10(%rsp),%rdi
call   1090 <getlockpid@plt>
test   %eax,%eax
js     14e6 <vps_lock+0x2f6>
je     14f2 <vps_lock+0x302>
lea    0xbf4(%rip),%rdx
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %eax,%eax
je     14de <vps_lock+0x2ee>
lea    0xbcc(%rip),%rdx
lea    0x10(%rsp),%rbx
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   10c0 <logger@plt>
mov    %rbx,%rdi
jmp    14f7 <vps_lock+0x307>
mov    $0x1,%r14d
jmp    1502 <vps_lock+0x312>
mov    $0x7a120,%edi
call   1110 <usleep@plt>
jmp    14fc <vps_lock+0x30c>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%r14d
lea    0x210(%rsp),%rdi
call   1030 <unlink@plt>
mov    %r14d,%eax
add    $0x610,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lockdir is not set\00"
@.str.1 = [10 x i8] c"%s/%d.lck\00"
@.str.2 = [9 x i8] c"%sXXXXXX\00"
@errno = global i64 0
@EROFS = global i64 0
@.str.3 = [53 x i8] c"Unable to create lock file %s, use --skiplock option\00"
@.str.4 = [41 x i8] c"Unable to create temporary lock file: %s\00"
@.str.5 = [7 x i8] c"%d\0A%s\0A\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c"/proc/%d\00"
@.str.8 = [28 x i8] c"Removing stale lock file %s\00"
define i32 @vps_lock(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [512 x i8]
%11 = alloca [512 x i8]
%12 = alloca [512 x i8]
%13 = alloca %struct.stat
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %14
store i32 -1, i32* %15
%16 = load i8*, i8** %6
%17 = call i64 @check_var(i8* %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %118
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = call i32 @stat_file(i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = call i64 @make_dir(i8* %25, i32 1)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %4
br label %118
29: ; preds = %24
br label %30
30: ; preds = %29, %20
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 512, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%37 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %35, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%39 = call i32 @mkstemp(i8* %38)
store i32 %39, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %30
%42 = load i64, i64* @errno
%43 = load i64, i64* @EROFS
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i64, i64* @errno
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%48 = call i32 @logger(i32 -1, i64 %46, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i8* %47)
br label %53
49: ; preds = %41
%50 = load i64, i64* @errno
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%52 = call i32 @logger(i32 -1, i64 %50, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %45
store i32 -1, i32* %4
br label %118
54: ; preds = %30
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%56 = call i32 (...) @getpid()
%57 = load i8*, i8** %7
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %54
br label %62
60: ; preds = %54
%61 = load i8*, i8** %7
br label %62
62: ; preds = %60, %59
%63 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), %59 ], [ %61, %60 ]
%64 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %55, i32 512, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %56, i8* %63)
%65 = load i32, i32* %8
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%68 = call i32 @strlen(i8* %67)
%69 = call i32 @write(i32 %65, i8* %66, i32 %68)
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %111, %86, %62
%73 = load i32, i32* %14
%74 = icmp slt i32 %73, 3
br i1 %74, label %75, label %114
75: ; preds = %72
%76 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%78 = call i32 @link(i8* %76, i8* %77)
%79 = icmp ne i32 %78, 0
br i1 %79, label %81, label %80
80: ; preds = %75
store i32 0, i32* %15
br label %114
81: ; preds = %75
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%83 = call i32 @getlockpid(i8* %82)
store i32 %83, i32* %9
%84 = load i32, i32* %9
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %81
%87 = call i32 @usleep(i32 500000)
%88 = load i32, i32* %14
%89 = add nsw i32 %88, 1
store i32 %89, i32* %14
br label %72
90: ; preds = %81
%91 = load i32, i32* %9
%92 = icmp eq i32 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %90
%94 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%95 = call i32 @unlink(i8* %94)
br label %110
96: ; preds = %90
%97 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%98 = load i32, i32* %9
%99 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %97, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %98)
%100 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%101 = call i32 @stat(i8* %100, %struct.stat* %13)
%102 = icmp ne i32 %101, 0
br i1 %102, label %104, label %103
103: ; preds = %96
store i32 1, i32* %15
br label %114
104: ; preds = %96
%105 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%106 = call i32 @logger(i32 0, i64 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %105)
%107 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%108 = call i32 @unlink(i8* %107)
br label %109
109: ; preds = %104
br label %110
110: ; preds = %109, %93
br label %111
111: ; preds = %110
%112 = load i32, i32* %14
%113 = add nsw i32 %112, 1
store i32 %113, i32* %14
br label %72
114: ; preds = %103, %80, %72
%115 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%116 = call i32 @unlink(i8* %115)
%117 = load i32, i32* %15
store i32 %117, i32* %4
br label %118
118: ; preds = %114, %53, %28, %19
%119 = load i32, i32* %4
ret i32 %119
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lockdir is not set\00"
@.str.1 = [10 x i8] c"%s/%d.lck\00"
@.str.2 = [9 x i8] c"%sXXXXXX\00"
@errno = global i64 0
@EROFS = global i64 0
@.str.3 = [53 x i8] c"Unable to create lock file %s, use --skiplock option\00"
@.str.4 = [41 x i8] c"Unable to create temporary lock file: %s\00"
@.str.5 = [7 x i8] c"%d\0A%s\0A\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c"/proc/%d\00"
@.str.8 = [28 x i8] c"Removing stale lock file %s\00"
define i32 @vps_lock(i32 %0, i8* %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
%7 = alloca %struct.stat
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = bitcast %struct.stat* %7 to i8*
%12 = tail call i64 @check_var(i8* %1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %100
14: ; preds = %3
%15 = tail call i32 @stat_file(i8* %1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i64 @make_dir(i8* %1, i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %100
20: ; preds = %17, %14
%21 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %9, i32 512, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %0)
%22 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%23 = call i32 @mkstemp(i8* %10)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EROFS
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @logger(i32 -1, i64 %26, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %100
31: ; preds = %25
%32 = call i32 @logger(i32 -1, i64 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %10)
br label %100
33: ; preds = %20
%34 = call i32 (...) @getpid()
%35 = icmp eq i8* %2, null
%36 = select i1 %35, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* %2
%37 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %34, i8* %36)
%38 = call i32 @strlen(i8* %8)
%39 = call i32 @write(i32 %23, i8* %8, i32 %38)
%40 = call i32 @close(i32 %23)
%41 = call i32 @link(i8* %10, i8* %9)
%42 = icmp eq i32 %41, 0
br i1 %42, label %97, label %43
43: ; preds = %33
%44 = call i32 @getlockpid(i8* %9)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %86
46: ; preds = %43
%47 = call i32 @usleep(i32 500000)
br label %48
48: ; preds = %94, %88, %46
%49 = call i32 @link(i8* %10, i8* %9)
%50 = icmp eq i32 %49, 0
br i1 %50, label %97, label %51
51: ; preds = %48
%52 = call i32 @getlockpid(i8* %9)
%53 = icmp slt i32 %52, 0
br i1 %53, label %65, label %54
54: ; preds = %51
%55 = icmp eq i32 %52, 0
br i1 %55, label %63, label %56
56: ; preds = %54
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %52)
%58 = call i32 @stat(i8* %8, %struct.stat* %7)
%59 = icmp eq i32 %58, 0
br i1 %59, label %97, label %60
60: ; preds = %56
%61 = call i32 @logger(i32 0, i64 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %9)
%62 = call i32 @unlink(i8* %9)
br label %67
63: ; preds = %54
%64 = call i32 @unlink(i8* %9)
br label %67
65: ; preds = %51
%66 = call i32 @usleep(i32 500000)
br label %67
67: ; preds = %60, %63, %65
%68 = call i32 @link(i8* %10, i8* %9)
%69 = icmp eq i32 %68, 0
br i1 %69, label %97, label %70
70: ; preds = %67
%71 = call i32 @getlockpid(i8* %9)
%72 = icmp slt i32 %71, 0
br i1 %72, label %84, label %73
73: ; preds = %70
%74 = icmp eq i32 %71, 0
br i1 %74, label %82, label %75
75: ; preds = %73
%76 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %71)
%77 = call i32 @stat(i8* %8, %struct.stat* %7)
%78 = icmp eq i32 %77, 0
br i1 %78, label %97, label %79
79: ; preds = %75
%80 = call i32 @logger(i32 0, i64 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %9)
%81 = call i32 @unlink(i8* %9)
br label %97
82: ; preds = %73
%83 = call i32 @unlink(i8* %9)
br label %97
84: ; preds = %70
%85 = call i32 @usleep(i32 500000)
br label %97
86: ; preds = %43
%87 = icmp eq i32 %44, 0
br i1 %87, label %88, label %90
88: ; preds = %86
%89 = call i32 @unlink(i8* %9)
br label %48
90: ; preds = %86
%91 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %44)
%92 = call i32 @stat(i8* %8, %struct.stat* %7)
%93 = icmp eq i32 %92, 0
br i1 %93, label %97, label %94
94: ; preds = %90
%95 = call i32 @logger(i32 0, i64 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %9)
%96 = call i32 @unlink(i8* %9)
br label %48
97: ; preds = %84, %82, %79, %75, %67, %56, %48, %90, %33
%98 = phi i32 [ 0, %33 ], [ 1, %90 ], [ 0, %48 ], [ 1, %56 ], [ 0, %67 ], [ 1, %75 ], [ -1, %79 ], [ -1, %82 ], [ -1, %84 ]
%99 = call i32 @unlink(i8* %10)
br label %100
100: ; preds = %29, %31, %17, %3, %97
%101 = phi i32 [ %98, %97 ], [ -1, %3 ], [ -1, %17 ], [ -1, %31 ], [ -1, %29 ]
ret i32 %101
}