#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 /*<<< orphan*/ st_mode;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ F_TLOCK;
int O_CREAT;
int O_WRONLY;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fdatasync(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
size_t strlen(char *);
scalar_t__ write(int, char *, size_t);
int create_pidfile(const char *path) {
  char buf[20];
  size_t len;
  int fd;
  (void)snprintf(buf, sizeof(buf), "%d", (int)getpid());
  buf[sizeof(buf) - 1] = '\0';
  len = strlen(buf);
  fd = open(path, O_WRONLY | O_CREAT | 0, 0644);
  if (fd != -1) {
    struct stat s;
    errno = 0;
    if ((fstat(fd, &s) == 0) && S_ISREG(s.st_mode) &&
        (lockf(fd, F_TLOCK, 0) == 0) && (ftruncate(fd, 0) == 0) &&
        (write(fd, buf, len) == (ssize_t)len) && (fdatasync(fd) == 0))
      return fd;
    if (errno == 0)
      errno = EACCES;
    (void)close(fd);
  }
  return -1;
}


x86 assembly (clang, -O0):
<create_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x14,%esi
lea    0xe23(%rip),%rdx
call   1070 <snprintf@plt>
movb   $0x0,-0x1d(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
je     12ee <create_pidfile+0x13e>
mov    0x2d9e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x3c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
jne    12c1 <create_pidfile+0x111>
mov    -0x40(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%rax
je     12c1 <create_pidfile+0x111>
mov    -0x3c(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <lockf@plt>
cmp    $0x0,%rax
jne    12c1 <create_pidfile+0x111>
mov    -0x3c(%rbp),%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
cmp    $0x0,%rax
jne    12c1 <create_pidfile+0x111>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x38(%rbp),%rax
jne    12c1 <create_pidfile+0x111>
mov    -0x3c(%rbp),%edi
call   1030 <fdatasync@plt>
cmp    $0x0,%rax
jne    12c1 <create_pidfile+0x111>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <create_pidfile+0x145>
mov    0x2d00(%rip),%rax
cmpq   $0x0,(%rax)
jne    12e6 <create_pidfile+0x136>
mov    0x2cff(%rip),%rax
mov    (%rax),%rcx
mov    0x2ce5(%rip),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%edi
call   10a0 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pidfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe35(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x14,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
movb   $0x0,0x23(%rsp)
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rax,%r14
mov    0x2dc2(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1253 <create_pidfile+0xa3>
mov    %eax,%ebp
mov    0x2da7(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
test   %rax,%rax
je     1260 <create_pidfile+0xb0>
cmpq   $0x0,(%r15)
jne    124c <create_pidfile+0x9c>
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   10b0 <S_ISREG@plt>
test   %rax,%rax
je     1239 <create_pidfile+0x89>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1090 <lockf@plt>
test   %rax,%rax
jne    1239 <create_pidfile+0x89>
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
test   %rax,%rax
jne    1239 <create_pidfile+0x89>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
call   1040 <write@plt>
cmp    %r14,%rax
jne    1239 <create_pidfile+0x89>
mov    %ebp,%edi
call   1030 <fdatasync@plt>
test   %rax,%rax
jne    1239 <create_pidfile+0x89>
mov    %ebp,%ebx
jmp    1253 <create_pidfile+0xa3>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@F_TLOCK = global i32 0
@EACCES = global i64 0
define i32 @create_pidfile(i8* nocapture %0) {
%2 = alloca [20 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = tail call i64 (...) @getpid()
%6 = trunc i64 %5 to i32
%7 = call i32 @snprintf(i8* %4, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 19
store i8 0, i8* %8
%9 = call i64 @strlen(i8* %4)
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %0, i32 %12, i32 420)
%14 = icmp eq i32 %13, -1
br i1 %14, label %45, label %15
15: ; preds = %1
%16 = bitcast %struct.stat* %3 to i8*
store i64 0, i64* @errno
%17 = call i64 @fstat(i32 %13, %struct.stat* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISREG(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %19
%25 = load i32, i32* @F_TLOCK
%26 = call i64 @lockf(i32 %13, i32 %25, i32 0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = call i64 @ftruncate(i32 %13, i32 0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = call i64 @write(i32 %13, i8* %4, i64 %9)
%33 = icmp eq i64 %32, %9
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i64 @fdatasync(i32 %13)
%36 = icmp eq i64 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %34, %31, %28, %24, %19, %15
%38 = load i64, i64* @errno
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load i64, i64* @EACCES
store i64 %41, i64* @errno
br label %42
42: ; preds = %40, %37
%43 = call i32 @close(i32 %13)
br label %45
44: ; preds = %34
br label %45
45: ; preds = %1, %42, %44
%46 = phi i32 [ %13, %44 ], [ -1, %42 ], [ -1, %1 ]
ret i32 %46
}