#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ENXIO;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ X_OK;
int access(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ pldebug(char *, char *, ...);
int readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int plFindName(char *p) {
  int n;
  char buf[1024], *cp;
  struct stat sbuf;
  pldebug("plFindName", "Trying to find %s\n", p);
  while ((n = readlink(p, buf, 1024)) > 0) {
    pldebug("plFindName", "Readlink read %d chars at: %s\n", n, p);
    if (buf[0] == '/') {
      strncpy(p, buf, n);
      p[n] = '\0';
      pldebug("plFindName", "Link is absolute: %s\n", p);
    } else {
      cp = 1 + strrchr(p, '/');
      strncpy(cp, buf, n);
      cp[n] = '\0';
      pldebug("plFindName", "Link is relative: %s\n\tTotal path:%s\n", cp, p);
    }
  }
  if (errno == EINVAL || errno == ENXIO) {
    pldebug("plFindName", "%s may be the one...\n", p);
    if ((stat(p, &sbuf) == 0) && S_ISREG(sbuf.st_mode)) {
      pldebug("plFindName", "%s is a regular file\n", p);
      return (access(p, X_OK));
    }
  }
  pldebug("plFindName", "%s found but is not executable\n", p);
  return (errno ? errno : -1);
}


x86 assembly (clang, -O0):
<plFindName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    0xe76(%rip),%rdi
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1285 <plFindName+0x115>
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe39(%rip),%rdi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
movsbl -0x420(%rbp),%eax
cmp    $0x2f,%eax
jne    1222 <plFindName+0xb2>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdx
lea    0xdf1(%rip),%rdi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
jmp    1280 <plFindName+0x110>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x428(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd8e(%rip),%rdi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
jmp    1198 <plFindName+0x28>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
je     12b9 <plFindName+0x149>
mov    0x2d32(%rip),%rax
mov    (%rax),%rax
mov    0x2d20(%rip),%rcx
cmp    (%rcx),%rax
jne    1339 <plFindName+0x1c9>
mov    -0x10(%rbp),%rdx
lea    0xd3c(%rip),%rdi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1334 <plFindName+0x1c4>
mov    -0x430(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%rax
je     1334 <plFindName+0x1c4>
mov    -0x10(%rbp),%rdx
lea    0xcf4(%rip),%rdi
lea    0xd7b(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
mov    -0x10(%rbp),%rdi
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
mov    %eax,-0x4(%rbp)
jmp    1396 <plFindName+0x226>
jmp    1339 <plFindName+0x1c9>
mov    -0x10(%rbp),%rdx
lea    0xcbc(%rip),%rdi
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1050 <pldebug@plt>
mov    0x2c7f(%rip),%rax
cmpq   $0x0,(%rax)
je     1379 <plFindName+0x209>
mov    0x2c6e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x438(%rbp)
jmp    138c <plFindName+0x21c>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x438(%rbp)
jmp    138c <plFindName+0x21c>
mov    -0x438(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plFindName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
lea    0xe79(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <pldebug@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    $0x400,%edx
call   1040 <readlink@plt>
test   %eax,%eax
jle    1289 <plFindName+0x119>
mov    %eax,%r13d
lea    0xe40(%rip),%r12
lea    0xe57(%rip),%r15
lea    0x10(%rsp),%rbx
mov    %r14,0x8(%rsp)
jmp    1243 <plFindName+0xd3>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbp
mov    %r15,%r14
lea    0x1(%rax),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
mov    %r13d,%eax
movb   $0x0,0x1(%rbp,%rax,1)
mov    %r12,%rdi
lea    0xe3c(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%r15
mov    0x8(%rsp),%r14
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <pldebug@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    1289 <plFindName+0x119>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <pldebug@plt>
cmpb   $0x2f,0x10(%rsp)
jne    11e0 <plFindName+0x70>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
mov    %r13d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r12,%rdi
lea    0xdc0(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <pldebug@plt>
jmp    122c <plFindName+0xbc>
mov    0x2d48(%rip),%rbp
mov    0x0(%rbp),%rax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%rax
je     12ac <plFindName+0x13c>
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    12d4 <plFindName+0x164>
lea    0xd4d(%rip),%rdi
lea    0xdbe(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <pldebug@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     130a <plFindName+0x19a>
lea    0xd25(%rip),%rdi
lea    0xdc2(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <pldebug@plt>
mov    0x0(%rbp),%rcx
cmp    $0x1,%rcx
sbb    %eax,%eax
or     %ecx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1080 <S_ISREG@plt>
test   %rax,%rax
je     12d4 <plFindName+0x164>
lea    0xce2(%rip),%rdi
lea    0xd69(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <pldebug@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <access@plt>
jmp    12f8 <plFindName+0x188>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"plFindName\00"
@.str.1 = [19 x i8] c"Trying to find %s\0A\00"
@.str.2 = [31 x i8] c"Readlink read %d chars at: %s\0A\00"
@.str.3 = [22 x i8] c"Link is absolute: %s\0A\00"
@.str.4 = [37 x i8] c"Link is relative: %s\0A\09Total path:%s\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@ENXIO = global i64 0
@.str.5 = [22 x i8] c"%s may be the one...\0A\00"
@.str.6 = [22 x i8] c"%s is a regular file\0A\00"
@X_OK = global i32 0
@.str.7 = [32 x i8] c"%s found but is not executable\0A\00"
define i32 @plFindName(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca %struct.stat
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %49, %1
%11 = load i8*, i8** %3
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i32 @readlink(i8* %11, i8* %12, i32 1024)
store i32 %13, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %50
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i8*, i8** %3
%18 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br i1 %22, label %23, label %34
23: ; preds = %15
%24 = load i8*, i8** %3
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @strncpy(i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %3
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %3
%33 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %49
34: ; preds = %15
%35 = load i8*, i8** %3
%36 = call i8* @strrchr(i8* %35, i8 signext 47)
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = load i32, i32* %4
%41 = call i32 @strncpy(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %6
%47 = load i8*, i8** %3
%48 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %46, i8* %47)
br label %49
49: ; preds = %34, %23
br label %10
50: ; preds = %10
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINVAL
%53 = icmp eq i64 %51, %52
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i64, i64* @errno
%56 = load i64, i64* @ENXIO
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %76
58: ; preds = %54, %50
%59 = load i8*, i8** %3
%60 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = load i8*, i8** %3
%62 = call i64 @stat(i8* %61, %struct.stat* %7)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %75
64: ; preds = %58
%65 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i64 @S_ISREG(i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %64
%70 = load i8*, i8** %3
%71 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %70)
%72 = load i8*, i8** %3
%73 = load i32, i32* @X_OK
%74 = call i32 @access(i8* %72, i32 %73)
store i32 %74, i32* %2
br label %87
75: ; preds = %64, %58
br label %76
76: ; preds = %75, %54
%77 = load i8*, i8** %3
%78 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %77)
%79 = load i64, i64* @errno
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = load i64, i64* @errno
br label %84
83: ; preds = %76
br label %84
84: ; preds = %83, %81
%85 = phi i64 [ %82, %81 ], [ -1, %83 ]
%86 = trunc i64 %85 to i32
store i32 %86, i32* %2
br label %87
87: ; preds = %84, %69
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"plFindName\00"
@.str.1 = [19 x i8] c"Trying to find %s\0A\00"
@.str.2 = [31 x i8] c"Readlink read %d chars at: %s\0A\00"
@.str.3 = [22 x i8] c"Link is absolute: %s\0A\00"
@.str.4 = [37 x i8] c"Link is relative: %s\0A\09Total path:%s\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@ENXIO = global i64 0
@.str.5 = [22 x i8] c"%s may be the one...\0A\00"
@.str.6 = [22 x i8] c"%s is a regular file\0A\00"
@X_OK = global i32 0
@.str.7 = [32 x i8] c"%s found but is not executable\0A\00"
define i32 @plFindName(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = call i32 @readlink(i8* %0, i8* %4, i32 1024)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %1, %26
%10 = phi i32 [ %27, %26 ], [ %7, %1 ]
%11 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %10, i8* %0)
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 47
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = call i32 @strncpy(i8* %0, i8* %4, i32 %10)
%16 = zext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
%18 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %26
19: ; preds = %9
%20 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = call i32 @strncpy(i8* %21, i8* %4, i32 %10)
%23 = zext i32 %10 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %21, i8* %0)
br label %26
26: ; preds = %19, %14
%27 = call i32 @readlink(i8* %0, i8* %4, i32 1024)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %9, label %29
29: ; preds = %26, %1
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINVAL
%32 = icmp eq i64 %30, %31
%33 = load i64, i64* @ENXIO
%34 = icmp eq i64 %30, %33
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %36, label %49
36: ; preds = %29
%37 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%38 = call i64 @stat(i8* %0, %struct.stat* %3)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @S_ISREG(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %40
%46 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %0)
%47 = load i32, i32* @X_OK
%48 = call i32 @access(i8* %0, i32 %47)
br label %55
49: ; preds = %29, %36, %40
%50 = call i32 (i8*, i8*, ...) @pldebug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %0)
%51 = load i64, i64* @errno
%52 = icmp eq i64 %51, 0
%53 = trunc i64 %51 to i32
%54 = select i1 %52, i32 -1, i32 %53
br label %55
55: ; preds = %49, %45
%56 = phi i32 [ %48, %45 ], [ %54, %49 ]
ret i32 %56
}