#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 pid_t;
typedef int /*<<< orphan*/ linkname;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ printf(char *);
int readlink(char *, char *, int);
scalar_t__ snprintf(char *, int, char *, int);
bool getExePath(char path[]) {
  char linkname[1024];
  pid_t pid;
  int ret;
  pid = getpid();
  if (snprintf(linkname, sizeof(linkname), "/proc/%d/exe", pid) == 0) {
    printf("Failure to find executable path.\n");
    exit(EXIT_FAILURE);
  }
  ret = readlink(linkname, path, 1024);
  if (ret == -1) {
    path[0] = '\0';
    return false;
  }
  if (ret >= 1024) {
    errno = ERANGE;
    path[0] = '\0';
    return false;
  }
  path[ret] = '\0';
  for (; path[ret] != '/'; ret--) {
    path[ret] = '\0';
  }
  return true;
}


x86 assembly (clang, -O0):
<getExePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%ecx
mov    $0x400,%esi
lea    0xe7b(%rip),%rdx
call   1060 <snprintf@plt>
cmp    $0x0,%rax
jne    11ae <getExePath+0x5e>
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    11e9 <getExePath+0x99>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    126f <getExePath+0x11f>
cmpl   $0x400,-0x418(%rbp)
jl     121e <getExePath+0xce>
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcf(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    126f <getExePath+0x11f>
mov    -0x10(%rbp),%rax
movslq -0x418(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x418(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1268 <getExePath+0x118>
mov    -0x10(%rbp),%rax
movslq -0x418(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x418(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x418(%rbp)
jmp    122d <getExePath+0xdd>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getExePath>:
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe97(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
test   %rax,%rax
je     11db <getExePath+0x8b>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ab <getExePath+0x5b>
cmp    $0x400,%eax
jl     11b2 <getExePath+0x62>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    0x2e2f(%rip),%rcx
mov    %eax,(%rcx)
movb   $0x0,(%rbx)
xor    %eax,%eax
jmp    11d2 <getExePath+0x82>
movslq %eax,%rcx
lea    (%rbx,%rcx,1),%rax
movb   $0x0,(%rbx,%rcx,1)
nopl   (%rax)
movb   $0x0,(%rax)
cmpb   $0x2f,-0x1(%rax)
lea    -0x1(%rax),%rax
jne    11c0 <getExePath+0x70>
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
ret
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [34 x i8] c"Failure to find executable path.\0A\00"
@EXIT_FAILURE = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
define i32 @getExePath(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 (...) @getpid()
store i32 %7, i32* %5
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %5
%10 = call i64 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 @readlink(i8* %17, i8* %18, i32 1024)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 0
store i8 0, i8* %24
store i32 0, i32* %2
br label %54
25: ; preds = %16
%26 = load i32, i32* %6
%27 = icmp sge i32 %26, 1024
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @ERANGE
store i32 %29, i32* @errno
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 0, i8* %31
store i32 0, i32* %2
br label %54
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %50, %32
%38 = load i8*, i8** %3
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 47
br i1 %44, label %45, label %53
45: ; preds = %37
%46 = load i8*, i8** %3
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
br label %50
50: ; preds = %45
%51 = load i32, i32* %6
%52 = add nsw i32 %51, -1
store i32 %52, i32* %6
br label %37
53: ; preds = %37
store i32 1, i32* %2
br label %54
54: ; preds = %53, %28, %22
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/exe\00"
@EXIT_FAILURE = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
@str = [33 x i8] c"Failure to find executable path.\00"
define i32 @getExePath(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = call i64 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = call i32 @readlink(i8* %3, i8* %0, i32 1024)
%13 = icmp eq i32 %12, -1
br i1 %13, label %28, label %14
14: ; preds = %11
%15 = icmp sgt i32 %12, 1023
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = load i32, i32* @ERANGE
store i32 %17, i32* @errno
br label %28
18: ; preds = %14
%19 = sext i32 %12 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %18, %21
%22 = phi i64 [ %19, %18 ], [ %24, %21 ]
%23 = phi i8* [ %20, %18 ], [ %25, %21 ]
store i8 0, i8* %23
%24 = add i64 %22, -1
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 47
br i1 %27, label %29, label %21
28: ; preds = %11, %16
store i8 0, i8* %0
br label %29
29: ; preds = %21, %28
%30 = phi i32 [ 0, %28 ], [ 1, %21 ]
ret i32 %30
}