#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 */

/* Variables and functions */
int /*<<< orphan*/ F_LOCK;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int pidfile_acquire(char *pidfile) {
  int pid_fd;
  if (pidfile == NULL)
    return -1;
  pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
  if (pid_fd < 0) {
    do {
      printf("Unable to open pidfile %s: %s\n", pidfile, strerror(errno));
      printf("\n");
    } while (0);
  } else {
    lockf(pid_fd, F_LOCK, 0);
  }
  return pid_fd;
}


x86 assembly (clang, -O0):
<pidfile_acquire>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <pidfile_acquire+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <pidfile_acquire+0xac>
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d3 <pidfile_acquire+0x93>
jmp    1195 <pidfile_acquire+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e6 <pidfile_acquire+0xa6>
mov    -0x14(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_acquire>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1191 <pidfile_acquire+0x41>
mov    %rdi,%rbx
mov    0x2e96(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1198 <pidfile_acquire+0x48>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
jmp    11c4 <pidfile_acquire+0x74>
mov    $0xffffffff,%ebp
jmp    11c4 <pidfile_acquire+0x74>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [31 x i8] c"Unable to open pidfile %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@F_LOCK = global i32 0
define i32 @pidfile_acquire(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 -1, i32* %2
br label %30
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12, i32 420)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %8
br label %17
17: ; preds = %16
%18 = load i8*, i8** %3
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %17
br label %28
24: ; preds = %8
%25 = load i32, i32* %4
%26 = load i32, i32* @F_LOCK
%27 = call i32 @lockf(i32 %25, i32 %26, i32 0)
br label %28
28: ; preds = %24, %23
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %7
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [31 x i8] c"Unable to open pidfile %s: %s\0A\00"
@errno = global i32 0
@F_LOCK = global i32 0
define i32 @pidfile_acquire(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
%13 = tail call i32 @putchar(i32 10)
br label %17
14: ; preds = %3
%15 = load i32, i32* @F_LOCK
%16 = tail call i32 @lockf(i32 %7, i32 %15, i32 0)
br label %17
17: ; preds = %9, %14, %1
%18 = phi i32 [ -1, %1 ], [ %7, %14 ], [ %7, %9 ]
ret i32 %18
}