#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 */
scalar_t__ readlink(char *, char *, size_t);
char *strrchr(char *, char);
size_t get_executable_path(char *buffer, size_t len) {
  char *path_end;
  if (readlink("/proc/self/exe", buffer, len) <= 0)
    return -1;
  path_end = strrchr(buffer, '/');
  if (path_end == NULL)
    return -1;
  ++path_end;
  *path_end = '\0';
  return (size_t)(path_end - buffer);
}


x86 assembly (clang, -O0):
<get_executable_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xec1(%rip),%rdi
call   1030 <readlink@plt>
cmp    $0x0,%rax
jg     115b <get_executable_path+0x3b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a7 <get_executable_path+0x87>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1185 <get_executable_path+0x65>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a7 <get_executable_path+0x87>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_executable_path>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rdx
mov    %rdi,%r14
lea    0xecf(%rip),%rdi
mov    %r14,%rsi
call   1030 <readlink@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
jle    1165 <get_executable_path+0x45>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1165 <get_executable_path+0x45>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,0x1(%rax)
sub    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
define i64 @get_executable_path(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = load i64, i64* %5
%9 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i64 %8)
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i64 -1, i64* %3
br label %27
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strrchr(i8* %13, i8 signext 47)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %12
store i64 -1, i64* %3
br label %27
18: ; preds = %12
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %6
%21 = load i8*, i8** %6
store i8 0, i8* %21
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
store i64 %26, i64* %3
br label %27
27: ; preds = %18, %17, %11
%28 = load i64, i64* %3
ret i64 %28
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i64 @get_executable_path(i8* %0, i64 %1) {
%3 = tail call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1)
%4 = icmp slt i64 %3, 1
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %9
%10 = ptrtoint i8* %9 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
br label %13
13: ; preds = %5, %2, %8
%14 = phi i64 [ %12, %8 ], [ -1, %2 ], [ -1, %5 ]
ret i64 %14
}