#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 ssize_t;

/* Variables and functions */
int PATH_MAX;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int getpid();
int /*<<< orphan*/ log_error(char *, ...);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *get_executable_name() {
  char buffer[PATH_MAX];
  snprintf(buffer, PATH_MAX, "/proc/%u/exe", getpid());
  char *filename = (char *)calloc(1, PATH_MAX);
  if (filename == NULL) {
    log_error("Error: calloc returned null, system out of memory.");
    return NULL;
  }
  ssize_t len = readlink(buffer, filename, PATH_MAX);
  if (len == -1) {
    log_error("Can't get executable name from \"%s\": %s", buffer,
              strerror(errno));
    free(filename);
    return NULL;
  }
  if (len >= PATH_MAX) {
    log_error("Executable name %.*s is longer than %d characters.", PATH_MAX,
              filename, PATH_MAX);
    free(filename);
    return NULL;
  }
  return filename;
}


x86 assembly (clang, -O0):
<get_executable_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %eax,%ecx
lea    0xe39(%rip),%rdx
call   1070 <snprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1210 <get_executable_name+0xa0>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12d4 <get_executable_name+0x164>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   1040 <readlink@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1272 <get_executable_name+0x102>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12d4 <get_executable_name+0x164>
mov    -0x28(%rbp),%eax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%eax
jl     12c5 <get_executable_name+0x155>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1060 <log_error@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12d4 <get_executable_name+0x164>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_executable_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6f(%rip),%r15
mov    (%r15),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe5a(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    (%r15),%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
test   %rax,%rax
je     11f7 <get_executable_name+0x87>
mov    %rax,%r14
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     120a <get_executable_name+0x9a>
mov    (%r15),%ecx
cmp    %ecx,%eax
jl     1236 <get_executable_name+0xc6>
lea    0xe7f(%rip),%rdi
mov    %ecx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <log_error@plt>
jmp    122b <get_executable_name+0xbb>
lea    0xe0f(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <log_error@plt>
jmp    1236 <get_executable_name+0xc6>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <log_error@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%u/exe\00"
@.str.1 = [51 x i8] c"Error: calloc returned null, system out of memory.\00"
@.str.2 = [40 x i8] c"Can't get executable name from \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [51 x i8] c"Executable name %.*s is longer than %d characters.\00"
define i8* @get_executable_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %2
%10 = alloca i8, i64 %8
store i64 %8, i64* %3
%11 = load i32, i32* @PATH_MAX
%12 = call i32 (...) @getpid()
%13 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @PATH_MAX
%15 = call i64 @calloc(i32 1, i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
store i32 1, i32* %5
br label %46
21: ; preds = %0
%22 = load i8*, i8** %4
%23 = load i32, i32* @PATH_MAX
%24 = call i32 @readlink(i8* %10, i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %10, i32 %29)
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
store i8* null, i8** %1
store i32 1, i32* %5
br label %46
33: ; preds = %21
%34 = load i32, i32* %6
%35 = load i32, i32* @PATH_MAX
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* @PATH_MAX
%39 = load i8*, i8** %4
%40 = load i32, i32* @PATH_MAX
%41 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %39, i32 %40)
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
store i8* null, i8** %1
store i32 1, i32* %5
br label %46
44: ; preds = %33
%45 = load i8*, i8** %4
store i8* %45, i8** %1
store i32 1, i32* %5
br label %46
46: ; preds = %44, %37, %27, %19
%47 = load i8*, i8** %2
%48 = load i8*, i8** %1
ret i8* %48
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%u/exe\00"
@.str.1 = [51 x i8] c"Error: calloc returned null, system out of memory.\00"
@.str.2 = [40 x i8] c"Can't get executable name from \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [51 x i8] c"Executable name %.*s is longer than %d characters.\00"
define i8* @get_executable_name() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32 (...) @getpid()
%5 = call i32 @snprintf(i8* %3, i32 %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @PATH_MAX
%7 = call i64 @calloc(i32 1, i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %27
12: ; preds = %0
%13 = load i32, i32* @PATH_MAX
%14 = call i32 @readlink(i8* %3, i8* %8, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %3, i32 %18)
%20 = call i32 @free(i8* %8)
br label %27
21: ; preds = %12
%22 = load i32, i32* @PATH_MAX
%23 = icmp slt i32 %14, %22
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %22, i8* %8, i32 %22)
%26 = call i32 @free(i8* %8)
br label %27
27: ; preds = %16, %24, %21, %10
%28 = phi i8* [ null, %10 ], [ null, %16 ], [ null, %24 ], [ %8, %21 ]
ret i8* %28
}