#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 */
struct stat {
  int st_nlink;
};

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ getpid();
int /*<<< orphan*/ link(char *, char const *);
int open(char *, int, int);
char *os_DirName(char const *);
char *os_NodeName();
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ unlink(char *);
char *xmalloc(scalar_t__);
int os_SetLock(char const *path, int perms, int left_open) {
  char *dirname = os_DirName(path);
  char *hostname = os_NodeName();
  char *uniq_name = 0;
  int fd = -1;
  int code = 0;
  int file_flag = 0;
  struct stat buf;
  uniq_name = xmalloc(strlen(dirname) + strlen(hostname) + 32);
  sprintf(uniq_name, "%s/LCK_%s_%d", dirname, hostname, (int)getpid());
  if ((fd = open(uniq_name, O_WRONLY | O_CREAT, perms)) < 0) {
    code = -2;
    goto free_resources;
  }
  file_flag = 1;
  link(uniq_name, path);
  if (fstat(fd, &buf) < 0) {
    code = -2;
    goto free_resources;
  }
  if (buf.st_nlink != 2) {
    code = -1;
    goto free_resources;
  }
  if (left_open) {
    code = fd;
    fd = -1;
  }
free_resources:
  if (file_flag)
    unlink(uniq_name);
  free(uniq_name);
  free(dirname);
  if (fd >= 0)
    close(fd);
  return code;
}


x86 assembly (clang, -O0):
<os_SetLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <os_DirName@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <os_NodeName@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x58(%rbp),%rdi
add    %rax,%rdi
add    $0x20,%rdi
call   1080 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
lea    0xd9a(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
mov    -0xc(%rbp),%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    12a1 <os_SetLock+0xe1>
movl   $0xfffffffe,-0x30(%rbp)
jmp    1309 <os_SetLock+0x149>
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10d0 <link@plt>
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0x0,%rax
jge    12d7 <os_SetLock+0x117>
movl   $0xfffffffe,-0x30(%rbp)
jmp    1309 <os_SetLock+0x149>
cmpl   $0x2,-0x38(%rbp)
je     12ed <os_SetLock+0x12d>
movl   $0xffffffff,-0x30(%rbp)
jmp    1309 <os_SetLock+0x149>
cmpl   $0x0,-0x10(%rbp)
je     1304 <os_SetLock+0x144>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
jmp    1309 <os_SetLock+0x149>
cmpl   $0x0,-0x34(%rbp)
je     131c <os_SetLock+0x15c>
mov    -0x28(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x2c(%rbp)
jl     1340 <os_SetLock+0x180>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_SetLock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r13d
mov    %rdi,%r12
call   1050 <os_DirName@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10a0 <os_NodeName@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x20,%rdi
call   1080 <xmalloc@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xde9(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
call   10c0 <sprintf@plt>
mov    0x2da1(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    %r13d,%edx
call   10b0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1288 <os_SetLock+0xc8>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   10e0 <fstat@plt>
test   %rax,%rax
js     1290 <os_SetLock+0xd0>
mov    $0xffffffff,%r12d
cmpl   $0x2,(%rsp)
jne    1296 <os_SetLock+0xd6>
mov    %r14d,%eax
neg    %eax
sbb    %eax,%eax
or     %ebx,%eax
test   %r14d,%r14d
mov    %ebx,%r12d
cmove  %r14d,%r12d
mov    %eax,%ebx
jmp    1296 <os_SetLock+0xd6>
mov    $0xfffffffe,%r12d
jmp    129e <os_SetLock+0xde>
mov    $0xfffffffe,%r12d
mov    %rbp,%rdi
call   1040 <unlink@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     12b9 <os_SetLock+0xf9>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"%s/LCK_%s_%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @os_SetLock(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%14 = load i8*, i8** %4
%15 = call i8* @os_DirName(i8* %14)
store i8* %15, i8** %7
%16 = call i8* (...) @os_NodeName()
store i8* %16, i8** %8
store i8* null, i8** %9
store i32 -1, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%17 = load i8*, i8** %7
%18 = call i64 @strlen(i8* %17)
%19 = load i8*, i8** %8
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %18, %20
%22 = add nsw i64 %21, 32
%23 = call i8* @xmalloc(i64 %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = load i8*, i8** %7
%26 = load i8*, i8** %8
%27 = call i64 (...) @getpid()
%28 = trunc i64 %27 to i32
%29 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26, i32 %28)
%30 = load i8*, i8** %9
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = load i32, i32* %5
%35 = call i32 @open(i8* %30, i32 %33, i32 %34)
store i32 %35, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %3
store i32 -2, i32* %11
br label %57
38: ; preds = %3
store i32 1, i32* %12
%39 = load i8*, i8** %9
%40 = load i8*, i8** %4
%41 = call i32 @link(i8* %39, i8* %40)
%42 = load i32, i32* %10
%43 = call i64 @fstat(i32 %42, %struct.stat* %13)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %38
store i32 -2, i32* %11
br label %57
46: ; preds = %38
%47 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp ne i32 %48, 2
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 -1, i32* %11
br label %57
51: ; preds = %46
%52 = load i32, i32* %6
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %10
store i32 %55, i32* %11
store i32 -1, i32* %10
br label %56
56: ; preds = %54, %51
br label %57
57: ; preds = %56, %50, %45, %37
%58 = load i32, i32* %12
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i8*, i8** %9
%62 = call i32 @unlink(i8* %61)
br label %63
63: ; preds = %60, %57
%64 = load i8*, i8** %9
%65 = call i32 @free(i8* %64)
%66 = load i8*, i8** %7
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* %10
%69 = icmp sge i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %63
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %63
%74 = load i32, i32* %11
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"%s/LCK_%s_%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @os_SetLock(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = tail call i8* @os_DirName(i8* %0)
%6 = tail call i8* (...) @os_NodeName()
%7 = bitcast %struct.stat* %4 to i8*
%8 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%10 = add i64 %8, 32
%11 = add i64 %10, %9
%12 = tail call i8* @xmalloc(i64 %11)
%13 = tail call i64 (...) @getpid()
%14 = trunc i64 %13 to i32
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %6, i32 %14)
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %12, i32 %18, i32 %1)
%20 = icmp slt i32 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %3
%22 = tail call i32 @link(i8* %12, i8* %0)
%23 = call i64 @fstat(i32 %19, %struct.stat* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = icmp eq i32 %2, 0
%31 = select i1 %30, i32 %19, i32 -1
%32 = select i1 %30, i32 0, i32 %19
br label %33
33: ; preds = %21, %25, %29
%34 = phi i32 [ %31, %29 ], [ %19, %25 ], [ %19, %21 ]
%35 = phi i32 [ %32, %29 ], [ -1, %25 ], [ -2, %21 ]
%36 = tail call i32 @unlink(i8* %12)
br label %37
37: ; preds = %3, %33
%38 = phi i32 [ %35, %33 ], [ -2, %3 ]
%39 = phi i32 [ %34, %33 ], [ %19, %3 ]
%40 = tail call i32 @free(i8* %12)
%41 = tail call i32 @free(i8* %5)
%42 = icmp sgt i32 %39, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = tail call i32 @close(i32 %39)
br label %45
45: ; preds = %43, %37
ret i32 %38
}