#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;

/* Variables and functions */
int /*<<< orphan*/ F_TLOCK;
int O_CLOEXEC;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ ftruncate(int, int /*<<< orphan*/);
int getpid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int pidlock(const char *lockpath) {
  char text[32];
  pid_t pid = getpid();
  int fd;
  fd = open(lockpath, O_RDWR | O_CLOEXEC | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd < 0)
    goto err_ret;
  if (lockf(fd, F_TLOCK, 0) < 0)
    goto err_fd;
  if (ftruncate(fd, 0) < 0)
    goto err_fd;
  sprintf(text, "%i\n", pid);
  if (write(fd, text, strlen(text)) < 0)
    goto err_fd;
  return fd;
err_fd:
  close(fd);
err_ret:
  if (errno >= 0)
    return -1;
  return -errno;
}


x86 assembly (clang, -O0):
<pidlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    0x2e0b(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11de <pidlock+0x5e>
jmp    1273 <pidlock+0xf3>
mov    -0x38(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <lockf@plt>
cmp    $0x0,%rax
jge    1200 <pidlock+0x80>
jmp    126b <pidlock+0xeb>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
jge    1219 <pidlock+0x99>
jmp    126b <pidlock+0xeb>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xdd9(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1260 <pidlock+0xe0>
jmp    126b <pidlock+0xeb>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129d <pidlock+0x11d>
mov    -0x38(%rbp),%edi
call   1080 <close@plt>
mov    0x2d46(%rip),%rax
cmpl   $0x0,(%rax)
jl     128f <pidlock+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <pidlock+0x11d>
xor    %eax,%eax
mov    0x2d28(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidlock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    0x2e3d(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    0x2e02(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1228 <pidlock+0xa8>
mov    %eax,%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <lockf@plt>
test   %rax,%rax
js     1221 <pidlock+0xa1>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
js     1221 <pidlock+0xa1>
lea    0xe05(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    123f <pidlock+0xbf>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
neg    %ecx
test   %eax,%eax
mov    $0xffffffff,%ebx
cmovs  %ecx,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_TLOCK = global i32 0
@.str = [4 x i8] c"%i\0A\00"
@errno = global i32 0
define i32 @pidlock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 (...) @getpid()
store i32 %7, i32* %5
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %9, %10
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %8, i32 %13, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
br label %48
21: ; preds = %1
%22 = load i32, i32* %6
%23 = load i32, i32* @F_TLOCK
%24 = call i64 @lockf(i32 %22, i32 %23, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %45
27: ; preds = %21
%28 = load i32, i32* %6
%29 = call i64 @ftruncate(i32 %28, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %45
32: ; preds = %27
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %6
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = call i64 @write(i32 %36, i8* %37, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %32
br label %45
43: ; preds = %32
%44 = load i32, i32* %6
store i32 %44, i32* %2
br label %55
45: ; preds = %42, %31, %26
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %20
%49 = load i32, i32* @errno
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 -1, i32* %2
br label %55
52: ; preds = %48
%53 = load i32, i32* @errno
%54 = sub nsw i32 0, %53
store i32 %54, i32* %2
br label %55
55: ; preds = %52, %51, %43
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_TLOCK = global i32 0
@.str = [4 x i8] c"%i\0A\00"
@errno = global i32 0
define i32 @pidlock(i8* nocapture %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %6, %5
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %0, i32 %9, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %1
%16 = load i32, i32* @F_TLOCK
%17 = tail call i64 @lockf(i32 %13, i32 %16, i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = tail call i64 @ftruncate(i32 %13, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4)
%24 = call i32 @strlen(i8* %3)
%25 = call i64 @write(i32 %13, i8* %3, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %22, %19, %15
%28 = call i32 @close(i32 %13)
br label %29
29: ; preds = %1, %27
%30 = load i32, i32* @errno
%31 = icmp sgt i32 %30, -1
%32 = sub nsw i32 0, %30
%33 = select i1 %31, i32 -1, i32 %32
br label %34
34: ; preds = %29, %22
%35 = phi i32 [ %13, %22 ], [ %33, %29 ]
ret i32 %35
}