#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*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
char *strrchr(char *, char);
int check_pidfile(const char *path) {
  int rv = -1;
  char *dup = NULL;
  char *end = NULL;
  dup = strdup(path);
  if (!dup)
    goto out;
  end = strrchr(dup, '/');
  if (!end)
    goto out;
  end[0] = '\0';
  if (access(dup, W_OK) != 0)
    goto out;
  rv = 0;
out:
  free(dup);
  dup = end = NULL;
  return rv;
}


x86 assembly (clang, -O0):
<check_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <check_pidfile+0x40>
jmp    11d1 <check_pidfile+0x91>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <check_pidfile+0x62>
jmp    11d1 <check_pidfile+0x91>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     11ca <check_pidfile+0x8a>
jmp    11d1 <check_pidfile+0x91>
movl   $0x0,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pidfile>:
push   %rbp
push   %rbx
push   %rax
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1180 <check_pidfile+0x40>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1180 <check_pidfile+0x40>
movb   $0x0,(%rax)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@W_OK = global i32 0
define i32 @check_pidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %3
store i8* null, i8** %4
store i8* null, i8** %5
%6 = load i8*, i8** %2
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
br label %26
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i8* @strrchr(i8* %12, i8 signext 47)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %11
br label %26
17: ; preds = %11
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
%20 = load i8*, i8** %4
%21 = load i32, i32* @W_OK
%22 = call i64 @access(i8* %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %26
25: ; preds = %17
store i32 0, i32* %3
br label %26
26: ; preds = %25, %24, %16, %10
%27 = load i8*, i8** %4
%28 = call i32 @free(i8* %27)
store i8* null, i8** %5
store i8* null, i8** %4
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@W_OK = global i32 0
define i32 @check_pidfile(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = tail call i8* @strrchr(i8* %2, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
store i8 0, i8* %5
%8 = load i32, i32* @W_OK
%9 = tail call i64 @access(i8* %2, i32 %8)
%10 = icmp ne i64 %9, 0
%11 = sext i1 %10 to i32
br label %12
12: ; preds = %7, %4, %1
%13 = phi i32 [ -1, %4 ], [ -1, %1 ], [ %11, %7 ]
%14 = tail call i32 @free(i8* %2)
ret i32 %13
}