#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*/ link_target;

/* Variables and functions */
int /*<<< orphan*/ abort();
int readlink(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
scalar_t__ xmalloc(size_t);
char *get_self_executable_directory() {
  int rval;
  char link_target[1024];
  char *last_slash;
  size_t result_length;
  char *result;
  rval = readlink("/proc/self/exe", link_target, sizeof(link_target));
  if (rval == -1)
    abort();
  else
    link_target[rval] = '\0';
  last_slash = strrchr(link_target, '/');
  if (last_slash == NULL || last_slash == link_target)
    abort();
  result_length = last_slash - link_target;
  result = (char *)xmalloc(result_length + 1);
  strncpy(result, link_target, result_length);
  result[result_length] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<get_self_executable_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
lea    -0x410(%rbp),%rsi
lea    0xe97(%rip),%rdi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1187 <get_self_executable_directory+0x37>
mov    $0x0,%al
call   1030 <abort@plt>
movslq -0x4(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     11d0 <get_self_executable_directory+0x80>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
jne    11d7 <get_self_executable_directory+0x87>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
add    $0x1,%rdi
call   1070 <xmalloc@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x420(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x428(%rbp),%rax
mov    -0x420(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_self_executable_directory>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xe9d(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
cmp    $0xffffffff,%eax
je     11c7 <get_self_executable_directory+0x77>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11c7 <get_self_executable_directory+0x77>
mov    %rax,%rbx
sub    %r14,%rbx
je     11c7 <get_self_executable_directory+0x77>
lea    0x1(%rbx),%rdi
call   1070 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_self_executable_directory() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6, i32 1024)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %12
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%18 = call i8* @strrchr(i8* %17, i8 signext 47)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, null
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21, %16
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %21
%28 = load i8*, i8** %3
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
store i64 %32, i64* %4
%33 = load i64, i64* %4
%34 = add i64 %33, 1
%35 = call i64 @xmalloc(i64 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %5
%37 = load i8*, i8** %5
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%39 = load i64, i64* %4
%40 = call i32 @strncpy(i8* %37, i8* %38, i64 %39)
%41 = load i8*, i8** %5
%42 = load i64, i64* %4
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %5
ret i8* %44
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_self_executable_directory() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %2, i32 1024)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %0
%8 = sext i32 %3 to i64
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %8
store i8 0, i8* %9
%10 = call i8* @strrchr(i8* %2, i8 signext 47)
%11 = icmp eq i8* %10, null
%12 = icmp eq i8* %10, %2
%13 = or i1 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %7
%17 = ptrtoint i8* %10 to i64
%18 = ptrtoint [1024 x i8]* %1 to i64
%19 = sub i64 %17, %18
%20 = add i64 %19, 1
%21 = call i64 @xmalloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = call i32 @strncpy(i8* %22, i8* %2, i64 %19)
%24 = getelementptr inbounds i8, i8* %22, i64 %19
store i8 0, i8* %24
ret i8* %22
}