#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 asprintf(char **, char *, char *, char const *);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pl_fatal(char *, ...);
char *realpath(char *, int /*<<< orphan*/ *);
char *pl_path(const char *relpath) {
  char *addedpath, *normalizedpath;
  static char *herepath = NULL;
  if (!herepath) {
    if (!(herepath = realpath("/proc/self/exe", NULL)))
      pl_fatal("realpath");
    herepath = dirname(herepath);
  }
  if (asprintf(&addedpath, "%s/%s", herepath, relpath) == -1)
    pl_fatal("asprintf");
  if (!(normalizedpath = realpath(addedpath, NULL)))
    pl_fatal("realpath %s", addedpath);
  free(addedpath);
  return normalizedpath;
}


x86 assembly (clang, -O0):
<pl_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2eec(%rip)
jne    11ac <pl_path+0x5c>
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <realpath@plt>
mov    %rax,0x2ecf(%rip)
cmp    $0x0,%rax
jne    1199 <pl_path+0x49>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1070 <pl_fatal@plt>
mov    0x2eb0(%rip),%rdi
call   1040 <dirname@plt>
mov    %rax,0x2ea4(%rip)
mov    0x2e9d(%rip),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11de <pl_path+0x8e>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1070 <pl_fatal@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <realpath@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    120b <pl_path+0xbb>
mov    -0x10(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1070 <pl_fatal@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pl_path>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ef1(%rip),%rdx
test   %rdx,%rdx
jne    11a5 <pl_path+0x55>
lea    0xe95(%rip),%rdi
xor    %esi,%esi
call   1050 <realpath@plt>
mov    %rax,0x2ed7(%rip)
test   %rax,%rax
jne    1193 <pl_path+0x43>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1070 <pl_fatal@plt>
mov    0x2ebd(%rip),%rax
mov    %rax,%rdi
call   1040 <dirname@plt>
mov    %rax,%rdx
mov    %rax,0x2eab(%rip)
lea    0xe6c(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rcx
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11cc <pl_path+0x7c>
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1070 <pl_fatal@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <realpath@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f3 <pl_path+0xa3>
mov    0x8(%rsp),%rsi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1070 <pl_fatal@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@pl_path.herepath = internal global i8* null
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"realpath\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [9 x i8] c"asprintf\00"
@.str.4 = [12 x i8] c"realpath %s\00"
define i8* @pl_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** @pl_path.herepath
%6 = icmp ne i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = call i8* @realpath(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32* null)
store i8* %8, i8** @pl_path.herepath
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = load i8*, i8** @pl_path.herepath
%14 = call i8* @dirname(i8* %13)
store i8* %14, i8** @pl_path.herepath
br label %15
15: ; preds = %12, %1
%16 = load i8*, i8** @pl_path.herepath
%17 = load i8*, i8** %2
%18 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i8*, i8** %3
%24 = call i8* @realpath(i8* %23, i32* null)
store i8* %24, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %22
%30 = load i8*, i8** %3
%31 = call i32 @free(i8* %30)
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
@pl_path.herepath = internal unnamed_addr global i8* null
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"realpath\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [9 x i8] c"asprintf\00"
@.str.4 = [12 x i8] c"realpath %s\00"
define i8* @pl_path(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = load i8*, i8** @pl_path.herepath
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %15
6: ; preds = %1
%7 = tail call i8* @realpath(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32* null)
store i8* %7, i8** @pl_path.herepath
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** @pl_path.herepath
br label %12
12: ; preds = %9, %6
%13 = phi i8* [ %11, %9 ], [ %7, %6 ]
%14 = tail call i8* @dirname(i8* %13)
store i8* %14, i8** @pl_path.herepath
br label %15
15: ; preds = %12, %1
%16 = phi i8* [ %14, %12 ], [ %4, %1 ]
%17 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = load i8*, i8** %2
%23 = call i8* @realpath(i8* %22, i32* null)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = call i32 (i8*, ...) @pl_fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %21
%29 = load i8*, i8** %2
%30 = call i32 @free(i8* %29)
ret i8* %23
}