#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 */
typedef int /*<<< orphan*/ path;

/* Variables and functions */
char *getenv(char *);
int readlink(char *, char *, int);
char *executable_path() {
  static char path[200];
  int length = readlink("/proc/self/exe", path, sizeof(path));
  if (length < 0 || length >= 200) {
    char *p = getenv("   ");
    if (p == NULL)
      p = getenv("_");
    return p;
  }
  path[length] = '\0';
  return path;
}


x86 assembly (clang, -O0):
<executable_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
lea    0x2f0a(%rip),%rsi
mov    $0xc8,%edx
call   1040 <readlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     115a <executable_path+0x3a>
cmpl   $0xc8,-0xc(%rbp)
jl     1192 <executable_path+0x72>
lea    0xeae(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <executable_path+0x65>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ac <executable_path+0x8c>
movslq -0xc(%rbp),%rcx
lea    0x2ea3(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e98(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executable_path>:
push   %rbx
lea    0xed8(%rip),%rdi
lea    0x2f11(%rip),%rbx
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1040 <readlink@plt>
cmp    $0xc8,%eax
jb     1164 <executable_path+0x44>
lea    0xec5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116a <executable_path+0x4a>
lea    0xeb5(%rip),%rdi
pop    %rbx
jmp    1030 <getenv@plt>
mov    %eax,%eax
movb   $0x0,(%rax,%rbx,1)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@executable_path.path = internal global [200 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [4 x i8] c" \00"
@.str.2 = [2 x i8] c"_\00"
define i8* @executable_path() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 0), i32 200)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = load i32, i32* %2
%9 = icmp sge i32 %8, 200
br i1 %9, label %10, label %18
10: ; preds = %7, %0
%11 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i8* @getenv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %15, i8** %3
br label %16
16: ; preds = %14, %10
%17 = load i8*, i8** %3
store i8* %17, i8** %1
br label %22
18: ; preds = %7
%19 = load i32, i32* %2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 %20
store i8 0, i8* %21
store i8* getelementptr inbounds ([200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 0), i8** %1
br label %22
22: ; preds = %18, %16
%23 = load i8*, i8** %1
ret i8* %23
}

LLVM IR (-O2):
@executable_path.path = internal global [200 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [4 x i8] c" \00"
@.str.2 = [2 x i8] c"_\00"
define i8* @executable_path() {
%1 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 0), i32 200)
%2 = icmp ugt i32 %1, 199
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %11
8: ; preds = %0
%9 = zext i32 %1 to i64
%10 = getelementptr inbounds [200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 %9
store i8 0, i8* %10
br label %11
11: ; preds = %3, %6, %8
%12 = phi i8* [ getelementptr inbounds ([200 x i8], [200 x i8]* @executable_path.path, i64 0, i64 0), %8 ], [ %7, %6 ], [ %4, %3 ]
ret i8* %12
}