#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 */

/* Variables and functions */
int EACCES;
int EAGAIN;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int ftruncate(int, int /*<<< orphan*/);
int lock_file(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
              int /*<<< orphan*/);
int open(char const *, int, int);
int create_pid_file(const char *path) {
  int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (-1 == fd) {
    return -1;
  }
  if (lock_file(fd, F_WRLCK, SEEK_SET, 0, 0) == -1) {
    int e = errno;
    close(fd);
    errno = e;
    if (e == EAGAIN || e == EACCES) {
      return -2;
    }
    return -1;
  }
  if (ftruncate(fd, 0) == -1) {
    int e = errno;
    close(fd);
    errno = e;
    return -1;
  }
  if (lock_file(fd, F_UNLCK, SEEK_SET, 0, 0) == -1) {
    assert(0);
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11a6 <create_pid_file+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <create_pid_file+0x150>
mov    -0x14(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1030 <lock_file@plt>
cmp    $0xffffffff,%eax
jne    122b <create_pid_file+0xdb>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2dcb(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
je     1213 <create_pid_file+0xc3>
mov    -0x18(%rbp),%eax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%eax
jne    121f <create_pid_file+0xcf>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a0 <create_pid_file+0x150>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <create_pid_file+0x150>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    126a <create_pid_file+0x11a>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d5c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <create_pid_file+0x150>
mov    -0x14(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1030 <lock_file@plt>
cmp    $0xffffffff,%eax
jne    129a <create_pid_file+0x14a>
xor    %edi,%edi
call   1060 <assert@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e82(%rip),%rax
mov    0x2e93(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e38(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1233 <create_pid_file+0xe3>
mov    %eax,%ebx
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%r14
mov    (%r14),%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <lock_file@plt>
cmp    $0xffffffff,%eax
je     11ea <create_pid_file+0x9a>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     121f <create_pid_file+0xcf>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <lock_file@plt>
cmp    $0xffffffff,%eax
jne    11e6 <create_pid_file+0x96>
xor    %edi,%edi
call   1060 <assert@plt>
mov    %ebx,%ebp
jmp    1233 <create_pid_file+0xe3>
mov    0x2dc7(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,(%r14)
mov    0x2d8b(%rip),%rax
cmp    (%rax),%ebp
sete   %al
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%ebp
sete   %cl
or     %al,%cl
movzbl %cl,%ebp
not    %ebp
jmp    1233 <create_pid_file+0xe3>
mov    0x2d92(%rip),%r14
mov    (%r14),%r15d
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
@EACCES = global i32 0
@F_UNLCK = global i32 0
define i32 @create_pid_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %7, i32 %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %57
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @F_WRLCK
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lock_file(i32 %19, i32 %20, i32 %21, i32 0, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %38
24: ; preds = %18
%25 = load i32, i32* @errno
store i32 %25, i32* %5
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %5
store i32 %28, i32* @errno
%29 = load i32, i32* %5
%30 = load i32, i32* @EAGAIN
%31 = icmp eq i32 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %24
%33 = load i32, i32* %5
%34 = load i32, i32* @EACCES
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32, %24
store i32 -2, i32* %2
br label %57
37: ; preds = %32
store i32 -1, i32* %2
br label %57
38: ; preds = %18
%39 = load i32, i32* %4
%40 = call i32 @ftruncate(i32 %39, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @errno
store i32 %43, i32* %6
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %6
store i32 %46, i32* @errno
store i32 -1, i32* %2
br label %57
47: ; preds = %38
%48 = load i32, i32* %4
%49 = load i32, i32* @F_UNLCK
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @lock_file(i32 %48, i32 %49, i32 %50, i32 0, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @assert(i32 0)
br label %55
55: ; preds = %53, %47
%56 = load i32, i32* %4
store i32 %56, i32* %2
br label %57
57: ; preds = %55, %42, %37, %36, %17
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
@EACCES = global i32 0
@F_UNLCK = global i32 0
define i32 @create_pid_file(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %37, label %10
10: ; preds = %1
%11 = load i32, i32* @F_WRLCK
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lock_file(i32 %8, i32 %11, i32 %12, i32 0, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i32 @close(i32 %8)
store i32 %16, i32* @errno
%18 = load i32, i32* @EAGAIN
%19 = icmp eq i32 %16, %18
%20 = load i32, i32* @EACCES
%21 = icmp eq i32 %16, %20
%22 = select i1 %19, i1 true, i1 %21
%23 = select i1 %22, i32 -2, i32 -1
br label %37
24: ; preds = %10
%25 = tail call i32 @ftruncate(i32 %8, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 @close(i32 %8)
store i32 %28, i32* @errno
br label %37
30: ; preds = %24
%31 = load i32, i32* @F_UNLCK
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lock_file(i32 %8, i32 %31, i32 %32, i32 0, i32 0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = tail call i32 @assert(i32 0)
br label %37
37: ; preds = %30, %35, %1, %27, %15
%38 = phi i32 [ %23, %15 ], [ -1, %27 ], [ -1, %1 ], [ %8, %35 ], [ %8, %30 ]
ret i32 %38
}