#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 /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_TLOCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int const, char *, long);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int create_pid_file(const char *pid_file_name) {
  int fd;
  const int BUF_SIZE = 32;
  char buf[BUF_SIZE];
  if (!pid_file_name) {
    errno = EINVAL;
    return -1;
  }
  fd = open(pid_file_name, O_RDWR | O_CREAT, 0644);
  if (fd == -1)
    return -1;
  if (lockf(fd, F_TLOCK, 0) == -1) {
    close(fd);
    return -1;
  }
  if (ftruncate(fd, 0) != 0) {
    close(fd);
    return -1;
  }
  snprintf(buf, BUF_SIZE, "%ld\n", (long)getpid());
  if (write(fd, buf, strlen(buf)) != (int)strlen(buf)) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x20,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ca <create_pid_file+0x4a>
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    0x2e13(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12ef <create_pid_file+0x16f>
mov    -0x10(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120a <create_pid_file+0x8a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12ef <create_pid_file+0x16f>
mov    -0x14(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <lockf@plt>
cmp    $0xffffffff,%eax
jne    1241 <create_pid_file+0xc1>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12ef <create_pid_file+0x16f>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
je     1270 <create_pid_file+0xf0>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12ef <create_pid_file+0x16f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
lea    -0x50(%rbp),%rdi
mov    $0x20,%esi
lea    0xd76(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    -0x50(%rbp),%rsi
call   1030 <write@plt>
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
je     12e2 <create_pid_file+0x162>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12ef <create_pid_file+0x16f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     122e <create_pid_file+0xae>
mov    0x2e3e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     124e <create_pid_file+0xce>
mov    %eax,%ebp
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <lockf@plt>
cmp    $0xffffffff,%eax
je     1247 <create_pid_file+0xc7>
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
jne    1247 <create_pid_file+0xc7>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0e(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %eax,%r15d
jne    1247 <create_pid_file+0xc7>
mov    %ebp,%ebx
jmp    124e <create_pid_file+0xce>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    0x2d8a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    124e <create_pid_file+0xce>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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