#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 flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CLOEXEC;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int ftruncate(int, int /*<<< orphan*/);
int getpid();
int open(char const *, int, int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ unlink(char const *);
int write(int, char *, int);
int getlock(const char *path) {
  char buf[16];
  struct flock fl;
  int n, fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
  if (fd == -1)
    return -1;
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    close(fd);
    return -1;
  }
  n = snprintf(buf, sizeof(buf), "%d\n", getpid());
  if (ftruncate(fd, 0) == -1 || write(fd, buf, n) != n) {
    close(fd);
    unlink(path);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    11ce <getlock+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <getlock+0x120>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1228 <getlock+0xa8>
mov    -0x40(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <getlock+0x120>
lea    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x10,%esi
lea    0xdb7(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     127c <getlock+0xfc>
mov    -0x40(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <write@plt>
cmp    -0x3c(%rbp),%eax
je     1299 <getlock+0x119>
mov    -0x40(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <getlock+0x120>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e4f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     125e <getlock+0xde>
mov    %eax,%ebx
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1257 <getlock+0xd7>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdf5(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x10,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %eax,%r15d
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1241 <getlock+0xc1>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
call   1050 <write@plt>
cmp    %r15d,%eax
je     125e <getlock+0xde>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
jmp    125e <getlock+0xde>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @getlock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [16 x i8]
%5 = alloca %struct.flock
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_CLOEXEC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13, i32 420)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %52
18: ; preds = %1
%19 = load i32, i32* @F_WRLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %19, i32* %20
%21 = load i32, i32* @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i64 0, i64* %24
%25 = load i32, i32* %7
%26 = load i32, i32* @F_SETLK
%27 = call i32 @fcntl(i32 %25, i32 %26, %struct.flock* %5)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %52
32: ; preds = %18
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%34 = call i32 (...) @getpid()
%35 = call i32 @snprintf(i8* %33, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %7
%37 = call i32 @ftruncate(i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %46, label %39
39: ; preds = %32
%40 = load i32, i32* %7
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%42 = load i32, i32* %6
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %6
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %39, %32
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = load i8*, i8** %3
%50 = call i32 @unlink(i8* %49)
store i32 -1, i32* %2
br label %52
51: ; preds = %39
store i32 0, i32* %2
br label %52
52: ; preds = %51, %46, %29, %17
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @getlock(i8* nocapture %0) {
%2 = alloca [16 x i8]
%3 = alloca %struct.flock
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.flock* %3 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_CLOEXEC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %35, label %13
13: ; preds = %1
%14 = load i32, i32* @F_WRLCK
%15 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @SEEK_SET
%17 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = bitcast %struct.flock* %3 to i8*
%19 = load i32, i32* @F_SETLK
%20 = call i32 @fcntl(i32 %11, i32 %19, %struct.flock* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @close(i32 %11)
br label %35
24: ; preds = %13
%25 = call i32 (...) @getpid()
%26 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 @ftruncate(i32 %11, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = call i32 @write(i32 %11, i8* %4, i32 %26)
%31 = icmp eq i32 %30, %26
br i1 %31, label %35, label %32
32: ; preds = %29, %24
%33 = call i32 @close(i32 %11)
%34 = call i32 @unlink(i8* %0)
br label %35
35: ; preds = %29, %1, %32, %22
%36 = phi i32 [ -1, %22 ], [ -1, %32 ], [ -1, %1 ], [ 0, %29 ]
ret i32 %36
}