#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 */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
scalar_t__ ENOENT;
int /*<<< orphan*/ F_TLOCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int check_pidfile(char *pidfile) {
  int fd;
  fd = open(pidfile, O_WRONLY);
  if (fd == -1) {
    if (errno == ENOENT)
      return 1;
    return -1;
  }
  int ret;
  ret = lockf(fd, F_TLOCK, 0);
  close(fd);
  if (ret == 0)
    return 1;
  if (errno == EACCES || errno == EAGAIN)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<check_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118d <check_pidfile+0x5d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
jne    1181 <check_pidfile+0x51>
movl   $0x1,-0x4(%rbp)
jmp    1208 <check_pidfile+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <check_pidfile+0xd8>
mov    -0x14(%rbp),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lockf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11c1 <check_pidfile+0x91>
movl   $0x1,-0x4(%rbp)
jmp    1208 <check_pidfile+0xd8>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <check_pidfile+0xc5>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <check_pidfile+0xd1>
movl   $0x0,-0x4(%rbp)
jmp    1208 <check_pidfile+0xd8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pidfile>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1194 <check_pidfile+0x64>
mov    %eax,%ebx
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <lockf@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
je     11b5 <check_pidfile+0x85>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
setne  %cl
mov    0x2e2b(%rip),%rdx
cmp    (%rdx),%rax
setne  %al
and    %cl,%al
movzbl %al,%eax
neg    %eax
jmp    11ba <check_pidfile+0x8a>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
sete   %dl
lea    (%rdx,%rdx,1),%eax
add    $0xffffffff,%eax
jmp    11ba <check_pidfile+0x8a>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@F_TLOCK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @check_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 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 1, i32* %2
br label %36
16: ; preds = %11
store i32 -1, i32* %2
br label %36
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @F_TLOCK
%20 = call i32 @lockf(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 1, i32* %2
br label %36
26: ; preds = %17
%27 = load i64, i64* @errno
%28 = load i64, i64* @EACCES
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
store i32 0, i32* %2
br label %36
35: ; preds = %30
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %34, %25, %16, %15
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@F_TLOCK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @check_pidfile(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
%9 = select i1 %8, i32 1, i32 -1
br label %23
10: ; preds = %1
%11 = load i32, i32* @F_TLOCK
%12 = tail call i32 @lockf(i32 %3, i32 %11, i32 0)
%13 = tail call i32 @close(i32 %3)
%14 = icmp eq i32 %12, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EACCES
%18 = icmp ne i64 %16, %17
%19 = load i64, i64* @EAGAIN
%20 = icmp ne i64 %16, %19
%21 = select i1 %18, i1 %20, i1 false
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %15, %10, %5
%24 = phi i32 [ %9, %5 ], [ 1, %10 ], [ %22, %15 ]
ret i32 %24
}