#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 scalar_t__ ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ flock(int, int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ memset(void *, int, int);
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, void const *, int /*<<< orphan*/);
int pidfile_create(const char *path, pid_t pid) {
  char pid_str[64];
  int fd = 0;
  ssize_t sz = 0;
  if ((int)pid == 0) {
    pid = getpid();
  }
  memset((void *)pid_str, (int)NULL, 64);
  snprintf(pid_str, (size_t)64, "%d", pid);
  fd = open(path, O_WRONLY | O_CREAT, ((S_IRUSR | S_IWUSR) & 0x0000));
  if (fd >= 0) {
    fchmod(fd, 0600);
    sz = write(fd, (const void *)pid_str, strlen(pid_str));
    flock(fd, LOCK_EX);
    close(fd);
    return (int)sz;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<pidfile_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x64(%rbp)
movq   $0x0,-0x70(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11c5 <pidfile_create+0x35>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x40,%esi
lea    0xe18(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2dc5(%rip),%rax
or     (%rax),%edx
and    $0x0,%edx
call   10a0 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jl     1285 <pidfile_create+0xf5>
mov    -0x64(%rbp),%edi
mov    $0x180,%esi
call   1090 <fchmod@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x70(%rbp)
mov    -0x64(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   10b0 <flock@plt>
mov    -0x64(%rbp),%edi
call   1080 <close@plt>
mov    -0x70(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    128c <pidfile_create+0xfc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
test   %esi,%esi
jne    11ac <pidfile_create+0x1c>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    0xe38(%rip),%rdx
mov    $0x40,%esi
mov    %rbx,%rdi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
mov    0x2dea(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1236 <pidfile_create+0xa6>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x180,%esi
call   1090 <fchmod@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%r15
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10b0 <flock@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r15d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@LOCK_EX = global i32 0
define i32 @pidfile_create(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i64 0, i64* %8
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @getpid()
store i32 %12, i32* %5
br label %13
13: ; preds = %11, %2
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 64)
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i32 @snprintf(i8* %16, i64 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = and i32 %25, 0
%27 = call i32 @open(i8* %19, i32 %22, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %13
%31 = load i32, i32* %7
%32 = call i32 @fchmod(i32 %31, i32 384)
%33 = load i32, i32* %7
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i64 @write(i32 %33, i8* %34, i32 %36)
store i64 %37, i64* %8
%38 = load i32, i32* %7
%39 = load i32, i32* @LOCK_EX
%40 = call i32 @flock(i32 %38, i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = load i64, i64* %8
%44 = trunc i64 %43 to i32
store i32 %44, i32* %3
br label %46
45: ; preds = %13
store i32 0, i32* %3
br label %46
46: ; preds = %45, %30
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@LOCK_EX = global i32 0
define i32 @pidfile_create(i8* nocapture %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @getpid()
br label %8
8: ; preds = %6, %2
%9 = phi i32 [ %7, %6 ], [ %1, %2 ]
%10 = call i32 @memset(i8* %4, i32 0, i32 64)
%11 = call i32 @snprintf(i8* %4, i64 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = call i32 @open(i8* %0, i32 %14, i32 0)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %25
17: ; preds = %8
%18 = call i32 @fchmod(i32 %15, i32 384)
%19 = call i32 @strlen(i8* %4)
%20 = call i64 @write(i32 %15, i8* %4, i32 %19)
%21 = load i32, i32* @LOCK_EX
%22 = call i32 @flock(i32 %15, i32 %21)
%23 = call i32 @close(i32 %15)
%24 = trunc i64 %20 to i32
br label %25
25: ; preds = %8, %17
%26 = phi i32 [ %24, %17 ], [ 0, %8 ]
ret i32 %26
}