#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int readlink(char *, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *strrchr(char *, char);
char *get_path(char *path, size_t size) {
  if (-1 == readlink("/proc/self/exe", path, size)) {
    fprintf(stderr, "Failed to read /proc/self/exe: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  char *last_slash = strrchr(path, '/');
  if (last_slash) {
    (*last_slash) = 0;
  }
  return path;
}


x86 assembly (clang, -O0):
<get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe91(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11bd <get_path+0x6d>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e1 <get_path+0x91>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path>:
push   %rbx
mov    %rsi,%rdx
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     1185 <get_path+0x35>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1180 <get_path+0x30>
movb   $0x0,(%rax)
mov    %rbx,%rax
pop    %rbx
ret
mov    0x2e54(%rip),%rax
mov    (%rax),%ebx
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe6c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Failed to read /proc/self/exe: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @get_path(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6, i64 %7)
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i8*, i8** %5
store i8 0, i8* %23
br label %24
24: ; preds = %22, %17
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Failed to read /proc/self/exe: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @get_path(i8* returned %0, i64 %1) {
%3 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8 0, i8* %13
br label %16
16: ; preds = %15, %12
ret i8* %0
}