#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 /*<<< orphan*/ printf(char *, char *);
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;
  }
  printf("full path %s\n", buffer);
  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    0xeb1(%rip),%rdi
call   1030 <readlink@plt>
cmp    $0x0,%rax
jg     116b <get_executable_path+0x3b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c7 <get_executable_path+0x97>
mov    -0x10(%rbp),%rsi
lea    0xe99(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <get_executable_path+0x75>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c7 <get_executable_path+0x97>
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    0xebf(%rip),%rdi
mov    %r14,%rsi
call   1030 <readlink@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
jle    1184 <get_executable_path+0x54>
lea    0xeb3(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1184 <get_executable_path+0x54>
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"
@.str.1 = [14 x i8] c"full path %s\0A\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 %29
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %4
%16 = call i8* @strrchr(i8* %15, i8 signext 47)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %12
store i64 -1, i64* %3
br label %29
20: ; preds = %12
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
%23 = load i8*, i8** %6
store i8 0, i8* %23
%24 = load i8*, i8** %6
%25 = load i8*, i8** %4
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
store i64 %28, i64* %3
br label %29
29: ; preds = %20, %19, %11
%30 = load i64, i64* %3
ret i64 %30
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [14 x i8] c"full path %s\0A\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 %14, label %5
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %7, i64 1
store i8 0, i8* %10
%11 = ptrtoint i8* %10 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
br label %14
14: ; preds = %5, %2, %9
%15 = phi i64 [ %13, %9 ], [ -1, %2 ], [ -1, %5 ]
ret i64 %15
}