#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("%s, ", "error");
      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    1201 <pidfile_acquire+0xc1>
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    11e8 <pidfile_acquire+0xa8>
jmp    1195 <pidfile_acquire+0x55>
lea    0xe64(%rip),%rdi
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fb <pidfile_acquire+0xbb>
mov    -0x14(%rbp),%edi
mov    0x2dee(%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    11d9 <pidfile_acquire+0x89>
mov    $0xffffffff,%ebp
jmp    11d9 <pidfile_acquire+0x89>
lea    0xe61(%rip),%rdi
lea    0xe5f(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe49(%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 = [5 x i8] c"%s, \00"
@.str.1 = [6 x i8] c"error\00"
@.str.2 = [31 x i8] c"Unable to open pidfile %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [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 %31
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 %25
16: ; preds = %8
br label %17
17: ; preds = %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %3
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %17
br label %29
25: ; preds = %8
%26 = load i32, i32* %4
%27 = load i32, i32* @F_LOCK
%28 = call i32 @lockf(i32 %26, i32 %27, i32 0)
br label %29
29: ; preds = %25, %24
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %7
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [5 x i8] c"%s, \00"
@.str.1 = [6 x i8] c"error\00"
@.str.2 = [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 %18, 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 %15
9: ; preds = %3
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %12)
%14 = tail call i32 @putchar(i32 10)
br label %18
15: ; preds = %3
%16 = load i32, i32* @F_LOCK
%17 = tail call i32 @lockf(i32 %7, i32 %16, i32 0)
br label %18
18: ; preds = %9, %15, %1
%19 = phi i32 [ -1, %1 ], [ %7, %15 ], [ %7, %9 ]
ret i32 %19
}