#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 /*<<< orphan*/ F_LOCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int create_pidfile(char *pidfile) {
  FILE *f = fopen(pidfile, "w");
  if (f == NULL)
    return -1;
  fprintf(f, "%d", getpid());
  fclose(f);
  int fd;
  fd = open(pidfile, O_WRONLY);
  if (fd == -1 || lockf(fd, F_LOCK, 0) == -1)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<create_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <create_pidfile+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <create_pidfile+0xae>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11fc <create_pidfile+0x9c>
mov    -0x1c(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
jne    1208 <create_pidfile+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <create_pidfile+0xae>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pidfile>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11d4 <create_pidfile+0x74>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe73(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d4 <create_pidfile+0x74>
mov    %eax,%ebx
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@F_LOCK = global i32 0
define i32 @create_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %30
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 (...) @getpid()
%14 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32*, i32** %4
%16 = call i32 @fclose(i32* %15)
%17 = load i8*, i8** %3
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %27, label %22
22: ; preds = %11
%23 = load i32, i32* %5
%24 = load i32, i32* @F_LOCK
%25 = call i32 @lockf(i32 %23, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22, %11
store i32 -1, i32* %2
br label %30
28: ; preds = %22
%29 = load i32, i32* %5
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %27, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@F_LOCK = global i32 0
define i32 @create_pidfile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @fclose(i32* %2)
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = load i32, i32* @F_LOCK
%13 = tail call i32 @lockf(i32 %9, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
%15 = select i1 %14, i32 -1, i32 %9
br label %16
16: ; preds = %11, %4, %1
%17 = phi i32 [ -1, %1 ], [ -1, %4 ], [ %15, %11 ]
ret i32 %17
}