#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 int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ F_TLOCK;
int O_CLOEXEC;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ 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 snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int create_pidfile(const char *path) {
  int fd = open(path, O_WRONLY | O_CREAT | 0 | O_CLOEXEC, 0644);
  if (fd == -1)
    return -1;
  struct stat s;
  char buf[4 * sizeof(int)];
  int len = snprintf(buf, sizeof(buf), "%u", (unsigned)getpid());
  if (fstat(fd, &s) == 0 && (errno = EACCES, S_ISREG(s.st_mode)) &&
      lockf(fd, F_TLOCK, 0) == 0 && ftruncate(fd, 0) == 0 &&
      write(fd, buf, len) == len && fdatasync(fd) == 0)
    return fd;
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<create_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f1 <create_pidfile+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <create_pidfile+0x12a>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    $0x10,%esi
lea    0xdee(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jne    12bb <create_pidfile+0x11b>
mov    0x2da1(%rip),%rax
mov    (%rax),%rcx
mov    0x2d87(%rip),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%edi
call   10a0 <S_ISREG@plt>
cmp    $0x0,%eax
je     12bb <create_pidfile+0x11b>
mov    -0x14(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <lockf@plt>
cmp    $0x0,%rax
jne    12bb <create_pidfile+0x11b>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
jne    12bb <create_pidfile+0x11b>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <write@plt>
cmp    -0x34(%rbp),%eax
jne    12bb <create_pidfile+0x11b>
mov    -0x14(%rbp),%edi
call   1030 <fdatasync@plt>
cmp    $0x0,%rax
jne    12bb <create_pidfile+0x11b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <create_pidfile+0x12a>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pidfile>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e0b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     120b <create_pidfile+0x6b>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe20(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
je     1219 <create_pidfile+0x79>
mov    %ebx,%edi
call   1090 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    0x2d9e(%rip),%rcx
mov    %rax,(%rcx)
mov    0x8(%rsp),%edi
call   10a0 <S_ISREG@plt>
test   %eax,%eax
je     1204 <create_pidfile+0x64>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <lockf@plt>
test   %rax,%rax
jne    1204 <create_pidfile+0x64>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
jne    1204 <create_pidfile+0x64>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    1204 <create_pidfile+0x64>
mov    %ebx,%edi
call   1030 <fdatasync@plt>
test   %rax,%rax
jne    1204 <create_pidfile+0x64>
jmp    1210 <create_pidfile+0x70>

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

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