#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 */
char *malloc(int);
int readlink(char *, char *, int);
char *executable_path() {
  char buf[1024];
  ssize_t n = readlink("/proc/self/exe", buf, 1024);
  char *out = malloc(sizeof(char) * (n + 1));
  for (int i = 0; i < n; i++) {
    out[i] = buf[i];
  }
  out[n] = '\0';
  return out;
}


x86 assembly (clang, -O0):
<executable_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    -0x400(%rbp),%rsi
lea    0xec7(%rip),%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x404(%rbp)
mov    -0x404(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x410(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x414(%rbp),%eax
cmp    -0x404(%rbp),%eax
jge    11b5 <executable_path+0x95>
movslq -0x414(%rbp),%rax
mov    -0x400(%rbp,%rax,1),%dl
mov    -0x410(%rbp),%rax
movslq -0x414(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    1170 <executable_path+0x50>
mov    -0x410(%rbp),%rax
movslq -0x404(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x410(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executable_path>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xebf(%rip),%rdi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
jle    116c <executable_path+0x3c>
mov    %ebx,%edx
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @executable_path() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%6 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 1024)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = trunc i64 %10 to i32
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %3
store i32 0, i32* %4
br label %13
13: ; preds = %26, %0
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
br label %26
26: ; preds = %17
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %13
29: ; preds = %13
%30 = load i8*, i8** %3
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define noalias i8* @executable_path() {
%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 = add nsw i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %7, %0
%11 = sext i32 %3 to i64
%12 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 0, i8* %12
ret i8* %5
}