#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(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strrchr(char *, char);
const char *getFilePath(char *dst, int size, const char *fileName) {
  if (readlink("/proc/self/exe", dst, size) == -1) {
    perror("readlink: ");
    exit(1);
  }
  char *last = strrchr(dst, '/');
  if (!last) {
    printf("Failed getting binary executable");
    exit(1);
  }
  strcpy(last + 1, fileName);
  return dst;
}


x86 assembly (clang, -O0):
<getFilePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe7f(%rip),%rdi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
jne    11a5 <getFilePath+0x45>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <getFilePath+0x78>
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFilePath>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%edx
mov    %rdi,%rbx
lea    0xe8d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ac <getFilePath+0x4c>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11c2 <getFilePath+0x62>
add    $0x1,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [11 x i8] c"readlink: \00"
@.str.2 = [33 x i8] c"Failed getting binary executable\00"
define i8* @getFilePath(i8* returned %0, i32 %1, i8* %2) {
%4 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = getelementptr inbounds i8, i8* %10, i64 1
%17 = tail call i32 @strcpy(i8* %16, i8* %2)
ret i8* %0
}