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

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int LOCK_UN;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int open(char *, int, int);
int pce_pidfile_create(char *pathname) {
  FILE *f;
  int fd;
  int pid;
  fd = open(pathname, O_RDWR | O_CREAT | O_TRUNC,
            S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
  if (fd == -1)
    return 0;
  if (flock(fd, LOCK_EX | LOCK_NB) == -1)
    return 0;
  pid = getpid();
  f = fdopen(fd, "r+");
  fprintf(f, "%d\n", pid);
  fflush(f);
  flock(fd, LOCK_UN);
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<pce_pidfile_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11dd <pce_pidfile_create+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    1264 <pce_pidfile_create+0xf4>
mov    -0x1c(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
call   1090 <flock@plt>
cmp    $0xffffffff,%eax
jne    120c <pce_pidfile_create+0x9c>
movl   $0x0,-0x4(%rbp)
jmp    1264 <pce_pidfile_create+0xf4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xde0(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xdcc(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pce_pidfile_create>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e5d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
mov    0x2e32(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e10(%rip),%rax
or     (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     1229 <pce_pidfile_create+0xb9>
mov    %eax,%ebp
mov    0x2e08(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1090 <flock@plt>
cmp    $0xffffffff,%eax
je     1229 <pce_pidfile_create+0xb9>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
lea    0xe11(%rip),%rsi
mov    %ebp,%edi
call   1070 <fdopen@plt>
mov    %rax,%r14
lea    0xe03(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1060 <fflush@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1090 <flock@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str = [3 x i8] c"r+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@LOCK_UN = global i32 0
define i32 @pce_pidfile_create(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* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWUSR
%14 = load i32, i32* @S_IRUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %7, i32 %12, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %1
store i32 0, i32* %2
br label %47
24: ; preds = %1
%25 = load i32, i32* %5
%26 = load i32, i32* @LOCK_EX
%27 = load i32, i32* @LOCK_NB
%28 = or i32 %26, %27
%29 = call i32 @flock(i32 %25, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 0, i32* %2
br label %47
32: ; preds = %24
%33 = call i32 (...) @getpid()
store i32 %33, i32* %6
%34 = load i32, i32* %5
%35 = call i32* @fdopen(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %35, i32** %4
%36 = load i32*, i32** %4
%37 = load i32, i32* %6
%38 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32*, i32** %4
%40 = call i32 @fflush(i32* %39)
%41 = load i32, i32* %5
%42 = load i32, i32* @LOCK_UN
%43 = call i32 @flock(i32 %41, i32 %42)
%44 = load i32, i32* %5
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %6
store i32 %46, i32* %2
br label %47
47: ; preds = %32, %31, %23
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str = [3 x i8] c"r+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@LOCK_UN = global i32 0
define i32 @pce_pidfile_create(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IWUSR
%8 = load i32, i32* @S_IRUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %6, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %30, label %16
16: ; preds = %1
%17 = load i32, i32* @LOCK_EX
%18 = load i32, i32* @LOCK_NB
%19 = or i32 %18, %17
%20 = tail call i32 @flock(i32 %14, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %30, label %22
22: ; preds = %16
%23 = tail call i32 (...) @getpid()
%24 = tail call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%25 = tail call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%26 = tail call i32 @fflush(i32* %24)
%27 = load i32, i32* @LOCK_UN
%28 = tail call i32 @flock(i32 %14, i32 %27)
%29 = tail call i32 @close(i32 %14)
br label %30
30: ; preds = %16, %1, %22
%31 = phi i32 [ %23, %22 ], [ 0, %1 ], [ 0, %16 ]
ret i32 %31
}